-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
471 lines (400 loc) · 15.2 KB
/
main.py
File metadata and controls
471 lines (400 loc) · 15.2 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import pygame
import random
import sys
from pygame import mixer
from pygame import *
import pickle
# Defining the globla variables
FPS = 120
FPSCLOCK = pygame.time.Clock()
high_score = 0
enimy_speed = 2
score_value = 0
num_of_enimys_to_load = 10
num_of_enimys = 3
screen = pygame.display.set_mode((800, 600))
def manue():
# initialze pygame
pygame.init()
global high_score
# create the screen
# screen = pygame.display.set_mode((800, 600))
background = pygame.image.load("data/Sprites/background.png")
# title and icon
pygame.display.set_caption("Space Invaders.......🛸🛸")
icon = pygame.image.load("data/Sprites/ufo.png")
pygame.display.set_icon(icon)
# player
playerimg = pygame.image.load("data/Sprites/player.png")
playerX = 370
playerY = 480
color = (255, 255, 255)
# dark shade of the button
color_dark = (255, 136, 44)
data = open("data/score", "rb")
high_score = pickle.load(data)
font = pygame.font.Font("freesansbold.ttf", 32)
font2 = pygame.font.Font("freesansbold.ttf", 24)
data.close()
text = font.render("START", True, color)
text1 = font.render("HELP", True, color)
text2 = font.render("EXIT", True, color)
text3 = font2.render("MENUE", True, (255, 200, 1))
textX = 275
texty = 240
def show(x, y):
score = font.render("HIGH SCORE:" + str(high_score), True, (255, 255, 255))
screen.blit(score, (x, y))
def help():
pygame.init()
screen = pygame.display.set_mode((800, 600))
background = pygame.image.load("data/Sprites/help.jpg")
button = pygame.image.load("data/Sprites/back.png")
screen.blit(background, (0, 0))
screen.blit(button, (10, 5))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and (event.key == K_SPACE):
# print("True")
return
if event.type == pygame.MOUSEBUTTONDOWN:
mouse = pygame.mouse.get_pos()
if 10 <= mouse[0] <= 42 and 5 <= mouse[1] <= 37:
# print("True")
return
FPSCLOCK.tick(FPS)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and (event.key == K_SPACE):
return
if event.type == pygame.MOUSEBUTTONDOWN:
if 330 <= mouse[0] <= 470 and 400 <= mouse[1] <= 440:
return
elif 330 <= mouse[0] <= 470 and 480 <= mouse[1] <= 520:
pygame.quit()
sys.exit()
elif 330 <= mouse[0] <= 470 and 440 <= mouse[1] <= 480:
help()
else:
mouse = pygame.mouse.get_pos()
screen.blit(background, (0, 0))
if 330 <= mouse[0] <= 470 and 400 <= mouse[1] <= 440:
pygame.draw.rect(screen, color_dark, [275, 400, 250, 40])
elif 330 <= mouse[0] <= 470 and 440 <= mouse[1] <= 480:
pygame.draw.rect(screen, color_dark, [275, 440, 250, 40])
elif 330 <= mouse[0] <= 470 and 480 <= mouse[1] <= 520:
pygame.draw.rect(
screen, color_dark, [275, 480, 250, 40], 0, 0, 0, 0, 11, 11
) # surface, color, rect, width=0, border_radius=0, border_top_left_radius=-1, border_top_right_radius=-1, border_bottom_left_radius=-1, border_bottom_right_radius=-1
pygame.draw.rect(
screen, color_dark, [275, 400, 250, 120], 2, 0, 0, 0, 11, 11
)
pygame.draw.rect(
screen, color_dark, [275, 372, 250, 30], 2, 0, 11, 11, 0, 0
)
screen.blit(text, (350, 406))
screen.blit(text1, (358, 446))
screen.blit(text2, (358, 486))
screen.blit(text3, (358, 377))
FPSCLOCK.tick(FPS)
show(textX, texty)
pygame.display.update()
def gameover():
global level_up
global score_value
global num_of_enimys
global exitgame
if exitgame:
return
over_font = pygame.font.Font("freesansbold.ttf", 64)
background = pygame.image.load("data/Sprites/background.jpg")
def game_over_text():
over_text = over_font.render("GAME OVER", True, (255, 255, 255))
screen.blit(over_text, (200, 250))
for i in range(0, 150):
if i == 150:
return
else:
level_up = False
score_value = 0
num_of_enimys = 3
screen.blit(background, (0, 0))
FPSCLOCK.tick(70)
game_over_text()
pygame.display.update()
# -------- main game function
def main_game():
import math
import time
# initialze pygame
global high_score
global level_up
global enimy_speed
global score_value
global num_of_enimys
global exitgame
pose = False
level_up = True
frame_delay_exp = 0
exp_frame = -1
explotion = False
start_animation = False
pygame.init()
pausebtn = pygame.image.load("data/Sprites/pause.png")
background = pygame.image.load("data/Sprites/background.jpg")
# title and icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("data/Sprites/ufo.png")
pygame.display.set_icon(icon)
# player
playerimg = pygame.image.load("data/Sprites/player.png")
playerX = 370
playerY = 480
playerX_change = 0
# explotion
images_exp = []
for i in range(1, 6):
img = pygame.image.load(f"data/Sprites/images/img{i}.png")
img = pygame.transform.scale(img, (100, 100))
images_exp.append(img)
# Enimy
enimyimg = []
enimyX = []
enimyY = []
enimy_collision = []
enimyX_change = []
enimyY_change = []
error_frame = []
for i in range(num_of_enimys_to_load):
enimyimg.append(pygame.image.load("data/Sprites/enimy.png"))
enimyX.append(random.randint(0, 800))
enimyY.append(30)
enimyX_change.append(enimy_speed)
enimyY_change.append(30)
enimy_collision.append(1)
error_frame.append(1)
# bullet
bulletimg = pygame.image.load("data/Sprites/bullet.png")
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 5
bullet_state = "ready"
# Score
font = pygame.font.Font("freesansbold.ttf", 32)
textX = 10
texty = 10
# background sound
mixer.music.load("data/Sounds/background.wav")
mixer.music.play(-1) # -1 will run the music in loop
# game over
def show(x, y):
score = font.render("score :" + str(score_value), True, (255, 255, 255))
screen.blit(score, (x, y))
def player(x, y):
screen.blit(playerimg, (x, y))
def enimy(x, y, i):
screen.blit(enimyimg[i], (x, y))
def fire(x, y):
global bullet_state
screen.blit(bulletimg, (x + 16, y + 10))
# colition
def iscollition(enimyX, enimyY, bulletX, bulletY):
distance = math.sqrt(
math.pow(enimyX - bulletX, 2) + (math.sqrt(math.pow(enimyY - bulletY, 2)))
)
if distance < 7:
return True
level_up_font = pygame.font.Font("freesansbold.ttf", 64)
def level_up_text():
up_text = level_up_font.render("LEVEL UP", True, (255, 255, 255))
screen.blit(up_text, (200, 250))
# pause function
def pause():
global exitgame
color_dark = (255, 136, 44)
pygame.init()
font = pygame.font.Font("freesansbold.ttf", 32)
text = font.render("RESUME", True, (255, 255, 255))
text1 = font.render("EXIT", True, (255, 255, 255))
background = pygame.image.load("data/Sprites/background.jpg")
screen.blit(background, (0, 0))
while True:
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and (event.key == K_SPACE):
# print("True")
return
if event.type == pygame.MOUSEBUTTONDOWN:
if 300 <= mouse[0] <= 500 and 250 <= mouse[1] <= 300:
return
if 300 <= mouse[0] <= 500 and 320 <= mouse[1] <= 370:
exitgame = True
return exitgame
else:
screen.blit(background, (0, 0))
if 300 <= mouse[0] <= 500 and 250 <= mouse[1] <= 300:
pygame.draw.rect(screen, color_dark, [300, 250, 200, 50], 0, 11)
if 300 <= mouse[0] <= 500 and 320 <= mouse[1] <= 370:
pygame.draw.rect(screen, color_dark, [300, 320, 200, 50], 0, 11)
# print(mouse)
pygame.draw.rect(screen, (255, 255, 255), [300, 250, 200, 50], 2, 11)
pygame.draw.rect(screen, (255, 255, 255), [300, 320, 200, 50], 2, 11)
screen.blit(text, (330, 260))
screen.blit(text1, (360, 330))
FPSCLOCK.tick(FPS)
pygame.display.update()
# game loop
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# if ky is pressed check whether it is right or light
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -4
if event.key == pygame.K_RIGHT:
playerX_change = 4
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_state = "fire"
bulletsound = mixer.Sound("data/Sounds/laser.wav")
bulletsound.play()
bulletX = playerX
fire(bulletX, playerY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
if event.type == pygame.MOUSEBUTTONDOWN:
mouse = pygame.mouse.get_pos()
if 384 <= mouse[0] <= 416 and 5 <= mouse[1] <= 37:
pygame.mixer.music.pause()
exitgame = pause()
pygame.mixer.music.unpause()
if exitgame:
pygame.mixer.music.pause()
return
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
for i in range(num_of_enimys):
collision = iscollition(enimyX[i], enimyY[i], bulletX, bulletY)
# ----------bullet collision----------
if collision:
collisionsound = mixer.Sound("data/Sounds/explosion.wav")
collisionsound.play()
bulletY = 480
bullet_state = "ready"
score_value += 1
expX = enimyX[i]
expY = enimyY[i]
start_animation = True
enimyX[i] = random.randint(0, 800)
enimyY[i] = random.randint(0, 50)
# enimyX_change[i]=enimyX_change*-1
enimyX[i] += enimyX_change[i]
# ------------------colition with wall-------------------
error_frame[i] += 1
enimy_collision[i] = 1
if error_frame[i] < 15:
enimy_collision[i] = 0
if error_frame[i] == 15:
error_frame[i] = 1
if enimyX[i] < 0 and enimy_collision[i] == 1:
enimyX_change[i] = enimy_speed
enimyY[i] += enimyY_change[i]
elif enimyX[i] > 736 and enimy_collision[i] == 1:
enimyX_change[i] = -enimy_speed
enimyY[i] += enimyY_change[i]
# game over
if enimyY[i] > 430:
if score_value > high_score:
high_score = score_value
# for j in range(num_of_enimys):
# enimyY[j]=2000
enimy_speed = 2
data = open("data/score", "wb")
pickle.dump(high_score, data)
data.close()
exitgame = False
pygame.mixer.music.pause()
return exitgame
enimy(enimyX[i], enimyY[i], i)
# bulet
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state == "fire":
fire(bulletX, bulletY)
bulletY -= bulletY_change
# explition animation
if start_animation:
frame_delay_exp += 1
try:
if frame_delay_exp % 12 == 0:
exp_frame += 1
explotion = True
exp_img = images_exp[exp_frame]
except:
exp_frame = 0
frame_delay_exp = 0
explotion = False
start_animation = False
if explotion:
screen.blit(exp_img, (expX, expY))
# level up ---------
if score_value == 10:
num_of_enimys = 5
enimy_speed = 2
if level_up:
level_up_text()
pose = True
level_up = False
if score_value == 11:
level_up = True
if score_value == 20 and level_up:
level_up = True
num_of_enimys = 7
enimy_speed = 3
level_up_text()
pose = True
level_up = False
if score_value == 21:
level_up = True
if score_value == 30 and level_up:
level_up = True
num_of_enimys = 10
enimy_speed = 4
pose = False
level_up_text()
pose = True
level_up = False
show(textX, texty)
screen.blit(pausebtn, (384, 5))
player(playerX, playerY)
FPSCLOCK.tick(FPS)
pygame.display.update()
if pose:
time.sleep(1)
pose = False
print("ENJOY THE GAME 😀")
while True:
manue()
main_game()
gameover()