Skip to content

Commit

Permalink
Player damage and death
Browse files Browse the repository at this point in the history
  • Loading branch information
Magemaster506 committed Jan 16, 2024
1 parent 7ae2a38 commit bced92e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Binary file modified __pycache__/settings.cpython-311.pyc
Binary file not shown.
24 changes: 23 additions & 1 deletion game.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def __init__(self, x, y, radius, health):
self.hit_flash_timer = 0
self.hit_bullets = []
self.alive = True # Add an 'alive' attribute
self.damage = 10

def apply_screen_shake(self, shake_offset):
self.position[0] += shake_offset[0]
Expand All @@ -240,6 +241,10 @@ def decrease_health(self, amount):

def is_alive(self):
return self.health > 0 and self.is_alive

def collide_with_player(self, player_position):
distance = math.sqrt((player_position[0] - self.position[0]) ** 2 + (player_position[1] - self.position[1]) ** 2)
return distance < self.radius

def update(self, player_position):
if self.hit_flash_timer > 0:
Expand Down Expand Up @@ -284,6 +289,14 @@ def check_enemy_collisions(enemies):
clock.tick(60)
wait_time += 1
player_muzzle_flash_particles = MuzzleFlashParticleSystem([player_rect.centerx, player_rect.centery], 4, 1, 1)

if player_invincibility_frames > 0:
player_invincibility_frames -= 1

if player_health <= 0:
pygame.quit()
sys.exit()

if screen_shake > 0:
shake_offset = (random.randint(-screen_shake, screen_shake), random.randint(-screen_shake, screen_shake))
enemy_shake_offset = (random.randint(-screen_shake, screen_shake), random.randint(-screen_shake, screen_shake))
Expand Down Expand Up @@ -415,6 +428,15 @@ def check_enemy_collisions(enemies):
pygame.draw.circle(screen, (255, 0, 0), (int(enemy.position[0]), int(enemy.position[1])), enemy.radius)
if enemy.hit_flash_timer > 0:
pygame.draw.circle(screen, (255, 255, 255), (int(enemy.position[0]), int(enemy.position[1])), enemy.radius)

if player_invincibility_frames == 0:
for enemy in enemies:
if enemy.is_alive():
if enemy.collide_with_player(player_position):
trigger_hit_screen_shake(HIT_SHAKE_INTENSITY)
player_health -= enemy.damage
player_invincibility_frames = invincibility_duration
print(f"Player Health: {player_health}")


screen.blit(player_animations[player_direction][current_frame], player_rect)
Expand All @@ -438,9 +460,9 @@ def check_enemy_collisions(enemies):

if any(enemy.is_alive() for enemy in enemies):
should_rain = 1
check = random.randint(0,5)

if should_rain == 1:
#trigger_hit_screen_shake(1)
sparks.append(Rain([WIN_WIDTH + 200, -200], math.radians(random.randint(100, 170)), random.randint(20, 30), (255, 255, 255, 1), .5))
sparks.append(Rain([WIN_WIDTH + 200, -200], math.radians(random.randint(100, 170)), random.randint(40, 50), (255, 255, 255, 1), .2))

Expand Down
5 changes: 5 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
FIRE_RATE = 10
BULLET_SIZE = 7.5

player_health = 40
player_invincibility_frames = 0
invincibility_duration = 30


HIT_SHAKE_INTENSITY = 2
ENEMY_DESTROY_SHAKE_INTENSITY = 20

Expand Down

0 comments on commit bced92e

Please sign in to comment.