-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.py
337 lines (295 loc) · 8.07 KB
/
main.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# Contra with Dash
# Pygame Template
import os
import random
import sys
import pygame
from settings import *
from sprites import *
from camera import *
from graphics import *
# Game template class
class Game:
def __init__(self):
# Initialize
size = width, height = 600, 480
if "-f" in sys.argv[1:]:
self.screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
else:
self.screen = screen
pygame.display.set_caption(TITLE)
self.clock = pygame.time.Clock()
self.running = True
self.soldierTimer = SOLDIER_SPAWN_TIMER
self.reinit()
def reinit(self):
self.all_sprites = pygame.sprite.Group()
self.grounds = pygame.sprite.Group()
self.player_sprite = pygame.sprite.Group()
self.bg_sprite = pygame.sprite.Group()
self.bullets = pygame.sprite.Group()
self.snipers = pygame.sprite.Group()
self.soldiers = pygame.sprite.Group()
self.enemy_bullets = pygame.sprite.Group()
self.tanks = pygame.sprite.Group()
self.powerups = pygame.sprite.Group()
self.bosses = pygame.sprite.Group()
self.death_anims = pygame.sprite.Group()
self.health = PLAYER_HEALTH
self.soldierTimer = SOLDIER_SPAWN_TIMER
self.powerupTimer = POWERUP_TIME
self.blinkRetract = BLINK_RETRACT
self.time = 0
def new(self):
self.reinit()
self.run()
def run(self):
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def deathAnim(self,sprite):
d = Death(sprite.rect.left,sprite.rect.top)
self.death_anims.add(d)
self.all_sprites.add(d)
def update(self):
self.time += 1
if not self.bosses:
self.playing = False
self.soldierTimer -= 1
self.powerupTimer -= 1
self.soldierTimer %= SOLDIER_SPAWN_TIMER
if self.soldierTimer == 0:
self.soldierTimer = SOLDIER_SPAWN_TIMER
s = Soldier(random.randint(int(p.pos.x+600),int(p.pos.x+800)),random.randint(int(p.pos.y),int(p.pos.y+300)))
self.soldiers.add(s)
self.all_sprites.add(s)
if self.powerupTimer == 0:
self.powerupTimer = POWERUP_TIME
po = Powerup(random.randint(int(p.pos.x+300),int(p.pos.x+600)),random.randint(0,3))
self.powerups.add(po)
self.all_sprites.add(po)
self.health = p.health
self.blinkRetract = p.blinkRetract
self.all_sprites.update()
h.update_HUD(self)
camera.update(p)
# Check if player fell off screen
if p.rect.top > HEIGHT or p.rect.x < 0:
p.die()
self.deathAnim(p)
self.playing = False
# Check if any enemy dies
h1 = pygame.sprite.groupcollide(g.bullets,g.snipers,True,True)
if h1:
for k in h1:
self.deathAnim(k)
h1 = pygame.sprite.groupcollide(g.bullets,g.soldiers,True,True)
if h1:
for k in h1:
self.deathAnim(k)
h1 = pygame.sprite.groupcollide(g.bullets,g.tanks,True,True)
if h1:
for k in h1:
self.deathAnim(k)
# Check player colliding with any bullet
hits = pygame.sprite.spritecollide(p,g.enemy_bullets,True)
if hits:
p.health -= 1
hit_sound.play()
if p.health == 0:
p.die()
self.deathAnim(p)
self.playing = False
# or enemy
h1 = pygame.sprite.spritecollide(p,g.snipers,False)
if h1:
p.die()
self.deathAnim(p)
self.playing = False
h1 = pygame.sprite.spritecollide(p,g.soldiers,False)
if h1:
p.die()
self.deathAnim(p)
self.playing = False
h1 = pygame.sprite.spritecollide(p,g.tanks,False)
if h1:
p.die()
self.deathAnim(p)
self.playing = False
# Sniper events
for e in self.snipers:
b = e.shoot_towards(p)
if b:
self.enemy_bullets.add(b)
self.all_sprites.add(b)
# Tank Events
for t in self.tanks:
b = t.shoot()
if b:
self.enemy_bullets.add(b)
self.all_sprites.add(b)
# Do not jump up a platform if the full body is not yet above the platform
hits = pygame.sprite.spritecollide(p,g.grounds,False)
if hits and p.collisions and p.vel.y <= 0:
p.collisions = False
# Enable collisions when body out of ground
if not hits and not p.collisions:
p.collisions = True
# Jump on platform only while falling
if p.vel.y > 0 and p.collisions:
if hits:
p.pos.y = hits[0].defaulty
p.stopJumping()
p.vel.y = 0
p.canJump = True
for e in self.soldiers:
hits = pygame.sprite.spritecollide(e,g.grounds,False)
if hits :
e.pos.y = hits[0].defaulty
e.vel.y = 0
# Powerup events
hits = pygame.sprite.spritecollide(p,g.powerups,False)
if hits:
powerup = hits[0]
action = powerup.powerup()
powerup_sound.play()
if action == 0:
p.blinkRetract = 0
self. blinkRetract = 0
elif action == 1:
p.health += 1
self.health += 1
else:
p.drop()
powerup.kill()
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if not p.dead:
if event.key == pygame.K_SPACE:
jump_sound.play()
p.jump()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if not p.dead:
b = p.shoot(pygame.mouse.get_pos())
self.all_sprites.add(b)
self.bullets.add(b)
def draw(self):
# Draw sprites and background on the screen
self.screen.fill(GREEN)
self.grounds.draw(self.screen)
self.bg_sprite.draw(self.screen)
self.player_sprite.draw(self.screen)
self.snipers.draw(self.screen)
self.soldiers.draw(self.screen)
self.enemy_bullets.draw(self.screen)
self.bullets.draw(self.screen)
self.tanks.draw(self.screen)
self.bosses.draw(self.screen)
self.powerups.draw(self.screen)
pygame.draw.rect(self.screen,SEA_BLUE,(0,437,6767,100))
self.death_anims.draw(self.screen)
pygame.display.update()
def draw_text(self, text, size, x, y):
font_name = pygame.font.match_font('Times')
font = pygame.font.Font(font_name, size)
text_surface = font.render(text,True, RED)
text_rect = text_surface.get_rect()
text_rect.center = (x,y)
self.screen.blit(text_surface,text_rect)
pygame.display.update()
def show_start_screen(self):
waiting_for_start = True
while waiting_for_start:
self.screen.blit(ss_background,ss_background.get_rect())
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
waiting_for_start = False
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
waiting_for_start = False
def show_game_over_screen(self):
text_to_display = "Game Over"
if not self.bosses:
text_to_display = "STAGE CLEAR ("+str(int(self.time/FPS))+" s)"
pygame.draw.rect(self.screen,WHITE,(WIDTH/2, HEIGHT/2 - 35, 200,50))
self.draw_text(text_to_display, 21, WIDTH/2 + 100, HEIGHT/2 - 20)
self.draw_text("R to restart, Q to quit", 21,WIDTH/2 + 100, HEIGHT/ 2)
# init game
g = Game()
g.show_start_screen()
g.running = True
while g.running:
pygame.mixer.music.play(loops = -1)
# init player
p = Player(g)
g.reinit()
g.player_sprite.add(p)
g.all_sprites.add(p)
# init level
for ground in LEVEL_1:
gs = Ground(*ground)
g.all_sprites.add(gs)
g.grounds.add(gs)
# init level background based on level
bg = Background(l1_bg)
g.bg_sprite.add(bg)
g.all_sprites.add(bg)
# init snipers
for s in LEVEL_1_SNIPERS:
sn = Sniper(*s)
g.snipers.add(sn)
g.all_sprites.add(sn)
# init soldiers
for sol in LEVEL_1_SOLDIERS:
s = Soldier(*sol)
g.soldiers.add(s)
g.all_sprites.add(s)
# init tanks
for t in LEVEL_1_TANKS:
tank = Tank(*t)
g.tanks.add(tank)
g.all_sprites.add(tank)
for po in LEVEL_1_PUPS:
pup = Powerup(*po)
g.powerups.add(pup)
g.all_sprites.add(pup)
# Boss
for boss in LEVEL_1_BOSSES:
b = Tank(*boss)
g.bosses.add(b)
g.tanks.add(b)
g.all_sprites.add(b)
# HUD
h = HUD()
g.player_sprite.add(h)
g.all_sprites.add(h)
# Start a new game
g.run()
g.show_game_over_screen()
waiting_for_quit_or_restart = True
while waiting_for_quit_or_restart:
for event in pygame.event.get():
if event.type == pygame.QUIT:
waiting_for_quit_or_restart = False
g.running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
waiting_for_quit_or_restart = False
g.running = False
if event.key == pygame.K_r:
waiting_for_quit_or_restart = False
pygame.quit()