-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-menu.py
405 lines (347 loc) · 12.7 KB
/
no-menu.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import pygame, random, math
from pygame.locals import *
# import pygame_menu
# Main Menu
# def mainmenu():
# pygame.init()
# surface = pygame.display.set_mode((640, 480))
# pygame.display.set_caption("War Plane")
# Starts the game
# def start_the_game():
# main()
# pass
# Menu Theme
# mytheme = pygame_menu.Theme(
# background_color=(0, 0, 0, 0),
# title_background_color=(0, 0, 0),
# widget_padding=25,
# title_font="fonts/Farenheight.ttf",
# widget_font="fonts/Farenheight.ttf",
# widget_background_color=(0, 0, 0),
# title_font_size=64,
# title_offset=(0, 7.5),
# title_font_antialias=True,
# widget_font_antialias=True,
# widget_margin=(0, 10),
# widget_font_size=32,
# )
# Menu background image
# bg = pygame_menu.baseimage.BaseImage(
# image_path="backgrounds/camo.png",
# drawing_mode=pygame_menu.baseimage.IMAGE_MODE_REPEAT_XY)
# mytheme.background_color = bg
# Menu title and buttons
# menu = pygame_menu.Menu('War Plane', 640, 480, theme=mytheme)
# menu.add.button('Play', start_the_game)
# menu.add.button('Quit',
# pygame_menu.events.EXIT,
# selection_color=(150, 10, 0))
# Main menu music
# pygame.mixer.music.load("sfx/war-is-coming.mp3")
# pygame.mixer.music.play(-1)
# pygame.mixer.music.set_volume(0.2)
# Adds the menu to the screen
# menu.mainloop(surface)
# Sound effects
pygame.mixer.init()
gunfire = pygame.mixer.Sound("sfx/9mm Glock 17.mp3")
gunfire.set_volume(0.3)
# Main game loop
def main():
# Defines values for width and height of the screen
screenWidth = 640
screenHeight = 480
# Clock for the game, used to set the FPS
clock = pygame.time.Clock()
# Player class
class Player(pygame.sprite.Sprite):
width = 64
height = 64
x = (screenWidth - width) / 2
y = (screenHeight - 2 * height)
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.transform.smoothscale(
pygame.image.load('sprites/playerplane.png').convert_alpha(),
(self.width, self.height))
self.mask = pygame.mask.from_surface(self.surf)
self.rect = self.surf.get_rect()
self.rect.x = self.x
self.rect.y = self.y
self.speed = 3
# Player controls
def update(self):
keys = pygame.key.get_pressed()
if keys[K_UP] or keys[K_w]:
self.rect.move_ip(0, -self.speed)
if keys[K_DOWN] or keys[K_s]:
self.rect.move_ip(0, self.speed)
if keys[K_LEFT] or keys[K_a]:
self.rect.move_ip(-self.speed, 0)
if keys[K_RIGHT] or keys[K_d]:
self.rect.move_ip(self.speed, 0)
# Prevents player from moving outside the screen
if self.rect.left < 0:
self.rect.left = 0
if self.rect.bottom > screenHeight:
self.rect.bottom = screenHeight
if self.rect.top < 0:
self.rect.top = 0
if self.rect.right > screenWidth:
self.rect.right = screenWidth
self.speed = 3 + score / 20
# Enemy class
class Enemy(pygame.sprite.Sprite):
width = 64
height = 64
def __init__(self):
super(Enemy, self).__init__()
self.surf = pygame.transform.flip(
pygame.transform.smoothscale(
pygame.image.load('sprites/enemyplane.png').convert_alpha(),
(self.width, self.height)), False, True)
self.mask = pygame.mask.from_surface(self.surf)
self.x = random.randint(self.width, screenWidth - self.width)
self.y = random.randint(0 - screenHeight, 0)
self.rect = self.surf.get_rect(center=(self.x, self.y))
self.speed = 2
def update(self):
# Enemy movement
self.rect.move_ip(0, self.speed)
# Enemy is deleted when it goes past the bottom of the screen
if self.rect.top > screenHeight:
self.kill()
# Speed increases by 0.1 with every point
self.speed = 2 + (score / 10)
# Bullet class
class Bullet(pygame.sprite.Sprite):
def __init__(self):
super(Bullet, self).__init__()
width = 16
height = 16
self.speed = -5
self.surf = pygame.transform.smoothscale(
pygame.image.load('sprites/bullet.png').convert_alpha(),
(width, height))
self.mask = pygame.mask.from_surface(self.surf)
self.rect = self.surf.get_rect()
self.rect.x = player.rect.x + player.rect.width / 2 - self.rect.width / 2
self.rect.y = player.rect.top
def update(self):
# If bullet goes past the top of the screen, it gets deleted
if self.rect.bottom < 0:
self.kill()
# Enemy Bullet subclass of Bullet
class EnemyBullet(Bullet):
def __init__(self):
super(EnemyBullet, self).__init__()
width = 16
height = 16
self.speed = 4
self.surf = pygame.transform.smoothscale(
pygame.image.load('sprites/enemybullet.png').convert_alpha(),
(width, height))
self.mask = pygame.mask.from_surface(self.surf)
self.rect = self.surf.get_rect()
self.rect.x = enemy.rect.x + enemy.rect.width / 2 - enemy.rect.width / 2
self.rect.y = enemy.rect.top
def update(self):
# If bullet goes past the bottom of the screen, it gets deleted
if self.rect.top > screenHeight:
self.kill()
# Speed increases by 0.1 with every point
self.speed = 4 + (score / 10)
# Sets score to 0
score = 0
# Starts Pygame
pygame.init()
# Screen Resolution
screen = pygame.display.set_mode((screenWidth, screenHeight), DOUBLEBUF)
# Game title
pygame.display.set_caption('War Plane')
# Adds sprites to classes and groups
player = Player()
enemy = Enemy()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
enemybullets = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
all_sprites.add(enemy)
enemies.add(enemy)
# Timer that sets when the ADDENEMY event is able to be called so too many enemies won't be created at once
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 1000)
# Timer that sets when the ENEMYBULLETFIRE event is able to be called so too many enemy bullets won't be created at once
ENEMYBULLETFIRE = pygame.USEREVENT + 2
pygame.time.set_timer(ENEMYBULLETFIRE, 5000)
# Background Image and turns the background into tiles to make it able to scroll
bg = pygame.transform.smoothscale(
pygame.image.load("backgrounds/sand.png").convert_alpha(),
(screenWidth, screenHeight))
scroll = 0
tiles = math.ceil(screenHeight / bg.get_height()) + 1
# Pause Menu
# pausetheme = pygame_menu.Theme(
# background_color=(0, 0, 0, 0),
# title_background_color=(0, 0, 0),
# widget_padding=25,
# title_font="fonts/Farenheight.ttf",
# widget_font="fonts/Farenheight.ttf",
# widget_background_color=(0, 0, 0),
# title_font_size=64,
# title_offset=(0, 7.5),
# title_font_antialias=True,
# widget_font_antialias=True,
# widget_margin=(0, 10),
# widget_font_size=32,
# )
# pause_menu = pygame_menu.Menu('Paused', 640, 480, theme=pausetheme)
# pause_menu.add.button("Continue", pygame_menu.events.BACK)
# pause_menu.add.button('Quit',
# pygame_menu.events.EXIT,
# selection_color=(150, 10, 0))
# Define the paused function
# def paused():
# Pause the game
# clock = pygame.time.Clock()
# paused = True
# while paused:
# Tick the clock
# clock.tick(60) # 60 FPS
# Pause music
# pygame.mixer.music.pause()
# Handle events
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# pygame.quit()
# exit()
# elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
# paused = False
# Show the pause menu
# result = pause_menu.mainloop(screen)
# if result == pygame_menu.events.BACK:
# paused = False
# elif result == pygame_menu.events.EXIT:
# pygame.quit()
# exit()
# When the game is running or has ended
running = True
end = False
while running:
# Sets FPS to 60
clock.tick(60)
# Vertical scrolling background
i = 0
while (i < tiles):
screen.blit(bg, (0, -(bg.get_height() * i + scroll)))
i = i + 1
# Scrolling speed that increases by 0.05 with every point
scroll = scroll - (1 + score / 20)
if abs(scroll) > bg.get_height():
scroll = 0
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
# Pause the game and show the pause menu
# paused()
# If Game Over, it will stop creating enemies and bullets
if end == False:
# Plane propeller sound effect during game
pygame.mixer.music.load("sfx/propeller.mp3")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.2)
# Creates new enemies
if event.type == ADDENEMY:
new_enemy = Enemy()
enemies.add(new_enemy)
all_sprites.add(new_enemy)
# Enemy bullet firing
for new_enemy in enemies:
if event.type == ENEMYBULLETFIRE:
enemybullet = EnemyBullet()
enemybullets.add(enemybullet)
all_sprites.add(enemybullet)
enemybullet.rect.x = new_enemy.rect.centerx - enemybullet.rect.width / 2
enemybullet.rect.y = new_enemy.rect.bottom
# Bullet controls
if event.type == pygame.KEYDOWN:
if event.key == K_SPACE:
# Bullet sound
pygame.mixer.Sound.play(gunfire)
# Creates new bullets
new_bullet = Bullet()
bullets.add(new_bullet)
all_sprites.add(new_bullet)
# Bullet movement
for new_bullet in bullets:
new_bullet.rect.move_ip(0, new_bullet.speed)
for enemybullet in enemybullets:
enemybullet.rect.move_ip(0, enemybullet.speed)
# Updates sprites
player.update()
enemies.update()
bullets.update()
enemybullets.update()
# Adds sprites to the game
for entity in all_sprites:
screen.blit(entity.surf, entity.rect)
# Collision detection for the player colliding with an enemy
if pygame.sprite.spritecollideany(player, enemies,
pygame.sprite.collide_mask):
# Kills both the player and enemy and goes to the Game Over screen
player.kill()
enemy.kill()
end = True
# Collision detection for bullets hitting enemies, both the bullet and enemy get deleted
if pygame.sprite.groupcollide(bullets, enemies, True, True,
pygame.sprite.collide_mask):
# Adds 1 point to the score whenever an enemy is killed
score = score + 1
# Collision detection for enemy bullets hitting the player, both sprites get deleted
if pygame.sprite.spritecollideany(player, enemybullets,
pygame.sprite.collide_mask):
player.kill()
enemybullet.kill()
end = True
# Collision detection for player and enemy bullets hitting each other, they both get deleted
if pygame.sprite.groupcollide(bullets, enemybullets, True, True,
pygame.sprite.collide_mask):
new_bullet.kill()
enemybullet.kill()
# Display score in-game which updates every time an enemy is killed
defaultfont = "fonts/AmericanCaptain.otf"
font = pygame.font.Font(defaultfont, 32)
scoretext = font.render("Score: " + str(score), 1, (0, 0, 0))
screen.blit(scoretext, (4, 4))
# Game Over screen which is displayed when the player dies
if end == True:
# Kills all sprites on the screen
for entity in all_sprites:
entity.kill()
# Stops propeller sound
pygame.mixer.music.stop()
# Displays the background image to hide the score text, enemies and stops scrolling
screen.blit(bg, (0, 0))
# Defines the font size for each text
gameoverFont = pygame.font.Font(defaultfont, 64)
scoreFont = pygame.font.Font(defaultfont, 48)
restartFont = pygame.font.Font(defaultfont, 32)
gameover = gameoverFont.render("Game Over", 1, (255, 0, 0))
# Shows the total score
endscore = scoreFont.render("Score: " + str(score), 1, (255, 0, 0))
restart = restartFont.render("Press Enter to Restart", 1, (255, 0, 0))
# Text is displayed to the screen
screen.blit(gameover, (screenWidth / 4, screenHeight / 4))
screen.blit(endscore, (screenWidth / 4, screenHeight / 2.5))
screen.blit(restart, (screenWidth / 4, screenHeight / 1.5))
# Game restarts when the Enter key is pressed
if event.type == pygame.KEYDOWN:
if event.key == K_RETURN:
main()
# Updates the display
pygame.display.update()
# Starts the game
main()