-
Notifications
You must be signed in to change notification settings - Fork 0
/
shooter.py
246 lines (195 loc) · 8.22 KB
/
shooter.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
from constants import *
class Tank(pygame.sprite.Sprite):
def __init__(self, image):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = image.get_rect()
self.x, self.y = WINDOW_WIDTH // 2, WINDOW_HEIGHT - WINDOW_MARGIN - DASHBOARD_HEIGHT - WINDOW_PADDING
def fire(self, bullets):
if len(bullets) >= MAX_BULLETS_ON_SCREEN:
return
bullet = Bullet(BULLET_TANK, UP_DIRECTION, BULLET_SPEED)
bullet.x, bullet.y = self.x + int(self.rect.width * .7), self.y
bullets.append(bullet)
def position(self):
return pygame.Rect(self.x, self.y, self.rect.width, self.rect.height)
class Alien:
def __init__(self, image):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = image.get_rect()
self.dir = 1
self.x, self.y = WINDOW_WIDTH // 2, WINDOW_HEIGHT - WINDOW_MARGIN - DASHBOARD_HEIGHT - WINDOW_PADDING
def fire(self, bullets):
bullet = Bullet(BULLET, DOWN_DIRECTION, BULLET_SPEED)
bullet.x, bullet.y = self.x + int(self.rect.width * (random.randint(1, 9) / 10)), self.y + int(
self.rect.height * .9)
bullets.append(bullet)
def move(self):
increment = self.rect.height
if self.x > WINDOW_WIDTH - 2 * (WINDOW_MARGIN + WINDOW_PADDING) - self.rect.width // 2:
self.dir = -1
self.y += increment
if self.x < WINDOW_MARGIN + WINDOW_PADDING:
self.dir = 1
self.y += increment
self.x += self.dir * ALIEN_SPEED
def position(self):
return pygame.Rect(self.x, self.y, self.rect.width, self.rect.height)
# differences: image, direction
class Bullet(pygame.sprite.Sprite):
def __init__(self, image, direction, speed):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = image.get_rect()
self.x, self.y = None, None
self.dir = direction
self.speed = speed
def move(self):
if self.y > WINDOW_HEIGHT - WINDOW_PADDING - WINDOW_MARGIN:
return
self.y += self.dir * self.speed
def position(self):
return pygame.Rect(self.x, self.y, self.rect.width, self.rect.height)
alienBullets = []
tankBullets = []
enemies = []
def create_enemy() -> None:
alien = Alien(ALIEN_SHIP)
alien.x = WINDOW_MARGIN + WINDOW_PADDING
alien.y = WINDOW_MARGIN + DASHBOARD_HEIGHT + WINDOW_PADDING
enemies.append(alien)
def run_game():
# game attrib
HITS = 0
BULLETS_USED = 0
LIVES_LEFT = 10
# game variables
create_enemy()
tank = Tank(TANK)
running = True
clock = pygame.time.Clock()
WINDOW.fill(BLACK)
font = pygame.font.SysFont('Futura', 25, True, False)
# utility variables
deadAliens = []
before = time.time()
d = float('inf')
while running:
clock.tick(FPS)
WINDOW.blit(BACKGROUND, (0, 0, WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.draw.rect(WINDOW, BORDER_COLOR,
(WINDOW_MARGIN, WINDOW_PADDING, WINDOW_WIDTH - 2 * WINDOW_MARGIN + 10, 10 + DASHBOARD_HEIGHT),
)
pygame.draw.rect(WINDOW, BACKGROUND_COLOR,
(WINDOW_MARGIN + 5, WINDOW_PADDING + 5, WINDOW_WIDTH - 2 * WINDOW_MARGIN, DASHBOARD_HEIGHT)
)
# draw game metrics here
WINDOW.blit(font.render(f'HITS: {HITS}', 1, RED),
(WINDOW_MARGIN + 3 * WINDOW_PADDING, int(2.5 * WINDOW_PADDING)))
WINDOW.blit(font.render(f'BULLETS: {BULLETS_USED}', 1, GREEN),
(WINDOW_MARGIN + WINDOW_PADDING + 200, int(2.5 * WINDOW_PADDING)))
WINDOW.blit(font.render(f'LIVES LEFT: {LIVES_LEFT}', 1, YELLOW),
(WINDOW_MARGIN + WINDOW_PADDING + 450, int(2.5 * WINDOW_PADDING)))
pygame.draw.rect(WINDOW, BORDER_COLOR,
(0 + WINDOW_MARGIN, 0 + WINDOW_MARGIN + DASHBOARD_HEIGHT, WINDOW_WIDTH - 2 * WINDOW_MARGIN,
WINDOW_HEIGHT - 2 * WINDOW_MARGIN - DASHBOARD_HEIGHT),
5)
WINDOW.blit(tank.image, (tank.x, tank.y))
for enemy in enemies:
WINDOW.blit(enemy.image, (enemy.x, enemy.y))
enemy.move()
bulletsOutOfScreen = []
for bullet in tankBullets + alienBullets:
WINDOW.blit(bullet.image, (bullet.x, bullet.y))
bullet.move()
if (bullet.dir == UP_DIRECTION and bullet.y < WINDOW_MARGIN + DASHBOARD_HEIGHT + WINDOW_PADDING) or \
(bullet.dir == DOWN_DIRECTION and bullet.y > WINDOW_HEIGHT - WINDOW_MARGIN - WINDOW_PADDING):
bulletsOutOfScreen.append(bullet)
for bullet in bulletsOutOfScreen:
if bullet in tankBullets:
tankBullets.remove(bullet)
if bullet in alienBullets:
alienBullets.remove(bullet)
for bullet in tankBullets:
for alien in enemies:
if pygame.Rect.colliderect(bullet.position(), alien.position()) \
and (alien not in deadAliens):
print("🎯")
EXPLODE.play()
HITS += 1
tankBullets.remove(bullet)
alien.image = EXPLOSION
deadAliens.append((alien, time.time()))
break
deadAliensCopy = deadAliens[:]
for alien in deadAliensCopy:
if time.time() - alien[1] > IMPRESSION_TIME:
try:
enemies.remove(alien[0])
except ValueError:
pass
else:
deadAliens.remove(alien)
del deadAliensCopy
# check for bullets hitting player
for bullet in alienBullets:
if pygame.Rect.colliderect(bullet.position(), tank.position()):
alienBullets.remove(bullet)
if d == float('inf'):
LIVES_LEFT -= 1
SHOOT_2.play()
if LIVES_LEFT < 1 and d == float('inf'):
tank.image = EXPLOSION
EXPLODE.play()
d = time.time()
for alien in enemies:
if pygame.Rect.colliderect(alien.position(), tank.position()) and d == float('inf'):
tank.image = EXPLOSION
alien.image = EXPLOSION
SHOOT_2.play()
EXPLODE.play()
deadAliens.append((alien, time.time()))
d = time.time()
# create enemies
if time.time() - before > IMPRESSION_TIME * 8 and random.random() > ALIEN_CHANCE:
before = time.time()
create_enemy()
# enemies fire at random
for alien in enemies:
if time.time() - before > IMPRESSION_TIME * 5 and random.random() > SHOOT_CHANCE:
alien.fire(alienBullets)
before = time.time()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
tank.fire(tankBullets)
SHOOT.play()
BULLETS_USED += 1
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_RIGHT]:
if tank.x < WINDOW_WIDTH - WINDOW_MARGIN - WINDOW_PADDING - tank.rect.width:
tank.x += SPEED
if keys_pressed[pygame.K_LEFT]:
if tank.x > WINDOW_MARGIN + WINDOW_PADDING:
tank.x -= SPEED
if time.time() - d > IMPRESSION_TIME * 10:
running = False
pygame.display.flip()
pygame.display.update()
WINDOW.blit(font.render("GAME ENDED!!!", 0, RED),
(WINDOW_WIDTH // 4, WINDOW_HEIGHT // 2))
pygame.display.update()
end_game = True
while end_game:
for event in pygame.event.get():
if event.type == pygame.KEYUP or event.type == pygame.QUIT:
end_game = False
WINDOW.blit(font.render("Exiting....... ", 0, RED),
(WINDOW_WIDTH // 4, WINDOW_HEIGHT // 4))
pygame.display.update()
pygame.mixer.music.fadeout(500)
pygame.time.wait(1000)
pygame.quit()