-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.py
More file actions
220 lines (185 loc) · 6.57 KB
/
template.py
File metadata and controls
220 lines (185 loc) · 6.57 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""TODO:
* Make power up which makes 3 balls appear"""
import sys
import pygame as pg
import constants
import player
import ball
import brick
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]
def event_loop(self):
"""Events are passed for handling to the current state."""
for event in pg.event.get():
self.state.get_event(event)
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()
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("vgafix.fon", 50)
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.
"""
for event in pg.event.get():
if event.type == pg.QUIT:
self.quit = True
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
class SplashScreen(GameState):
def __init__(self):
super(SplashScreen, self).__init__()
self.main_font = pg.font.Font("vgafix.fon", 50)
self.title_text = self.main_font.render("Breakout", 0, (255, 0, 0))
self.persist["screen_color"] = constants.BLACK
self.next_state = "GAMEPLAY"
def get_event(self, event):
if event.type == pg.QUIT:
self.quit = True
elif event.type == pg.MOUSEBUTTONUP:
self.persist["screen_color"] = constants.BLACK
self.done = True
def draw(self, surface):
surface.fill(constants.BLUE)
surface.blit(self.title_text, (250, 250))
class Gameplay(GameState):
def __init__(self):
super(Gameplay, self).__init__()
self.player = player.Player()
self.ball = ball.Ball(self.player)
self.main_font = pg.font.Font("vgafix.fon", 100)
self.GO_font = pg.font.Font("PLAYBILL.ttf", 150)
self.lives_text = self.main_font.render("Lives: " + str(self.player.lives), 0, (0, 0, 255))
self.game_over_text = self.GO_font.render("You kind of suck", 0, (255, 0, 0))
self.all_sprites = constants.all_sprites
self.brick_sprites = constants.brick_sprites
self.laser_bricks = constants.laser_bricks
self.bullet_sprites = constants.bullet_sprites
self.all_sprites.add(self.player)
self.all_sprites.add(self.ball)
self.level_1()
if not self.brick_sprites:
self.level_2()
def startup(self, persistent):
self.persist = persistent
color = self.persist["screen_color"]
self.screen_color = color
def get_event(self, event):
if event.type == pg.QUIT:
self.quit = True
def update(self, dt):
self.all_sprites.update()
self.brick_sprites.update()
self.laser_bricks.update()
self.bullet_sprites.update()
self.lives_text = self.main_font.render("Lives: " + str(self.player.lives), 0, (0, 0, 255))
def draw(self, surface):
surface.fill(self.screen_color)
self.all_sprites.draw(screen)
self.brick_sprites.draw(screen)
self.laser_bricks.draw(screen)
self.bullet_sprites.draw(screen)
surface.blit(self.lives_text, (5, 10))
if self.player.game_over == True:
self.ball.kill()
self.player.kill()
surface.blit(self.game_over_text, (200, 300))
def level_1(self):
for r in range(3):
for i in range(13):
brick_img = brick.Brick((i * 64) + 10, (r * 32 + 20), img_path=constants.BLUE_DIR)
self.brick_sprites.add(brick_img)
for i in range(3):
las_img = brick.Brick((i * 64)+10, (constants.HEIGHT/2)-180, img_path=constants.RED_DIR)
self.laser_bricks.add(las_img)
def level_2(self):
for r in range(2):
for i in range(13):
brick_img = brick.Brick((i * 64) + 10, (r * 32 + 60), img_path=constants.BLUE_DIR)
self.brick_sprite.add(brick_img)
if __name__ == "__main__":
pg.init()
screen = constants.SCREEN
states = {"SPLASH": SplashScreen(),
"GAMEPLAY": Gameplay()}
game = Game(screen, states, "SPLASH")
game.run()
pg.quit()
sys.exit()