Add unit tests for UnitFactory functionality and initialize units package

This commit is contained in:
2025-08-13 20:36:51 +02:00
parent 088ae02080
commit 689b21bf65
10 changed files with 181 additions and 203 deletions
+8
View File
@@ -0,0 +1,8 @@
"""
Units package - Game unit classes and factory.
"""
from .unit import Unit
from .rat import Rat, Male, Female
from .bomb import Bomb, Timer, Explosion
from .points import Point
+9 -6
View File
@@ -31,14 +31,14 @@ class Bomb(Unit):
if n < 0:
n = 0
image = self.game.bomb_assets[n]
image_size = self.game.engine.get_image_size(image)
image_size = self.game.render_engine.get_image_size(image)
self.rat_image = image
partial_x, partial_y = 0, 0
x_pos = self.position_before[0] * self.game.cell_size + (self.game.cell_size - image_size[0]) // 2 + partial_x
y_pos = self.position_before[1] * self.game.cell_size + (self.game.cell_size - image_size[1]) // 2 + partial_y
self.game.engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
self.game.render_engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
class Timer(Bomb):
def move(self):
@@ -51,7 +51,7 @@ class Timer(Bomb):
score = 10
print("BOOM")
target_unit = unit if unit else self
self.game.play_sound("BOMB.WAV")
self.game.render_engine.play_sound("BOMB.WAV")
# Use base class cleanup with error handling
try:
@@ -62,20 +62,23 @@ class Timer(Bomb):
# Bomb-specific behavior: create explosion
self.game.spawn_unit(Explosion, target_unit.position)
# Check for chain reactions in all four directions
for direction in ["N", "S", "E", "W"]:
x, y = unit.position
x, y = target_unit.position
while True:
if not self.game.map.is_wall(x, y):
self.game.spawn_unit(Explosion, (x, y))
for victim in self.game.unit_positions.get((x, y), []):
if victim.id in self.game.units:
if victim.partial_move >= 0.5:
print(f"Victim {victim.id} at {x}, {y} dies")
victim.die(score=score)
if score < 160:
score *= 2
for victim in self.game.unit_positions_before.get((x, y), []):
if victim.id in self.game.units:
if victim.partial_move < 0.5:
print(f"Victim {victim.id} at {x}, {y} dies")
victim.die(score=score)
if score < 160:
score *= 2
@@ -99,10 +102,10 @@ class Explosion(Bomb):
def draw(self):
image = self.game.assets["BMP_EXPLOSION"]
image_size = self.game.engine.get_image_size(image)
image_size = self.game.render_engine.get_image_size(image)
partial_x, partial_y = 0, 0
x_pos = self.position_before[0] * self.game.cell_size + (self.game.cell_size - image_size[0]) // 2 + partial_x
y_pos = self.position_before[1] * self.game.cell_size + (self.game.cell_size - image_size[1]) // 2 + partial_y
self.game.engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
self.game.render_engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
+2 -2
View File
@@ -32,11 +32,11 @@ class Point(Unit):
def draw(self):
image = self.game.assets[f"BMP_BONUS_{self.value}"]
image_size = self.game.engine.get_image_size(image)
image_size = self.game.render_engine.get_image_size(image)
self.rat_image = image
partial_x, partial_y = 0, 0
x_pos = self.position_before[0] * self.game.cell_size + (self.game.cell_size - image_size[0]) // 2 + partial_x
y_pos = self.position_before[1] * self.game.cell_size + (self.game.cell_size - image_size[1]) // 2 + partial_y
self.game.engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
self.game.render_engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
+8 -8
View File
@@ -98,12 +98,12 @@ class Rat(Unit):
self.game.spawn_unit(Point, target_unit.position_before, value=score)
def draw(self):
start_perf = self.game.engine.get_perf_counter()
start_perf = self.game.render_engine.get_perf_counter()
direction = self.calculate_rat_direction()
sex = self.sex if self.age > AGE_THRESHOLD else "BABY"
image = self.game.rat_assets[sex][direction]
image_size = self.game.engine.get_image_size(image)
image_size = self.game.render_engine.get_image_size(image)
self.rat_image = image
partial_x, partial_y = 0, 0
@@ -114,9 +114,9 @@ class Rat(Unit):
x_pos = self.position_before[0] * self.game.cell_size + (self.game.cell_size - image_size[0]) // 2 + partial_x
y_pos = self.position_before[1] * self.game.cell_size + (self.game.cell_size - image_size[1]) // 2 + partial_y
self.game.engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
self.game.render_engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
self.bbox = (x_pos, y_pos, x_pos + image_size[0], y_pos + image_size[1])
#self.game.engine.draw_rectangle(self.bbox[0], self.bbox[1], self.bbox[2] - self.bbox[0], self.bbox[3] - self.bbox[1], "unit")
#self.game.render_engine.draw_rectangle(self.bbox[0], self.bbox[1], self.bbox[2] - self.bbox[0], self.bbox[3] - self.bbox[1], "unit")
class Male(Rat):
def __init__(self, game, position=(0,0), id=None):
@@ -125,7 +125,7 @@ class Male(Rat):
def fuck(self, unit):
if not unit.pregnant:
self.game.play_sound("SEX.WAV")
self.game.render_engine.play_sound("SEX.WAV")
self.stop = 100
unit.stop = 200
unit.pregnant = PREGNANCY_DURATION
@@ -144,7 +144,7 @@ class Female(Rat):
self.babies -= 1
self.stop = 20
if self.partial_move > 0.2:
self.game.new_rat(self.position)
self.game.spawn_rat(self.position)
else:
self.game.new_rat(self.position_before)
self.game.play_sound("BIRTH.WAV")
self.game.spawn_rat(self.position_before)
self.game.render_engine.play_sound("BIRTH.WAV")