-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
142 lines (106 loc) · 3.59 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
import pygame
import random
pygame.init()
clock = pygame.time.Clock()
size = width, height = 800, 500
# font of current score
my_font = pygame.font.SysFont('Helvetica', 25)
my_screen = pygame.display.set_mode(size)
pygame.display.set_caption("Boooooooom !!!!")
# defining game variables
move_speed = 3
score = 0
last_strike = pygame.time.get_ticks()
alien_interval = 600
# load images
jet = pygame.image.load('res/jet.png')
alien = pygame.image.load('res/alien.png')
bullet = pygame.image.load('res/bullet.png')
bg = pygame.image.load('res/bg.png')
# A function for drawing score on the my_screen
def draw_text(text, font, text_col, x, y):
img = font.render(text, True, text_col)
my_screen.blit(img, (x, y))
class Jet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = jet
self.rect = self.image.get_rect()
def update(self):
self.rect.center = [35, pygame.mouse.get_pos()[1]]
pass
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = bullet
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self):
self.rect.x += 12 # bullet speed
if self.rect.x > width + 100:
self.kill()
pass
class Alien(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = alien
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.step_y = random.randrange(-3, 3)
self.step_x = random.randrange(3, 7)
def update(self):
self.rect.x -= self.step_x
if self.rect.bottomleft[1] > height or self.rect.topleft[1] < 0:
self.step_y = - self.step_y
self.rect.y -= self.step_y
pass
# Sprite groups
alien_group = pygame.sprite.Group()
bullet_group = pygame.sprite.Group()
jet_group = pygame.sprite.Group()
jet_sprite = Jet(100, 100)
jet_group.add(jet_sprite)
# Game loop goes here
running = True
while running:
clock.tick(100) # This game is 100 fps
my_screen.blit(bg, (0, 0))
alien_group.draw(my_screen)
jet_group.draw(my_screen)
bullet_group.draw(my_screen)
jet_group.update()
bullet_group.update()
alien_group.update()
now = pygame.time.get_ticks()
if now - last_strike > alien_interval:
alien_group.add(Alien(width, random.randrange(50, height - 50)))
last_strike = now
# collisions
for i in alien_group:
for j in bullet_group:
if pygame.sprite.collide_rect(i, j):
i.kill()
j.kill()
score += 1
pygame.mixer.music.load('res/explosion.mp3')
pygame.mixer.music.play()
for i in alien_group:
if i.rect.x < -10:
running = False
print('--------------------- GAME OVER --------------------------')
print(f"Your score was : {score}")
# displaying score
your_score = my_font.render(f"Score : {score}", False, (20, 20, 20))
my_screen.blit(your_score, (5, height-30))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
bullet_group.add(Bullet(jet_sprite.rect.center[0] + 25, jet_sprite.rect.center[1] - 5))
pygame.mixer.music.load('res/fire.mp3')
pygame.mixer.music.play()
print('shuttle is at x : ', jet_sprite.rect.x)
pygame.display.update()
pygame.quit()