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
View File
+20 -7
View File
@@ -1,37 +1,50 @@
"""Camera module - manages zoom and pan for a 2D viewport."""
import pygame as pg
class Camera():
class Camera:
"""2D camera with zoom and world/screen coordinate conversion.
Attributes:
zoom (float): Current zoom multiplier, clamped to [0.5, 4.0].
offset (pg.math.Vector2): Top-left world position currently in view.
"""
def __init__(self):
self.zoom = 2
self.offset = pg.math.Vector2(0, 0)
def world_to_screen(self, pos):
"""Convert a world-space position to screen pixels."""
return (
int((pos[0] - self.offset.x) * self.zoom),
int((pos[1] - self.offset.y) * self.zoom)
int((pos[1] - self.offset.y) * self.zoom),
)
def screen_to_world(self, pos):
"""Convert a screen pixel position to world-space coordinates."""
return (
pos[0] / self.zoom + self.offset.x,
pos[1] / self.zoom + self.offset.y
pos[1] / self.zoom + self.offset.y,
)
def move(self, dx, dy):
"""Pan the camera by (dx, dy) in world-space units."""
self.offset.x += dx
self.offset.y += dy
def change_zoom(self, amount):
"""Adjust zoom by amount, clamped to [0.5, 4.0]."""
self.zoom += amount
self.zoom = max(0.5, min(self.zoom, 4.0))
def zoom_offset(self, screen_pos, zoom_factor):
"""Zoom toward screen_pos, keeping the world point under it stationary."""
world_before = self.screen_to_world(screen_pos)
self.change_zoom(zoom_factor)
world_after = self.screen_to_world(screen_pos)
# Compensate offset so the point under the cursor doesn't shift
self.offset.x += world_before[0] - world_after[0]
self.offset.y += world_before[1] - world_after[1]
self.offset.y += world_before[1] - world_after[1]
+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 = (
+4
View File
@@ -1,3 +1,7 @@
"""
game class
"""
import pygame
import pygame_gui
View File