-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
256 lines (210 loc) · 8.38 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
import sys
import pygame.mixer
from objects import *
# global variables
DEFAULT_ACCEL = 1
DEFAULT_VEL = 5
DEFAULT_BULLET_VEL = 10
DEFAULT_ANG_VEL = 6
MAX_ASTEROID_N = 10
VOL_INCREMENT = 0.005
MAX_VOL = 0.2
CWD = os.getcwd()
# initialize pygame and screen
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Gabsteroids")
# sounds
menu_music = pygame.mixer.Sound(os.path.join(CWD, "sounds", "Asteroids - menu.wav"))
menu_music.set_volume(MAX_VOL)
gameplay_music = pygame.mixer.Sound(os.path.join(CWD, "sounds", "Asteroids - Gameplay.wav"))
gameplay_music.set_volume(0)
gameover_music = pygame.mixer.Sound(os.path.join(CWD, "sounds", "Asteroids - retry.wav"))
gameover_music.set_volume(0)
asteroids_sounds = []
for i in range(1, 6):
asteroids_sounds.append(pygame.mixer.Sound(os.path.join(CWD, "sounds", f"Explosao {i}.wav")))
shooting_sound = pygame.mixer.Sound(os.path.join(CWD, "sounds", "tiro.wav"))
shooting_sound.set_volume(MAX_VOL)
damage_sound = pygame.mixer.Sound(os.path.join(CWD, "sounds", "spawn.wav"))
# main menu loop - calls game loop
def main():
menu_music.play(-1)
gameplay_music.play(-1)
gameover_music.play(-1)
# initialize background
background = pygame.Surface(screen.get_size()).convert()
background.fill((255, 255, 255))
screen.blit(background, (0, 0))
pygame.display.flip()
while True:
text_str = "Press Enter to start a new game or Esc to exit"
text_font = pygame.font.Font(os.path.join(CWD, "fonts", "VT323-Regular.ttf"), 30)
text_surf = text_font.render(text_str, True, (0,0,0))
text_pos = ((screen.get_width() - text_surf.get_width()) / 2, (screen.get_height() - text_surf.get_height()) / 2)
screen.blit(text_surf, text_pos)
pygame.display.flip()
# event handling
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
# keyboard handling
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
gameplay_music.set_volume(0)
return
if keys[pygame.K_RETURN]:
gameloop()
def gameloop():
# initialize background
background = pygame.Surface(screen.get_size()).convert()
background.fill((255, 255, 255))
screen.blit(background, (0, 0))
pygame.display.flip()
# initialize game objects
player = Player()
player_sprite = pygame.sprite.RenderUpdates(player)
player_lives = pygame.sprite.RenderUpdates()
for i in range(player.lives):
player_lives.add(Heart((i * 38, 0)))
asteroids_sprites = pygame.sprite.RenderUpdates(Asteroids())
debris_sprites = pygame.sprite.RenderUpdates()
bullets_sprites = pygame.sprite.RenderUpdates()
scoreboard = Scoreboard()
clock = pygame.time.Clock()
spawn_intervals = 0 # keep track of time to create new asteroids
shoot_delay = 0
music_volume = 0
# game main loop
while True:
clock.tick_busy_loop(60)
time_intervals = clock.get_time()
spawn_intervals += time_intervals
if music_volume < MAX_VOL:
music_volume += VOL_INCREMENT
gameplay_music.set_volume(music_volume)
if player.immune > 0:
player.immune -= time_intervals
# create new asteroids every 2 seconds (if there's less than 10)
if spawn_intervals > 2000 and asteroids_sprites.__len__() < MAX_ASTEROID_N:
asteroids_sprites.add(Asteroids())
spawn_intervals = 0
# event handling
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
# keyboard handling
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
gameplay_music.set_volume(0)
return
if keys[pygame.K_UP]:
player.move(DEFAULT_VEL)
# player.accel(DEFAULT_ACCEL) # realistic movement
else:
player.stop()
if keys[pygame.K_LEFT]:
player.turn(DEFAULT_ANG_VEL)
elif keys[pygame.K_RIGHT]:
player.turn(-DEFAULT_ANG_VEL)
else:
player.stop_turn()
if (keys[pygame.K_c] or keys[pygame.K_KP0]) and shoot_delay <= 0:
bullets_sprites.add(Bullets(player.nose, player.angle, DEFAULT_BULLET_VEL))
shooting_sound.play()
shoot_delay = 500
else:
shoot_delay -= time_intervals
# collision checks
for bullet in bullets_sprites.sprites():
if not bullet.area.contains(bullet.rect):
bullet.kill() # get rid of bullets offscreen
# check bullet-asteroids collisions
for asteroid in asteroids_sprites.sprites():
if pygame.sprite.collide_mask(bullet, asteroid):
asteroid_size = round(asteroid.size, 1)
# divide asteroids into two
if asteroid_size > 0.1:
new_asteroids = asteroid.split()
asteroids_sprites.add(new_asteroids)
else:
debris = asteroid.debris()
debris_sprites.add(debris)
# delete debris sprites if there are more than 50
# to optimize a little bit
while debris_sprites.__len__() > 50:
debris_list = debris_sprites.sprites()
debris_sprites.remove(debris_list[0])
bullet.kill()
asteroid.kill()
asteroids_sounds[int(asteroid_size * 10 - 1)].play()
scoreboard.score += 1
scoreboard.update_score()
# check player-asteroids collisions
if pygame.sprite.spritecollide(player, asteroids_sprites, False, pygame.sprite.collide_mask) \
and player.immune <= 0:
player.lives -= 1
player.immune = 2000
damage_sound.play()
# end game if there are no lives left
if player.lives < 1:
scoreboard.update_highscore()
break
# update everything
dirty_rects = []
screen.blit(background, (0, 0))
player_sprite.update()
dirty_rects += player_sprite.draw(screen)
player_lives.empty()
for i in range(player.lives):
player_lives.add(Heart((i * 38, 0)))
dirty_rects += player_lives.draw(screen)
asteroids_sprites.update()
dirty_rects += asteroids_sprites.draw(screen)
debris_sprites.update()
dirty_rects += debris_sprites.draw(screen)
bullets_sprites.update()
dirty_rects += bullets_sprites.draw(screen)
screen.blit(scoreboard.surface, scoreboard.pos)
pygame.display.update(dirty_rects)
pygame.display.flip()
# game over screen
time_counter = 0
while True:
gameover_music.set_volume(MAX_VOL)
clock.tick(60)
time_counter += clock.get_time()
if time_counter > 1000:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN:
gameover_music.set_volume(0)
pygame.event.get()
return
else:
pygame.event.get()
if music_volume > 0:
music_volume -= VOL_INCREMENT
gameplay_music.set_volume(music_volume)
# GAME OVER
screen.blit(background, (0, 0))
gameover_font = pygame.font.Font(os.path.join(CWD, "fonts", "VT323-Regular.ttf"), 40)
gameover_surf = gameover_font.render("GAME OVER", True, (0, 0, 0))
gameover_pos = (
(background.get_width() - gameover_surf.get_width()) / 2,
(background.get_height() - gameover_surf.get_height()) / 2
)
background.blit(gameover_surf, gameover_pos)
# play again/exit text
text_str = "Press Enter to start a new game or Esc to exit"
text_font = pygame.font.Font(os.path.join(CWD, "fonts", "VT323-Regular.ttf"), 30)
text_surf = text_font.render(text_str, True, (0, 0, 0))
text_pos = (
(screen.get_width() - text_surf.get_width()) / 2,
(screen.get_height() + text_surf.get_height()) / 2
)
background.blit(text_surf, text_pos)
pygame.display.flip()
if __name__ == '__main__':
main()