60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
from .unit import Unit
|
|
from .bomb import Explosion
|
|
class Mine(Unit):
|
|
def __init__(self, game, position=(0,0), id=None):
|
|
super().__init__(game, position, id)
|
|
self.speed = 1.0 # Mine doesn't move but needs speed for consistency
|
|
self.armed = True # Mine is active and ready to explode
|
|
|
|
def move(self):
|
|
"""Mines don't move, but we need to check for collision with rats each frame."""
|
|
pass
|
|
|
|
def collisions(self):
|
|
"""Check if a rat steps on the mine (has position_before on mine's position)."""
|
|
if not self.armed:
|
|
return
|
|
|
|
# Check for rats that have position_before on this mine's position
|
|
for rat_unit in self.game.unit_positions_before.get(self.position, []):
|
|
if hasattr(rat_unit, 'sex'): # Check if it's a rat (rats have sex attribute)
|
|
# Mine explodes and kills the rat
|
|
self.explode(rat_unit)
|
|
break
|
|
|
|
def explode(self, victim_rat):
|
|
"""Mine explodes, killing the rat and destroying itself."""
|
|
if not self.armed:
|
|
return
|
|
|
|
print("MINE EXPLOSION!")
|
|
self.game.render_engine.play_sound("BOMB.WAV")
|
|
|
|
# Kill the rat that stepped on the mine
|
|
if victim_rat.id in self.game.units:
|
|
victim_rat.die(score=5)
|
|
|
|
# Remove the mine from the game
|
|
self.die()
|
|
|
|
def draw(self):
|
|
"""Draw the mine using the mine asset."""
|
|
if not self.armed:
|
|
return
|
|
|
|
# Use mine asset
|
|
image = self.game.assets["mine"]
|
|
image_size = self.game.render_engine.get_image_size(image)
|
|
|
|
# Center the mine in the cell
|
|
x_pos = self.position[0] * self.game.cell_size + (self.game.cell_size - image_size[0]) // 2
|
|
y_pos = self.position[1] * self.game.cell_size + (self.game.cell_size - image_size[1]) // 2
|
|
|
|
self.game.render_engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")
|
|
|
|
def die(self, score=None):
|
|
"""Remove mine from game and disarm it."""
|
|
self.armed = False
|
|
Explosion(self.game, self.position).draw()
|
|
super().die(score)
|