-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
executable file
·191 lines (143 loc) · 5.25 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
# Leon: Hero 273 -- A game made for a 48-hour game jam.
# Copyright (C) 2014 JT-Dev
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# 1. The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import pygame
import g
import sys
import os
import core
import constants
import managers
import utils
import entities
def init_levels(levels, window, loader):
for index, level in enumerate(levels):
world = lambda window=window, index=index, level=level, loader=loader: core.World(window, index, level, loader)
g.screen_map[index] = world
splashscreen = lambda loader=loader: core.SplashScreen(loader)
g.screen_map["splash"] = splashscreen
titlescreen = lambda loader=loader: core.TitleScreen(loader)
g.screen_map["title"] = titlescreen
creditscreen = lambda loader=loader: core.CreditScreen(loader)
g.screen_map["credit"] = creditscreen
infoscreen = lambda loader=loader: core.InfoScreen(loader)
g.screen_map["info"] = infoscreen
winscreen = lambda loader=loader: core.WinScreen(loader)
g.screen_map["win"] = winscreen
class Game(object):
def __init__(self, window, surface, loader):
self.running = True
self.force_update = False
self.fps_clock = pygame.time.Clock()
self.window = window
self.surface = surface
self.loader = loader
self.inputmanager = managers.InputManager()
self.tracks = [
loader.load("sounds/gameloops/music1.ogg", "sound"),
loader.load("sounds/gameloops/music2.ogg", "sound"),
loader.load("sounds/gameloops/music3.ogg", "sound"),
]
def init(self):
g.screens = []
levels = []
for asset in sorted(os.listdir(os.path.join(constants.ASSET_DIR, "levels"))):
if asset.endswith(".txt"):
asset = os.path.join("levels", asset)
level = [line.strip() for line in self.loader.load(asset, "text")]
levels.append(level)
init_levels(levels, self.window, self.loader)
if not g.screen_map:
utils.error("no screens! panic!")
g.screen = g.screen_map["splash"]
def _update(self):
if not g.screen_map:
utils.error(".init() not run -- what are you playing at?")
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.QUIT:
utils.debug("quit signal caught")
self.running = False
return
self.inputmanager.handle(event)
if self.inputmanager.check_key(pygame.K_q):
self.running = False
return
if self.inputmanager.check_key_single(pygame.K_m):
g.screen = g.screen_map["title"]
self.force_update = True
return
if self.inputmanager.check_key_single(pygame.K_r):
g.deaths += 1
self.force_update = True
return
#if self.inputmanager.check_key_single(pygame.K_n):
#if isinstance(self.screen, core.World):
#g.screen = g.screen_map.get(self.screen.index+1, g.screen_map["win"])
#self.force_update = True
#return
self.screen.update(self.inputmanager)
def _draw(self):
if self.force_update:
return
self.surface.fill((0, 0, 0))
self.screen.draw(self.surface)
pygame.display.update()
def run(self):
utils.debug("Starting main game loop ...")
self.screen = g.screen()
while self.running:
self._update()
self._draw()
if self.screen.peek() or self.force_update:
if (isinstance(self.screen, core.World)):
self.tracks[self.screen.index % 3].stop()
self.screen = g.screen()
if (isinstance(self.screen, core.World)):
self.tracks[self.screen.index % 3].play(loops=-1)
self.force_update = False
self.fps_clock.tick(60)
pygame.quit()
sys.exit()
def _init_game():
# Dem inits tho.
pygame.init()
pygame.display.init()
pygame.mixer.init()
# File loader
loader = utils.FileLoader(constants.ASSET_DIR)
# Icons
pygame.display.set_icon(loader.load("sprites/icon.png", "image"))
# Get best screen resolution.
constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT = pygame.display.list_modes()[0]
#constants.SCREEN_WIDTH = 640
#constants.SCREEN_HEIGHT = 480
# Buffer
constants.BUFFER_WIDTH, constants.BUFFER_HEIGHT = constants.SCREEN_WIDTH + 200, constants.SCREEN_HEIGHT + 200
# Set up screen surface.
os.environ["SDL_VIDEO_CENTERED"] = "1"
window = utils.Size(constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT)
surface = pygame.display.set_mode((window.width, window.height), pygame.FULLSCREEN)
game = Game(window, surface, loader)
game.init()
return game
def _play_game():
game = _init_game()
game.run()
if __name__ == "__main__":
_play_game()