-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
353 lines (295 loc) · 12 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
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
import pygame
import random
import sys
pygame.init()
# Initialize the game
screen_width = 1000
screen_height = 500
score = 0
# Create the screen
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Harish Game")
# Load the background image for the menu
menu_background = pygame.image.load("assets/menu.png")
menu_background = pygame.transform.scale(menu_background, (screen_width, screen_height))
#Load the background image for the game
background_image = pygame.image.load("assets/background.png").convert()
background_image = pygame.transform.scale(background_image, (screen_width, screen_height))
bg_width = background_image.get_width()
player_lives = 3
font = pygame.font.Font(None, 36)
#Laser Class
class Bullet:
def __init__(self,x,y, img):
self.x = x
self.y = y
self.img = pygame.image.load(img)
self.img = pygame.transform.scale(self.img, (20, 20))
self.rect = self.img.get_rect()
self.rect.center = (x, y)
def draw(self, window):
self.rect.center = (self.x, self.y)
screen.blit(self.img, self.rect)
def move(self, vel):
self.x += vel
def enemy_move(self,vel):
self.x -= vel
def off_screen(self):
return not(self.x <= screen_width and self.x >= 0)
enemy_bullets = []
#Enemy Class
class Enemy:
#constructor for enemy class
def __init__(self, x, y):
self.x = x
self.y = y
self.img = pygame.image.load('assets/enemy1.png')
self.img = pygame.transform.scale(self.img, (130, 110))
self.bullet_img = 'assets/enemy_bullet.png'
self.rect = self.img.get_rect()
self.rect.center = (x, y)
self.run_animation_count=0
self.img_dict = {
0:'assets/enemy1.png',
1:'assets/enemy2.png',
2:'assets/enemy3.png',
}
self.last_shot_time = pygame.time.get_ticks()
#Drawing the enemy on the screen
def draw(self):
self.rect.center = (self.x, self.y)
screen.blit(self.img, self.rect)
#running animation of the enemy
def run_animation_enemy(self):
self.img = pygame.image.load(self.img_dict[int(self.run_animation_count)])
self.img = pygame.transform.scale(self.img, (130, 110))
self.rect = self.img.get_rect()
self.rect.center = (self.x, self.y)
self.run_animation_count+=0.3
self.run_animation_count=self.run_animation_count%3
#Shooting the enemy bullets
def shoot(self):
print("enemy bullets")
enemy_laser = Bullet(self.x - 28, self.y -18, self.bullet_img)
enemy_bullets.append(enemy_laser)
self.last_shot_time = current_time
print("Shooting enemy bullet:", enemy_laser.x, enemy_laser.y)
#enemy and player
enemies = []
player_bullets = []
# class for the main character
class Character:
def __init__(self, x, y):
self.x = x
self.y = y
self.img = pygame.image.load('assets/run1.png')
self.img = pygame.transform.scale(self.img, (100, 100))
self.bullet_img = 'assets/bullet.png'
self.rect = self.img.get_rect()
self.rect.center = (x, y)
self.is_jump = False
self.jump_count = 15
self.run_animation_count = 0
self.img_dict = {
0:'assets/run1.png',
1:'assets/run2.png',
2:'assets/run3.png',
3:'assets/run4.png',
}
#drawing the main character on the screen
def draw(self):
self.rect.center = (self.x, self.y)
screen.blit(self.img, self.rect)
#jumping function for the player
def jump(self):
if self.jump_count >= -15:
n = 1
if self.jump_count < 0:
n = -1
self.y -= ((self.jump_count ** 2) / 10 * n)
self.jump_count -= 1
else:
self.is_jump = False
self.jump_count = 15
def run_animation_player(self):
if(not self.is_jump ):
self.img = pygame.image.load(self.img_dict[int(self.run_animation_count)])
self.img = pygame.transform.scale(self.img, (100, 100))
self.rect = self.img.get_rect()
self.rect.center = (self.x, self.y)
self.run_animation_count+=0.5
self.run_animation_count=self.run_animation_count%4
def shoot(self):
bullet = Bullet(self.x -28, self.y -18, self.bullet_img)
player_bullets.append(bullet)
print("Shooting player bullet:", bullet.x, bullet.y)
# Shield class
class Shield:
def __init__(self, x, y):
self.x = x
self.y = y
self.img = pygame.image.load('assets/shield.png')
self.img = pygame.transform.scale(self.img, (100, 100))
self.rect = self.img.get_rect()
self.rect.center = (x, y)
def draw(self):
self.rect.center = (self.x, self.y)
screen.blit(self.img, self.rect)
#funcion created to check collision with enemy bullet
def collides_with_enemy_laser(self, enemy_laser):
return pygame.Rect.colliderect(self.rect, enemy_laser.rect)
#Game over font
game_over_font = pygame.font.Font(None, 64)
#tells the time at which the last enemy was spawned, helps to keep track of the plan
last_enemy_spawn_time = pygame.time.get_ticks()
# Create the character
player = Character(100, 386)
# Game state
menu_active = True
game_active = False
# Game loop
running = True
clock = pygame.time.Clock()
speed_increasing_rate = 0
bg_x = 0
shield = None
shield_active = False
while running:
# Menu
if menu_active:
screen.blit(menu_background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and menu_active:
mouse_x, mouse_y = pygame.mouse.get_pos()
if 400 <= mouse_x <= 600 and 300 <= mouse_y <= 400: # Replace these coordinates with the button area
menu_active = False
game_active = True
last_enemy_spawn_time = pygame.time.get_ticks()
pygame.display.update()
clock.tick(30)
# Game
if game_active:
#infinite running background
speed_increasing_rate += 0.006
bg_x -= (10 + speed_increasing_rate)
if bg_x < -bg_width:
bg_x = 0
screen.blit(background_image, (bg_x, 0))
screen.blit(background_image, (bg_x + bg_width, 0))
score += 1 # scoring
# Drawing the player
player.draw()
player.run_animation_player()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Jumping code
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not player.is_jump:
player.is_jump = True
if event.key == pygame.K_RETURN: # Checking if shield is open or not
if not shield_active:
shield_active = True
shield = Shield(player.x, player.y)
else:
shield_active = False
# Command for shooting
if event.key == pygame.K_RIGHT and not shield_active:
player.shoot()
if player.is_jump:
player.jump()
# Update and draw player bullets
for bullet in player_bullets[:]:
bullet.move(10) # Move the bullet horizontally
bullet.draw(screen) # Draw the bullet
if bullet.off_screen():
player_bullets.remove(bullet) # Removing offscreen lasers
# Update and draw enemy bullets
for enemy_laser in enemy_bullets[:]:
enemy_laser.move(-20)
enemy_laser.draw(screen)
if shield_active and shield is not None:
if shield.collides_with_enemy_laser(enemy_laser):
enemy_bullets.remove(enemy_laser)
if enemy_laser.off_screen():
enemy_bullets.remove(enemy_laser)
# Forming shield in front of the player
if shield_active:
shield.x = player.x
shield.y = player.y
shield.draw()
current_time = pygame.time.get_ticks()
# Conditions for spawning of enemy
if current_time - last_enemy_spawn_time >= 2000:
if random.randint(0, 100) < 3:
enemy_x = screen_width + 900
enemy_y = 396
enemy = Enemy(enemy_x, enemy_y)
enemies.append(enemy)
last_enemy_spawn_time = current_time
# Spawning enemies
for enemy in enemies[:]:
enemy.x -= 15
enemy.draw()
enemy.run_animation_enemy()
current_time = pygame.time.get_ticks()
# Enemy shooting time constraint
if current_time - enemy.last_shot_time >= 2000:
enemy.shoot()
# Collision for enemy and player bullet
for bullet in player_bullets[:]:
if pygame.Rect.colliderect(enemy.rect, bullet.rect):
print("Collision detected!")
player_bullets.remove(bullet)
enemies.remove(enemy)
score += 10
print("Remaining lasers:", len(player_bullets))
break
# Collision for enemy bullet and player
for enemy_laser in enemy_bullets[:]:
if pygame.Rect.colliderect(player.rect, enemy_laser.rect):
print("Collision detected!")
enemy_bullets.remove(enemy_laser)
if not shield_active:
player_lives -= 1
print("Remaining lasers:", len(enemy_bullets))
break
# Collision between enemy and player
if enemy.rect.colliderect(player.rect):
speed_increasing_rate = 0
player_lives -= 1
enemies.remove(enemy)
if player_lives <= 0:
game_over_text = game_over_font.render("Game Over", True, (255, 255, 255))
screen.blit(game_over_text, (screen_width // 2 - 120, screen_height // 2))
pygame.display.update()
pygame.time.wait(2000)
pygame.quit()
sys.exit()
# Removing enemy from screen
if enemy.x + enemy.rect.width < 0:
enemies.remove(enemy)
# Collision detection between player bullets and enemy bullets
for bullet in player_bullets[:]:
for enemy_laser in enemy_bullets[:]:
if pygame.Rect.colliderect(bullet.rect, enemy_laser.rect):
print("Collision detected between bullets!")
player_bullets.remove(bullet)
enemy_bullets.remove(enemy_laser)
break
# Displaying lives
lives_text = font.render(f"Lives: {player_lives}", True, (0, 0, 0))
screen.blit(lives_text, (screen_width - 120, 10))
# Displaying Score
score_text = font.render(f"Score: {score}", True, (0, 0, 0))
screen.blit(score_text, (20, 10))
# Displaying game name
game_name_text = font.render("Naruto Firestorm Clash", True, (0, 0, 0))
game_name_rect = game_name_text.get_rect(center=(screen_width / 2, 30))
screen.blit(game_name_text, game_name_rect)
# Updating the screen
pygame.display.update()
clock.tick(30) # Timer for the FPS
pygame.quit()