Final 2.0

This commit is contained in:
2025-08-20 16:43:16 +02:00
parent 0141063baa
commit 17e5abc7df
8 changed files with 112 additions and 11 deletions
Binary file not shown.
+69
View File
@@ -0,0 +1,69 @@
from .unit import Unit
from .rat import Rat
import random
# Costanti
AGE_THRESHOLD = 200
class Gas(Unit):
def __init__(self, game, position=(0,0), id=None, parent_id=None):
super().__init__(game, position, id)
self.parent_id = parent_id
# Specific attributes for gas
self.speed = 50
self.fight = False
self.age = 0
if parent_id:
self.age = round(random.uniform(0, AGE_THRESHOLD))
self.spreading_cells = []
self.last_spreading_cells = []
def move(self):
if self.age > AGE_THRESHOLD:
self.die()
return
self.age += 1
#victims = self.game.unit_positions.get(self.position, [])
victims = [rat for rat in self.game.unit_positions.get(self.position, []) if rat.partial_move>0.5]
for rat in self.game.unit_positions_before.get(self.position, []):
if rat.partial_move<0.5 and rat is Rat:
victims.append(rat)
for victim in victims:
victim.gassed += 1
if self.age % self.speed:
return
parent = self.game.get_unit_by_id(self.parent_id)
if (parent) or self.parent_id is None:
print(f"Gas at {self.position} is spreading")
# Spread gas to adjacent cells
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
new_x = self.position[0] + dx
new_y = self.position[1] + dy
if not self.game.map.is_wall(new_x, new_y):
if not any(isinstance(unit, Gas) for unit in self.game.units.values() if unit.position == (new_x, new_y)):
print(f"Spreading gas from {self.position} to ({new_x}, {new_y})")
self.game.spawn_unit(Gas, (new_x, new_y), parent_id=self.parent_id if self.parent_id else self.id)
def collisions(self):
pass
def die(self, unit=None):
if not unit:
unit = self
self.game.units.pop(unit.id)
def draw(self):
image = self.game.assets["BMP_GAS"]
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.render_engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
for cell in self.spreading_cells:
x_pos = cell[0] * self.game.cell_size + (self.game.cell_size - image_size[0]) // 2 + partial_x
y_pos = cell[1] * self.game.cell_size + (self.game.cell_size - image_size[1]) // 2 + partial_y
self.game.render_engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
+10 -1
View File
@@ -17,6 +17,7 @@ class Rat(Unit):
# Specific attributes for rats
self.speed = 0.10 # Rats are slower
self.fight = False
self.gassed = 0
# Initialize position using pathfinding
self.position = self.find_next_position()
@@ -46,8 +47,15 @@ class Rat(Unit):
self.position_before = self.position
self.position_before = self.position
return neighbors[random.randint(0, len(neighbors) - 1)]
def choked(self):
self.game.render_engine.play_sound("CHOKE.WAV")
self.die(score=10)
def move(self):
if self.gassed > 35:
self.choked()
return
self.age += 1
if self.age == AGE_THRESHOLD:
self.speed *= SPEED_REDUCTION
@@ -69,6 +77,7 @@ class Rat(Unit):
return
units = []
units.extend(self.game.unit_positions.get(self.position_before, []))
units.extend(self.game.unit_positions_before.get(self.position, []))
for unit in units:
if unit.id == self.id or unit.age < AGE_THRESHOLD or self.position != unit.position_before: