generated from kadraman/pygame-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
90 lines (78 loc) · 2.54 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
import pygame as pg
import constants
class Game(object):
"""
A single instance of this class is responsible for
managing which individual game state is active
and keeping it updated. It also handles many of
pygame's nuts and bolts (managing the event
queue, framerate, updating the display, etc.).
and its run method serves as the "game loop".
"""
def __init__(self, screen, controller, states, start_state):
"""
Initialize the Game object.
:param screen: the pygame display surface
:param states: a dict mapping state-names to GameState objects
:param start_state: name of the first active game state
"""
self.done = False
self.screen = screen
self.controller = controller
self.clock = pg.time.Clock()
self.fps = constants.FPS
self.states = states
self.state_name = start_state
self.state = self.states[self.state_name]
def event_loop(self):
"""
Events are passed for handling to the current state.
"""
for event in pg.event.get():
self.state.get_event(event, self.controller)
def flip_state(self):
"""
Switch to the next game state.
"""
current_state = self.state_name
self.state.cleanup()
next_state = self.state.next_state
self.state.done = False
self.state_name = next_state
persistent = self.state.persist
self.state = self.states[self.state_name]
self.state.startup(persistent)
def update(self, dt):
"""
Check for state flip and update active state.
:param dt: delta time since last frame
"""
if self.state.quit:
self.done = True
elif self.state.done:
self.flip_state()
self.state.update(dt)
def draw(self):
"""
Pass display surface to active state for drawing.
"""
self.state.draw(self.screen)
def run(self):
"""
Main game loop
"""
# run startup on first state
persistent = self.state.persist
self.state.startup(persistent)
if not constants.PLAY_SOUNDS:
pg.mixer.pause()
dt = 0
while not self.done:
# dt = self.clock.tick(self.fps) * 0.1
dt = self.clock.tick(self.fps) / 1000.0
self.event_loop()
self.update(dt)
self.draw()
pg.display.flip()
if constants.PLAY_SOUNDS:
pg.mixer.music.stop()