Added pylint and started commenting files (camera and enemy)

enemy as to be checked
This commit is contained in:
2026-07-13 21:33:23 +02:00
parent b4315962cb
commit 367a839c8a
9 changed files with 743 additions and 15 deletions
+56 -8
View File
@@ -1,8 +1,20 @@
from game.util import *
import pygame as pg
"""
Enemy module - enemy movement, rendering, and health management.
"""
import math
import pygame as pg
from game.util import tile_to_pixel
class Enemy(pg.sprite.Sprite):
"""A single enemy that follows waypoints
across the tilemap.
Attributes:
game (Game): Whole Game instances, contains everything that the game class has.
speed (float): Movement speed in pixels per update.
health (float): Current health points.
angle (float): Current facing angle in degrees.
"""
def __init__(self, game, speed, health):
super().__init__()
self.game = game
@@ -20,9 +32,7 @@ class Enemy(pg.sprite.Sprite):
)
self.target_index = 1
self.rect.center = self.pos
self.healthbar = HealthBar(
18,
5,
@@ -30,10 +40,12 @@ class Enemy(pg.sprite.Sprite):
)
def update(self):
"""Advance movement and update rotation each frame."""
self.move()
self.rotate()
self.rotate()
def move(self):
"""Step toward the current waypoint, advancing to the next when reached."""
if self.target_index >= len(self.waypoints):
return
@@ -58,6 +70,7 @@ class Enemy(pg.sprite.Sprite):
self.pos += direction * self.speed
def rotate(self):
"""Face the enemy toward its current target waypoint."""
dist = self.target - self.pos
self.angle = math.degrees(math.atan2(-dist[1], dist[0]))
@@ -66,9 +79,14 @@ class Enemy(pg.sprite.Sprite):
self.rect.center = self.pos
def render(self, surf, camera):
"""Draw the enemy and its health bar scaled to the camera zoom.
Args:
surf (pg.Surface): Target surface to draw onto.
camera (Camera): Active camera used for coordinate conversion and zoom.
"""
zoom = camera.zoom
scaled_rect = self.rect.scale_by(zoom)
scaled_image = pg.transform.scale(
self.image,
(scaled_rect.size[0], scaled_rect.size[1])
@@ -84,23 +102,45 @@ class Enemy(pg.sprite.Sprite):
self.pos,
self.rect.height
)
def take_damage(self, amount):
"""Reduce health by amount and kill the sprite if health reaches zero.
Args:
amount (float): Damage to apply.
"""
self.health -= amount
self.healthbar.hp = self.health
if self.health <= 0:
self.kill()
class EnemyGroup(pg.sprite.Group):
"""Sprite group that delegates rendering and updates to each enemy."""
def render(self, surf, camera):
"""Render all enemies in the group.
Args:
surf (pg.Surface): Target surface to draw onto.
camera (Camera): Active camera for zoom and coordinate conversion.
"""
for enemy in self.sprites():
enemy.render(surf, camera)
def update(self):
"""Update all enemies in the group."""
for enemy in self.sprites():
enemy.update()
class HealthBar():
"""A simple two-rect health bar drawn in world space.
Attributes:
w (int): Width in world pixels.
h (int): Height in world pixels.
hp (float): Current health value.
max_hp (float): Maximum health value.
"""
def __init__(self, w, h, max_hp):
self.w = w
self.h = h
@@ -108,6 +148,14 @@ class HealthBar():
self.max_hp = max_hp
def render(self, surf, camera, pos, enemy_height):
"""Draw the health bar above the enemy, scaled to the camera zoom.
Args:
surf (pg.Surface): Target surface to draw onto.
camera (Camera): Active camera for zoom and coordinate conversion.
pos (pg.Vector2): Enemy's world-space centre position.
enemy_height (int): Enemy sprite height, used to position the bar.
"""
ratio = self.hp / self.max_hp
world_pos = (