Skip to content
Snippets Groups Projects
Commit a445c926 authored by Jantz's avatar Jantz
Browse files

Added turret enemy targeting and shooting

parent e1b8c4dc
No related branches found
No related tags found
No related merge requests found
import pygame as pg
from enemy import Enemy
class Turret(pg.sprite.Sprite):
def __init__(self,image,pos):
def __init__(self, image, pos):
pg.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.rect.center = pos
self.attack_damage = 10
self.attack_cooldown = 0.25
self.time_since_last_attack = self.attack_cooldown
def find_nearest_enemy(self, enemies: list[Enemy]) -> Enemy:
"""
Returns the nearest enemy object.
Parameters:
enemies:
The list of enemies.
"""
nearest_enemy = None
nearest_distance = -1
for enemy in enemies:
distance = ((self.rect.center[0] - enemy.screen_position[0])**2 + (self.rect.center[1] - enemy.screen_position[1])**2) ** 0.5
if distance < nearest_distance or nearest_distance < 0:
nearest_enemy = enemy
nearest_distance = distance
return nearest_enemy
\ No newline at end of file
......@@ -77,6 +77,16 @@ class World:
if not collision:
self.turret_group.add(temp_turret)
print(self.cursor_turret.get_size())
def turret_attacks(self, dt: float):
for turret in self.turret_group:
target = turret.find_nearest_enemy(self.enemies)
if target is not None:
turret.time_since_last_attack += dt
if turret.time_since_last_attack >= turret.attack_cooldown:
target.hp -= turret.attack_damage
print(f"Enemy hit! HP: {target.hp}")
turret.time_since_last_attack = 0
def draw(self):
"""Draws the world elements to the passed surface."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment