-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiTank.py
391 lines (324 loc) · 12.6 KB
/
multiTank.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
import pygame
import random
import sys
# Game window dimensions
WIDTH = 1100
HEIGHT = 600
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0,255,0)
BLUE = (0,0,255)
pygame.init()
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tank Battle Game")
clock = pygame.time.Clock()
# Load images
background_image = pygame.image.load("images/background.jpg").convert()
tank_image = pygame.image.load("images/tank.png").convert_alpha()
tank_image2=pygame.image.load("images/tank2.png").convert_alpha()
bullet_image = pygame.image.load("images/bullet.png").convert_alpha()
enemy_image = pygame.image.load("images/enemy.png").convert_alpha()
# Scale images to fit the game window
background_image = pygame.transform.scale(background_image, (WIDTH, HEIGHT))
tank_image = pygame.transform.scale(tank_image, (70, 70))
tank_image2=pygame.transform.scale(tank_image2, (60,60))
bullet_image = pygame.transform.scale(bullet_image, (50, 50))
enemy_image = pygame.transform.scale(enemy_image, (60, 60))
# Load sounds
bullet_sound = pygame.mixer.Sound("music/bullet.mp3")
game_background_music = pygame.mixer.Sound("music/game_bgm.mp3") # Replace with the path to your background music file
start_screen_bgm = pygame.mixer.Sound("music/starting_bgm.mp3")
# Tank class
class Tank(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = tank_image
self.rect = self.image.get_rect()
self.rect.center = (WIDTH // 2, HEIGHT - 50)
self.speed = 5
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.rect.x -= self.speed
if keys[pygame.K_RIGHT]:
self.rect.x += self.speed
if health1==0:
self.kill()
self.rect.x = max(0, min(WIDTH - self.rect.width, self.rect.x)) # Keep tank within screen boundaries
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.y)
all_sprites.add(bullet)
bullets.add(bullet)
pygame.mixer.Channel(0).play(bullet_sound, maxtime=600)
# tank2 class
class Tank2(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = tank_image2
self.rect = self.image.get_rect()
self.rect.center = (WIDTH // 2, HEIGHT - 50)
self.speed = 5
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
self.rect.x -= self.speed
if keys[pygame.K_d]:
self.rect.x += self.speed
if health2==0:
self.kill()
self.rect.x = max(0, min(WIDTH - self.rect.width, self.rect.x)) # Keep tank within screen boundaries
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.y)
all_sprites.add(bullet)
bullets.add(bullet)
pygame.mixer.Channel(0).play(bullet_sound, maxtime=600)
# Bullet class
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = bullet_image
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.speed = 10
def update(self):
self.rect.y -= self.speed
if self.rect.bottom < 0:
self.kill()
# Enemy class
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = enemy_image
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, WIDTH - self.rect.width)
self.rect.y = random.randint(-100, -40)
self.speed = random.uniform(1.0, 2.0) # Randomized enemy speed
def update(self):
self.rect.y += self.speed
if self.rect.top > HEIGHT:
self.rect.x = random.randint(0, WIDTH - self.rect.width)
self.rect.y = random.randint(-100, -40)
self.speed = random.uniform(1.0, 2.0) # Randomized enemy speed
decrease_health()
def decrease_health():
global health1
global health2
health1 -= 5
health2 -= 5
def decrease_health1():
global health1
health1 -= 10
def decrease_health2():
global health2
health2 -= 10
def game_over():
global game_over_screen
pygame.time.delay(1000) # Delay for 1 second
game_over_screen = True
def game_complete():
global game_complete_screen
pygame.time.delay(1000) # Delay for 1 second
game_complete_screen = True
def show_start_screen():
window.fill(BLACK)
title_font = pygame.font.SysFont(None, 80)
text_font = pygame.font.SysFont(None, 40)
title_text = title_font.render("Tank Battle Game", True, WHITE)
start_text = text_font.render("Press ENTER to Start", True, GREEN)
controls_text0 = text_font.render("Controls", True, WHITE)
controls_text = text_font.render("1st Player : Left/Right Arrow Keys to Move, Right Ctrl key to Shoot", True, WHITE)
controls_text1 = text_font.render("2nd Player : A/D Keys to Move, Space to Shoot", True, WHITE)
quit_text = text_font.render("Press Q to Quit", True, RED)
title_rect = title_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 200))
start_rect = start_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 130))
control = start_text.get_rect(center=(WIDTH // 2 + 80, HEIGHT // 2 - 70))
controls_rect = controls_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 ))
controls_rect1 = controls_text1.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 50))
quit_rect = quit_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 125))
window.blit(title_text, title_rect)
window.blit(start_text, start_rect)
window.blit(controls_text0, control)
window.blit(controls_text, controls_rect)
window.blit(controls_text1, controls_rect1)
window.blit(quit_text, quit_rect)
pygame.display.flip()
def draw_health_bar():
pygame.draw.rect(window, RED, (20, 20, health1, 20))
pygame.draw.rect(window, WHITE, (20, 20, 100, 20), 2)
pygame.draw.rect(window, RED, (20, 50, health2, 20))
pygame.draw.rect(window, WHITE, (20, 50, 100, 20), 2)
def show_game_over_screen():
window.fill(BLACK)
title_font = pygame.font.SysFont(None, 80)
text_font = pygame.font.SysFont(None, 40)
title_text = title_font.render("Game Over", True, WHITE)
restart_text = text_font.render("Press ENTER to Restart", True, WHITE)
quit_text = text_font.render("Press Q to Quit", True, WHITE)
title_rect = title_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 100))
restart_rect = restart_text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
quit_rect = quit_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 50))
window.blit(title_text, title_rect)
window.blit(restart_text, restart_rect)
window.blit(quit_text, quit_rect)
pygame.display.flip()
def show_game_complete_screen():
window.fill(BLACK)
title_font = pygame.font.SysFont(None, 80)
text_font = pygame.font.SysFont(None, 40)
title_text = title_font.render("Awesome!", True, WHITE)
congrat_text=text_font.render("You have completed the game",True,WHITE)
restart_text = text_font.render("Press ENTER to Restart", True, WHITE)
quit_text = text_font.render("Press Q to Quit", True, WHITE)
title_rect = title_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 100))
congrat_rect = congrat_text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
restart_rect = restart_text.get_rect(center=(WIDTH // 2, HEIGHT // 2+50))
quit_rect = quit_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 100))
window.blit(title_text, title_rect)
window.blit(congrat_text,congrat_rect)
window.blit(restart_text, restart_rect)
window.blit(quit_text, quit_rect)
pygame.display.flip()
# Sprite groups
all_sprites = pygame.sprite.Group()
bullets = pygame.sprite.Group()
enemies = pygame.sprite.Group()
# Create player tank
player_tank = Tank()
all_sprites.add(player_tank)
# Create player tank2
player_tank2 = Tank2()
all_sprites.add(player_tank2)
# Game variables
health2= 100
health1=100
score = 0
level = 1
enemy_spawn_rate = 0
enemy_speed = 1.0
running = True
start_screen = True
game_over_screen = False
game_complete_screen=False
text_font = pygame.font.SysFont(None, 40) # Define font for score and level text
while running:
if start_screen:
show_start_screen()
start_screen_bgm.play(-1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
start_screen = False
if event.key == pygame.K_q:
running = False
elif game_over_screen:
show_game_over_screen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
game_over_screen = False
health2 = 100
health1=100
score = 0
level = 1
enemy_spawn_rate = 0
enemy_speed = 1.0
all_sprites.empty()
bullets.empty()
enemies.empty()
player_tank = Tank()
all_sprites.add(player_tank)
player_tank2 = Tank2()
all_sprites.add(player_tank2)
if event.key == pygame.K_q:
running = False
elif game_complete_screen:
show_game_complete_screen()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
game_complete_screen = False
health2 = 100
health1=100
score = 0
level = 1
enemy_spawn_rate = 0
enemy_speed = 1.0
all_sprites.empty()
bullets.empty()
enemies.empty()
player_tank = Tank()
all_sprites.add(player_tank)
player_tank2 = Tank2()
all_sprites.add(player_tank2)
if event.key == pygame.K_q:
running = False
else:
start_screen_bgm.stop()
game_background_music.play(-1)
if len(enemies) < level + enemy_spawn_rate:
enemy = Enemy()
all_sprites.add(enemy)
enemies.add(enemy)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == event.key == pygame.K_RCTRL:
player_tank.shoot()
if event.key == pygame.K_SPACE:
player_tank2.shoot()
if event.key == pygame.K_q:
running = False
all_sprites.update()
# Check for bullet collisions with enemies
bullet_hits = pygame.sprite.groupcollide(bullets, enemies, True, True)
for bullet in bullet_hits.keys():
score += 10
if score >= 100*level and level<15:
level += 1
enemy_spawn_rate += 1
enemy_speed += 0.3
if score >= 100*level and level >= 15 and level < 25:
level += 1
enemy_spawn_rate +=1
enemy_speed += 0.5
if level==25:
game_complete()
# Check for enemy collisions with player tank
enemy_hits = pygame.sprite.spritecollide(player_tank, enemies, True)
if enemy_hits:
decrease_health1()
# Check for enemy collisions with player tank
enemy_hits1 = pygame.sprite.spritecollide(player_tank2, enemies, True)
if enemy_hits1:
decrease_health2()
window.blit(background_image, (0, 0))
all_sprites.draw(window)
draw_health_bar()
# Display score and level
score_text = text_font.render("Score: " + str(score), True, WHITE)
level_text = text_font.render("Level: " + str(level), True, WHITE)
window.blit(score_text, (20, HEIGHT - 50))
window.blit(level_text, (WIDTH - 120, HEIGHT - 50))
pygame.display.flip()
if health1 <=0 :
player_tank.kill()
if health2 <= 0:
player_tank2.kill()
if health1 <= 0 and health2 <=0:
game_over()
if len(enemies) == 0:
level += 1
enemy_spawn_rate += 1
enemy_speed += 0.2
clock.tick(60)
pygame.quit()
sys.exit()