-
Notifications
You must be signed in to change notification settings - Fork 0
/
states.py
196 lines (152 loc) · 4.59 KB
/
states.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
192
193
194
195
196
import setup
from constants import *
from entity import Player
from graphics.colors import *
pygame.init()
class States(object):
def __init__(self):
self.done = False
self.next = None
self.quit = False
self.previous = None
self.screen = SCREEN
class Menu(States):
def __init__(self):
States.__init__(self)
self.next = "game"
self.bg_color = MENUBACKGROUND
self.font = MENU_FONT
self.font_color = RED
self.menu_items = ("Start", "Options", "Quit")
def cleanup(self):
self.labels = None
print("do some cleanup")
def startup(self):
self.labels = []
self.height_text = 0
for index, item in enumerate(self.menu_items):
label = self.font.render(item, 1, self.font_color)
lbl_w, lbl_h = label.get_rect().width, label.get_rect().height
self.height_text = len(self.menu_items)*lbl_h
pos_x = (SCREEN_WIDTH / 2) - (lbl_w / 2)
pos_y = (SCREEN_HEIGHT / 2) - (self.height_text / 2) + (
index * lbl_h)
self.labels.append([item, label, (lbl_w, lbl_h), (pos_x, pos_y)])
def get_event(self, event):
if event.type == pygame.QUIT:
self.done, self.quit = True, True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.done = True
elif event.key == pygame.K_p:
print(self.screen.get_rect().width, " ", self.screen.get_rect(
).height)
elif event.key == pygame.K_ESCAPE:
self.done, self.quit = True, True
def update(self, screen, dt):
self.draw(screen)
def draw(self, screen):
screen.fill(self.bg_color)
for item, label, (w, h), (x, y) in self.labels:
self.screen.blit(label, (x, y))
class Loading(States):
def __init__(self):
States.__init__(self)
class Game(States):
def __init__(self):
States.__init__(self)
self.next = "menu"
self.all_sprites = pygame.sprite.Group()
self.wall_list = pygame.sprite.Group()
self.countHorBlock = 8
self.countVertBlock = 10
self.walls = setup.makeouterwalls(BLOCK, SCREEN_SIZE)
self.blocks = setup.makeinnerblocks(BLOCK, SCREEN_SIZE,
self.countHorBlock,
self.countVertBlock)
self.player = Player(PLAYER, 20, 20)
self.wall_list.add(self.walls, self.blocks)
self.all_sprites.add(self.wall_list, self.player)
self.player.walls = self.wall_list
def cleanup(self):
self.all_sprites = None
self.wall_list = None
self.walls = None
self.blocks = None
self.player = None
def startup(self):
self.__init__()
def get_event(self, event):
if event.type == pygame.QUIT:
self.done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.player.changespeed(-3, 0)
elif event.key == pygame.K_RIGHT:
self.player.changespeed(3, 0)
elif event.key == pygame.K_UP:
self.player.changespeed(0, -3)
elif event.key == pygame.K_DOWN:
self.player.changespeed(0, 3)
if event.key == pygame.K_SPACE:
self.all_sprites.add(self.player.putbomb())
if event.key == pygame.K_ESCAPE:
self.done = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
self.player.changespeed(3, 0)
elif event.key == pygame.K_RIGHT:
self.player.changespeed(-3, 0)
elif event.key == pygame.K_UP:
self.player.changespeed(0, 3)
elif event.key == pygame.K_DOWN:
self.player.changespeed(0, -3)
def update(self, screen, dt):
self.draw(screen)
self.all_sprites.update()
def draw(self, screen):
screen.fill(GAMEBACKGROUND)
self.all_sprites.draw(SCREEN)
class Control:
def __init__(self, **settings):
self.__dict__.update(settings)
self.done = False
self.screen = SCREEN
self.clock = CLOCK
def setup_states(self, state_dict, start_state):
self.state_dict = state_dict
self.state_name = start_state
self.state = self.state_dict[self.state_name]
self.state.startup()
def flip_state(self):
self.state.done = False
previous, self.state_name = self.state_name, self.state.next
self.state.cleanup()
self.state = self.state_dict[self.state_name]
self.state.startup()
self.state.previous = previous
def update(self, dt):
if self.state.quit:
self.done = True
elif self.state.done:
self.flip_state()
self.state.update(self.screen, dt)
def event_loop(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.done = True
self.state.get_event(event)
def main_game_loop(self):
while not self.done:
delta_time = self.clock.tick(self.fps) / 1000.0
self.event_loop()
self.update(delta_time)
pygame.display.update()
settings = {'fps': 60}
app = Control(**settings)
state_dict = {
'menu': Menu(),
'game': Game()
}
app.setup_states(state_dict, 'menu')
app.main_game_loop()