-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.pyw
More file actions
511 lines (399 loc) · 14.5 KB
/
Copy pathmain.pyw
File metadata and controls
511 lines (399 loc) · 14.5 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# importing modules
import pygame
import random
import sys
# importing sub-modules
from random import randint
# initializing modules
pygame.init()
pygame.mixer.init()
pygame.font.init()
# game settings
# game constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SCREEN_TITLE = "ProjectX"
FPS = 60
PLAYER_SPEED = 10
BULLET_SPEED = 30
ROCKET_SPEED = 10
METEOR_SPEED = 5
EARTH_HEALTH = 3
# rgb color tuples
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
SILVER = (150, 150, 150)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
ALPHA = GREEN
# game variables
# settings
__version__ = "v1.0"
running = True
switch_screen = True
game_over_trigger = True
alpha_value = 128
# decoration
sun_pos_x = 200
moon_pos_x = 600
animation_counter = 0
score = 0
# weaponary variables
# bullets
bullet_ready = True
bullet_amount = 30
bullet_amount_maximum = bullet_amount
# rockets
rocket_ready = True
rocket_amount = 3
# setting up screen
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
TITLE = pygame.display.set_caption(f"{SCREEN_TITLE} {__version__}")
MOUSE_VISIBILITY = pygame.mouse.set_visible(False)
# game classes
# class player
class Player(pygame.sprite.Sprite):
def __init__(self, pos_x=0, pos_y=0):
super().__init__()
self.image = pygame.image.load("static/player.png").convert_alpha()
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect(center = [pos_x, pos_y])
def update(self):
self.rect.x = pygame.mouse.get_pos()[0] - 120
self.rect.y = pygame.mouse.get_pos()[1] - 25
# class bullet
class Bullet(pygame.sprite.Sprite):
def __init__(self, pos_x=0, pos_y=0):
super().__init__()
self.image = pygame.image.load("static/bullet.png").convert_alpha()
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect(center = [pos_x + 80, pos_y])
def update(self):
self.rect.x += BULLET_SPEED
# destroying bullet mechanism
if self.rect.x >= SCREEN_WIDTH:
self.kill()
# class rocket
class Rocket(pygame.sprite.Sprite):
def __init__(self, pos_x=0, pos_y=0):
super().__init__()
self.image = pygame.image.load("static/rocket.png").convert_alpha()
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect(center = [pos_x + 80, pos_y])
def update(self):
self.rect.x += ROCKET_SPEED
# destroying bullet mechanism
if self.rect.x >= SCREEN_WIDTH:
self.kill()
# in gaming mob is enemy
# class mob
class Meteor(pygame.sprite.Sprite):
def __init__(self, pos_x=SCREEN_WIDTH, pos_y=random.randint(30, SCREEN_HEIGHT - 30)):
super().__init__()
self.image = pygame.image.load("static/meteor.png").convert_alpha()
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect(center = [pos_x, pos_y])
def update(self):
# mob movement direction
self.rect.x -= METEOR_SPEED
# delete mob when is out of screen
if self.rect.x <= -50:
self.kill()
class Hudge_Meteor(pygame.sprite.Sprite):
def __init__(self, pos_x=(SCREEN_WIDTH + SCREEN_WIDTH), pos_y=random.randint(30, SCREEN_HEIGHT - 30)):
super().__init__()
self.image = pygame.image.load("static/hudge_meteor.png").convert_alpha()
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect(center = [pos_x, pos_y])
def update(self):
# mob movement direction
self.rect.x -= METEOR_SPEED - 3
# delete mob when is out of screen
if self.rect.x <= -100:
self.kill()
class Small_Meteor(pygame.sprite.Sprite):
def __init__(self, pos_x=(SCREEN_WIDTH + SCREEN_WIDTH), pos_y=random.randint(30, SCREEN_HEIGHT - 30)):
super().__init__()
self.image = pygame.image.load("static/small_meteor.png").convert_alpha()
self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect(center = [pos_x, pos_y])
def update(self):
# mob movement direction
self.rect.x -= METEOR_SPEED + 3
# delete mob when is out of screen
if self.rect.x <= -100:
self.kill()
# game functions
# exit game function
def exit():
pygame.quit()
sys.exit()
running = False
# indicators
# health indicator
def earth_health_indicator():
# earth thumbnail
earth_thumbnail = pygame.image.load("static/earth_thumbnail.png").convert_alpha()
earth_thumbnail.set_colorkey(ALPHA)
# 100% health
pygame.draw.rect(SCREEN, SILVER, pygame.Rect(65, 15, 100, 15))
pygame.draw.rect(SCREEN, GREEN, pygame.Rect(65, 15, (EARTH_HEALTH * 33), 15))
pygame.draw.rect(SCREEN, WHITE, pygame.Rect(65, 15, 100, 15), 3)
# 75% health
if EARTH_HEALTH <= 2:
pygame.draw.rect(SCREEN, YELLOW, pygame.Rect(65, 15, (EARTH_HEALTH * 33), 15))
pygame.draw.rect(SCREEN, WHITE, pygame.Rect(65, 15, 100, 15), 3)
# 50% health
if EARTH_HEALTH <= 1:
pygame.draw.rect(SCREEN, RED, pygame.Rect(65, 15, (EARTH_HEALTH * 33), 15))
pygame.draw.rect(SCREEN, WHITE, pygame.Rect(65, 15, 100, 15), 3)
SCREEN.blit(earth_thumbnail, (150, 11))
def bullet_overheat():
# 100% bullets
pygame.draw.rect(SCREEN, SILVER, pygame.Rect(210, 15, 480, 15))
pygame.draw.rect(SCREEN, GREEN, pygame.Rect(210, 15, (bullet_amount) * 16, 15))
pygame.draw.rect(SCREEN, WHITE, pygame.Rect(210, 15, 480, 15), 3)
# text
my_font = pygame.font.SysFont("Comic Sans MS", 25)
text_bullets = my_font.render("BULLETS", False, RED)
SCREEN.blit(text_bullets, (410, 25))
my_font = pygame.font.SysFont("Comic Sans MS", 25)
text_bullets_0 = my_font.render("0", False, RED)
text_bullets_30 = my_font.render("30", False, GREEN)
SCREEN.blit(text_bullets_0, (190, 4))
SCREEN.blit(text_bullets_30, (692, 4))
# rocket indicator
def rockets_indicator():
# rocket thumbnail
if rocket_amount == 3:
rocket_thumbnail1 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail1.set_colorkey(ALPHA)
rocket_thumbnail2 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail2.set_colorkey(ALPHA)
rocket_thumbnail3 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail3.set_colorkey(ALPHA)
SCREEN.blit(rocket_thumbnail1, (730, 10))
SCREEN.blit(rocket_thumbnail2, (750, 10))
SCREEN.blit(rocket_thumbnail3, (770, 10))
elif rocket_amount == 2:
rocket_thumbnail1 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail1.set_colorkey(ALPHA)
rocket_thumbnail2 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail2.set_colorkey(ALPHA)
rocket_thumbnail3 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail3.set_colorkey(ALPHA)
rocket_thumbnail3.set_alpha(alpha_value)
SCREEN.blit(rocket_thumbnail1, (730, 10))
SCREEN.blit(rocket_thumbnail2, (750, 10))
SCREEN.blit(rocket_thumbnail3, (770, 10))
elif rocket_amount == 1:
rocket_thumbnail1 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail1.set_colorkey(ALPHA)
rocket_thumbnail2 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail2.set_colorkey(ALPHA)
rocket_thumbnail2.set_alpha(alpha_value)
rocket_thumbnail3 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail3.set_colorkey(ALPHA)
rocket_thumbnail3.set_alpha(alpha_value)
SCREEN.blit(rocket_thumbnail1, (730, 10))
SCREEN.blit(rocket_thumbnail2, (750, 10))
SCREEN.blit(rocket_thumbnail3, (770, 10))
else:
rocket_thumbnail1 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail1.set_colorkey(ALPHA)
rocket_thumbnail1.set_alpha(alpha_value)
rocket_thumbnail2 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail2.set_colorkey(ALPHA)
rocket_thumbnail2.set_alpha(alpha_value)
rocket_thumbnail3 = pygame.image.load("static/rocket_thumbnail.png").convert_alpha()
rocket_thumbnail3.set_colorkey(ALPHA)
rocket_thumbnail3.set_alpha(alpha_value)
SCREEN.blit(rocket_thumbnail1, (730, 10))
SCREEN.blit(rocket_thumbnail2, (750, 10))
SCREEN.blit(rocket_thumbnail3, (770, 10))
# creating sprite groups
# player sprite group
player_group = pygame.sprite.Group()
player = Player()
player_group.add(player)
bullet_group = pygame.sprite.Group()
bullet = Bullet(pos_x=player.rect.x, pos_y=player.rect.y)
bullet_group.add(bullet)
rocket_group = pygame.sprite.Group()
rocket = Bullet(pos_x=player.rect.x, pos_y=player.rect.y)
rocket_group.add(rocket)
meteor_group = pygame.sprite.Group()
meteor = Meteor()
meteor_group.add(meteor)
hudge_meteor_group = pygame.sprite.Group()
hudge_meteor = Hudge_Meteor()
hudge_meteor_group.add(hudge_meteor)
small_meteor_group = pygame.sprite.Group()
small_meteor = Small_Meteor()
small_meteor_group.add(small_meteor)
# loading decoration images
# earth image
earth = pygame.image.load("static/earth.png").convert_alpha()
earth.set_colorkey(ALPHA)
# sun image
sun = pygame.image.load("static/sun.png").convert_alpha()
sun.set_colorkey(ALPHA)
# moon image
moon = pygame.image.load("static/moon.png").convert_alpha()
moon.set_colorkey(ALPHA)
# main game loop
while running:
# decoration movement
sun_pos_x += 0.5
moon_pos_x -= 0.5
# decoration repositioning
if sun_pos_x >= SCREEN_WIDTH:
sun_pos_x = (SCREEN_WIDTH - SCREEN_WIDTH - 500)
if moon_pos_x <= (SCREEN_WIDTH - SCREEN_WIDTH - 500):
moon_pos_x = SCREEN_WIDTH
# if key pressed statements
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# create new mob when is out of the screen
if meteor.rect.x <= -50:
meteor = Meteor(pos_y=random.randint(0, SCREEN_HEIGHT))
meteor_group.add(meteor)
# if meteor (mob) hit earth 3x GAME OVER
EARTH_HEALTH -= 1
if EARTH_HEALTH < 0:
bullet_ready = False
rocket_ready = False
game_over_trigger = False
player.kill()
if hudge_meteor.rect.x <= -50:
hudge_meteor = Small_Meteor(pos_y=random.randint(0, SCREEN_HEIGHT * 12))
hudge_meteor_group.add(hudge_meteor)
# if meteor (mob) hit earth 3x GAME OVER
EARTH_HEALTH -= 3
if EARTH_HEALTH < 0:
bullet_ready = False
rocket_ready = False
game_over_trigger = False
player.kill()
if small_meteor.rect.x <= -50:
small_meteor = Small_Meteor(pos_y=random.randint(0, SCREEN_HEIGHT))
small_meteor_group.add(small_meteor)
# if meteor (mob) hit earth 3x GAME OVER
EARTH_HEALTH -= 1
if EARTH_HEALTH < 0:
bullet_ready = False
rocket_ready = False
game_over_trigger = False
player.kill()
# keyboard imput
keys = pygame.key.get_pressed()
# fullscreen and window mode switcher
if keys[pygame.K_f] and switch_screen == True:
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN)
switch_screen = False
elif keys[pygame.K_f] and switch_screen == False:
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
switch_screen = True
# triggering player shooting
# on left mouse click fire bullets
mouse_click = pygame.mouse.get_pressed()
# condition for fireing bullets
if mouse_click[0] and bullet_amount > 0 and bullet_ready == True:
bullet_amount -= 1
bullet = Bullet(pos_x=(player.rect.x), pos_y=(player.rect.y + 25))
bullet_group.add(bullet)
# bullet reloading mechanism
elif bullet_amount <= bullet_amount:
bullet_amount += 0.1
if bullet_amount >= bullet_amount_maximum:
bullet_amount = bullet_amount_maximum
# on right mouse click fire rockets
if mouse_click[2] and rocket_amount > 0 and rocket_ready == True:
rocket_amount -= 1
# drawing rockets on right mouse click (fire)
rocket = Rocket(pos_x=(player.rect.x), pos_y=(player.rect.y + 25))
rocket_group.add(rocket)
# checking for collisions
# player hit mob
if pygame.sprite.groupcollide(player_group, meteor_group, True, False, pygame.sprite.collide_rect_ratio(.85)):
# if mob hit player bullets and rockets not fireing
bullet_ready = False
rocket_ready = False
# game over triger
game_over_trigger = False
if pygame.sprite.groupcollide(player_group, hudge_meteor_group, True, False, pygame.sprite.collide_rect_ratio(.85)):
# if mob hit player bullets and rockets not fireing
bullet_ready = False
rocket_ready = False
# game over triger
game_over_trigger = False
if pygame.sprite.groupcollide(player_group, small_meteor_group, True, False, pygame.sprite.collide_rect_ratio(.85)):
# if mob hit player bullets and rockets not fireing
bullet_ready = False
rocket_ready = False
# game over triger
game_over_trigger = False
# bullet hit metheor
if pygame.sprite.groupcollide(bullet_group, meteor_group, True, True, pygame.sprite.collide_rect_ratio(1)):
meteor = Meteor(pos_y=random.randint(30, SCREEN_HEIGHT - 30))
meteor_group.add(meteor)
score += 5
# rocket hit metheor
if pygame.sprite.groupcollide(rocket_group, meteor_group, False, True, pygame.sprite.collide_rect_ratio(1)):
meteor = Meteor(pos_y=random.randint(30, SCREEN_HEIGHT - 30))
meteor_group.add(meteor)
score += 10
# bullet hit hudge meteor
if pygame.sprite.groupcollide(bullet_group, hudge_meteor_group, True, False, pygame.sprite.collide_rect_ratio(1)):
pass
# rocket hit hudge meteor
if pygame.sprite.groupcollide(rocket_group, hudge_meteor_group, True, True, pygame.sprite.collide_rect_ratio(1)):
hudge_meteor_group = pygame.sprite.Group()
hudge_meteor = Hudge_Meteor(pos_x=random.randint(4500, 5500))
hudge_meteor_group.add(hudge_meteor)
score += 20
# bullet hit small meteor
if pygame.sprite.groupcollide(bullet_group, small_meteor_group, True, True, pygame.sprite.collide_rect_ratio(1)):
pass
# rocket hit small meteor
if pygame.sprite.groupcollide(rocket_group, small_meteor_group, True, True, pygame.sprite.collide_rect_ratio(1)):
small_meteor_group = pygame.sprite.Group()
small_meteor = Small_Meteor(pos_x=random.randint(9500, 10500))
small_meteor_group.add(small_meteor)
score += 30
# drawing sprites on the screen
SCREEN.fill(BLACK)
SCREEN.blit(earth, (0, 0))
SCREEN.blit(sun, (sun_pos_x, 100))
SCREEN.blit(moon, (moon_pos_x, 500))
bullet_group.update()
bullet_group.draw(SCREEN)
rocket_group.update()
rocket_group.draw(SCREEN)
player_group.update()
player_group.draw(SCREEN)
meteor_group.update()
meteor_group.draw(SCREEN)
hudge_meteor_group.update()
hudge_meteor_group.draw(SCREEN)
small_meteor_group.update()
small_meteor_group.draw(SCREEN)
earth_health_indicator()
bullet_overheat()
rockets_indicator()
# condition for displaying game over text over the screen
if game_over_trigger == False:
# game over text
my_font = pygame.font.SysFont("Comic Sans MS", 90)
game_over = my_font.render("GAME OVER!", False, RED)
SCREEN.blit(game_over, ((SCREEN_WIDTH // 6), (SCREEN_HEIGHT // 2.5)))
# game over score
my_font = pygame.font.SysFont("Comic Sans MS", 30)
game_over_score = my_font.render(f"SCORE: {score}", False, GREEN)
SCREEN.blit(game_over_score, ((SCREEN_WIDTH // 2.4), (SCREEN_HEIGHT // 1.75)))
# fps counter
pygame.display.flip()
pygame.time.Clock().tick(FPS)