/
game.py
250 lines (187 loc) · 6.53 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import pygame
from pygame import display, mouse
import random
from config import Config, init_screen
import game_objects
import menu
from game_objects import Images, Texts
# INIT
# Load config
config = Config()
screen = init_screen(config)
clock = pygame.time.Clock()
images = Images(config)
texts = Texts(config)
# CONFIG
SCREEN_SIZE = config.SCREEN_SIZE
ASSET_SIZE = config.ASSET_SIZE
SHIP_LOCALE = config.SHIP_LOCALE
# FUNCTIONS
vector = game_objects.vector # make vector function local
def check_collision(bogeys, missiles):
hits = 0
for bogey in bogeys:
for missile in missiles:
if missile.rect.colliderect(bogey.rect) and missile.image is not images.splash_img:
splash_it(missile, bogey)
hits += 1
return hits
def splash_it(missile, bogey):
missile.splash(images.splash_img) # Change missile image to "images.splash_img"
missile.speed(0) # Stop the missile
missile.show(20) # hide missile for after 20 frames
missile.expire = True # remove missile after it is hidden
bogey.speed(0) # stop bogey
bogey.show(5) # hide missile for after 20 frames
bogey.expire = True # remove missile after it is hidden
def refill_clip(clip):
if clip < 4:
clip += 1
return clip
# SPAWNERS
def spawn_bogey(x, v):
bogey = game_objects.Object(images.bogey_img, x, v=v)
bogey.show(1875)
return bogey
def spawn_missile(vect, v):
missile = game_objects.Object(
image = images.missile_img,
x = SHIP_LOCALE[0],
y = SHIP_LOCALE[1],
v = v,
vect = vect)
missile.rotate(vect)
missile.show(300)
return missile
def random_speed(min, max):
speed = random.randrange(round(min*1000), round(max*1000))
return speed/1000
def play():
# OBJECTS
croshair = game_objects.Object(images.croshair_img)
bogeys = []
missiles = []
clip = 4
score = 0
missed = 0
lives = 3
ship = game_objects.Object(
image=images.ship_img,
x = SHIP_LOCALE[0],
y = SHIP_LOCALE[1],
expire = False
)
missile_speed = 20 * 25 / fps
min_bogey_speed = 5*25/fps
max_bogey_speed = 15*25/fps
show_target = 20 * fps/25
# ship_rect = config.ship_img.get_rect(center = (SHIP_LOCALE))
ship.show(-1)
# EVENTS
SPAWNBOGEY = pygame.USEREVENT
pygame.time.set_timer(SPAWNBOGEY,800) # Start 1500
REFILL = pygame.USEREVENT + 1
pygame.time.set_timer(REFILL,700)
running = True
# GAME LOOP
while running == True:
# GARBAGE COLLECTION
#
# deletes objects that have expired:
# whose time has run out and expiration is enabled
missiles = [missile for missile in missiles if not missile.is_garbage()]
bogeys = [bogey for bogey in bogeys if not bogey.is_garbage()]
# DRAWING
screen.blit( images.background_img, (0,0) )
ship.draw(screen)
croshair.draw(screen)
texts.clip_display(screen, config, clip)
texts.score_display(screen, config, score)
texts.missed_display(screen, config, missed)
# texts.debug_display(screen, config, missiles, bogeys)
# Draw Bogeys
for bogey in bogeys:
bogey.move_y()
bogey.draw(screen)
# Check if Bogey is leaving the screen
if bogey.rect.centery > SCREEN_SIZE[1]:
missed += 1
bogey.show(0)
# Draw Missiles
for missile in missiles:
missile.move_2d()
missile.draw(screen)
# Check collisions
hits = check_collision(bogeys, missiles)
score += hits
# If enough bogeys aren't shot down
# end the game
if missed >= lives:
running = False
# EVENTS
current_events = pygame.event.get()
for event in current_events:
# KEYBOARD
if event.type == pygame.KEYDOWN:
if event.key in (pygame.K_SPACE,
pygame.K_ESCAPE,
pygame.K_RETURN,
pygame.K_KP_ENTER
):
running = False
# APP CLOSE
if event.type == pygame.QUIT:
running = False
# MOUSE
if event.type == pygame.MOUSEBUTTONDOWN:
if clip > 0:
clip -= 1
mouse_x, mouse_y = mouse.get_pos()
vect = vector( (mouse_x,mouse_y), SHIP_LOCALE )
missiles.append( spawn_missile(vect, missile_speed) )
croshair.pos(mouse_x, mouse_y)
croshair.show(show_target)
# TIMED EVENTS
if event.type == SPAWNBOGEY:
bogeys.append( spawn_bogey(
random.randrange(32, SCREEN_SIZE[0]-32),
random_speed(min_bogey_speed, max_bogey_speed)
))
if event.type == REFILL:
clip = refill_clip(clip)
display.update()
clock.tick(fps)
# SHOW POST_GAME
post_game = True
while post_game:
screen.blit( images.background_img, (0,0) )
texts.clip_display(screen, config, clip)
texts.score_display(screen, config, score)
# texts.debug_display(screen, config, missiles, bogeys)
texts.missed_display(screen, config, missed)
gameover_rect = texts.gameover_display(screen, config, score)
current_events = pygame.event.get()
for event in current_events:
# KEYBOARD
if event.type == pygame.KEYDOWN:
post_game = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = mouse.get_pos()
if gameover_rect.collidepoint((mouse_x, mouse_y)):
post_game = False
# APP CLOSE
if event.type == pygame.QUIT:
post_game = False
display.update()
clock.tick(25)
return score
if __name__ == "__main__":
high_score = 0
mode = 1
fps = config.FPS_MODES[0]
while mode > 0:
fps, mode = menu.menu(screen, clock, images, config, fps, high_score)
if mode > 0:
score = play()
if score > high_score:
high_score = score