-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgame.py
More file actions
186 lines (141 loc) · 5.09 KB
/
Copy pathgame.py
File metadata and controls
186 lines (141 loc) · 5.09 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
import itertools
import os
import sys
import pygame
from pygame.constants import KEYUP, QUIT, K_SPACE, K_q, K_c
from objects import Ship, Bullet, RockGenerator, GameInfo, GameOverMessage, AudioEffects
def get_remaining_objects(bullets, rocks, bullet_group, rock_group):
collisions = pygame.sprite.groupcollide(bullet_group, rock_group, True, True)
n_collisions = len(collisions)
if n_collisions > 0:
collided_bullets = set([b.uuid for b in collisions.keys()])
bullets = [b for b in bullets if b.uuid not in collided_bullets]
collided_rocks = set([r.uuid for r in itertools.chain(*collisions.values())])
rocks = [r for r in rocks if r.uuid not in collided_rocks]
return bullets, rocks, n_collisions
def get_remaining_rocks(ship, rocks, rock_group):
collisions = pygame.sprite.spritecollide(ship, rock_group, True)
n_collisions = len(collisions)
if n_collisions > 0:
collided_rocks = set([r.uuid for r in collisions])
rocks = [r for r in rocks if r.uuid not in collided_rocks]
return rocks, -n_collisions
def get_params():
width = 500
height = 400
DISPLAYSURF = pygame.display.set_mode((width, height))
DISPLAYSURF.fill((255, 255, 255))
score = 0
lives = 1
return {
'FPS': 30,
'width': width,
'height': height,
'DISPLAYSURF': DISPLAYSURF,
'fps_clock': pygame.time.Clock(),
'score': score,
'lives': lives,
'bullets': [],
'rocks': [],
'bullet_group': pygame.sprite.Group(),
'rock_group': pygame.sprite.Group(),
'ship': Ship((width / 2, height - 50), 0.5),
'rock_generator': RockGenerator(width, height),
'game_info': GameInfo(score, lives),
'audio': AudioEffects()
}
def quit_game():
pygame.quit()
sys.exit()
def start_game(**params):
width, height = params['width'], params['height']
ship = params['ship']
score, lives = params['score'], params['lives']
bullets, rocks = params['bullets'], params['rocks']
bullet_group, rock_group = params['bullet_group'], params['rock_group']
rock_generator = params['rock_generator']
game_info = params['game_info']
DISPLAYSURF = params['DISPLAYSURF']
fps_clock, FPS = params['fps_clock'], params['FPS']
audio = params['audio']
while True:
kwargs = {
'width': width,
'height': height,
'keys': pygame.key.get_pressed(),
'position': ship.get_center(),
'score': score,
'lives': lives
}
for event in pygame.event.get():
if event.type == QUIT:
quit_game()
elif event.type == KEYUP and event.key == K_SPACE:
bullet = Bullet.instance(**kwargs)
bullet_group.add(bullet)
bullets.append(bullet)
audio.play('bullet')
if rock_generator.should_generate():
rock = rock_generator.next()
rocks.append(rock)
rock_group.add(rock)
DISPLAYSURF.fill((255, 255, 255, 0))
game_info.draw(DISPLAYSURF, **kwargs)
ship.draw(DISPLAYSURF, **kwargs)
for bullet in bullets:
bullet.draw(DISPLAYSURF, **kwargs)
for rock in rocks:
rock.draw(DISPLAYSURF, **kwargs)
bullets, rocks, points = get_remaining_objects(bullets, rocks, bullet_group, rock_group)
score += points
for _ in range(points):
audio.play('explosion')
rocks, hits = get_remaining_rocks(ship, rocks, rock_group)
lives += hits
pygame.display.update()
fps_clock.tick(FPS)
if lives <= 0:
break
return score, lives
def show_game_over(**params):
DISPLAYSURF = params['DISPLAYSURF']
fps_clock, FPS = params['fps_clock'], params['FPS']
width, height = params['width'], params['height']
score, lives = params['score'], params['lives']
message = GameOverMessage(width, height, score, lives)
do_continue = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
quit_game()
elif event.type == KEYUP:
if event.key == K_q:
quit_game()
elif event.key == K_c:
print('continue')
do_continue = True
break
if do_continue:
break
DISPLAYSURF.fill((255, 255, 255, 0))
message.draw(DISPLAYSURF)
pygame.display.update()
fps_clock.tick(FPS)
return do_continue
def start():
pygame.init()
pygame.display.set_caption('Space Battle')
pygame.mixer.init()
pygame.mixer.music.load('./audio/game.wav')
pygame.mixer.music.play(loops=-1)
while True:
params = get_params()
params['score'], params['lives'] = start_game(**params)
do_continue = show_game_over(**params)
if not do_continue:
break
pygame.mixer.music.stop()
quit_game()
if __name__ == '__main__':
os.environ['SDL_VIDEO_CENTERED'] = '1'
start()