-
Notifications
You must be signed in to change notification settings - Fork 2
/
state_engine.py
132 lines (110 loc) · 3.85 KB
/
state_engine.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
import sys
from itertools import cycle
import pygame as pg
import prepare
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, states, start_state):
"""
Initialize the Game object.
screen: the pygame display surface
states: a dict mapping state-names to GameState objects
start_state: name of the first active game state
"""
self.done = False
self.screen = screen
self.clock = pg.time.Clock()
self.fps = 60
self.states = states
self.state_name = start_state
self.state = self.states[self.state_name]
self.fullscreen = False
def toggle_fullscreen(self):
self.fullscreen = not self.fullscreen
if self.fullscreen:
self.screen = pg.display.set_mode(prepare.SCREEN_SIZE, pg.FULLSCREEN)
else:
self.screen = pg.display.set_mode(prepare.SCREEN_SIZE)
def event_loop(self):
"""Events are passed for handling to the current state."""
for event in pg.event.get():
self.state.get_event(event)
if event.type == pg.KEYUP:
if event.key == pg.K_f and self.state_name != "NEW_COURSE":
self.toggle_fullscreen()
def flip_state(self):
"""Switch to the next game state."""
current_state = self.state_name
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.
dt: milliseconds 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):
"""
Pretty much the entirety of the game's runtime will be
spent inside this while loop.
"""
while not self.done:
dt = self.clock.tick(self.fps)
self.event_loop()
self.update(dt)
self.draw()
pg.display.update()
pg.display.set_caption("{}".format(self.clock.get_fps()))
class GameState(object):
"""
Parent class for individual game states to inherit from.
"""
def __init__(self):
self.done = False
self.quit = False
self.next_state = None
self.screen_rect = pg.display.get_surface().get_rect()
self.persist = {}
self.font = pg.font.Font(None, 24)
def startup(self, persistent):
"""
Called when a state resumes being active.
Allows information to be passed between states.
persistent: a dict passed from state to state
"""
self.persist = persistent
def get_event(self, event):
"""
Handle a single event passed by the Game object.
"""
pass
def update(self, dt):
"""
Update the state. Called by the Game object once
per frame.
dt: time since last frame
"""
pass
def draw(self, surface):
"""
Draw everything to the screen.
"""
pass