Refactor unit classes to reduce code duplication and improve maintainability
- Updated the Unit base class to include common attributes and methods for all units. - Refactored Bomb, Point, and Rat classes to inherit from the new Unit class structure. - Implemented a consistent die method across units for better cleanup. - Removed redundant code in unit initialization and added specific attributes where necessary. - Deleted obsolete configuration file and added a comprehensive architecture guide for future reference.
This commit is contained in:
+13
-15
@@ -8,16 +8,9 @@ AGE_THRESHOLD = 200
|
||||
|
||||
class Bomb(Unit):
|
||||
def __init__(self, game, position=(0,0), id=None):
|
||||
super().__init__(position)
|
||||
self.id = id if id else uuid.uuid4()
|
||||
self.game = game
|
||||
self.position = position
|
||||
self.bbox = (0, 0, 0, 0)
|
||||
self.stop = 0
|
||||
self.age = 0
|
||||
self.speed = 4
|
||||
self.partial_move = 0
|
||||
self.position_before = position
|
||||
super().__init__(game, position, id)
|
||||
# Specific attributes for bombs
|
||||
self.speed = 4 # Bombs age faster
|
||||
self.fight = False
|
||||
|
||||
def move(self):
|
||||
@@ -54,16 +47,21 @@ class Timer(Bomb):
|
||||
self.die()
|
||||
|
||||
def die(self, unit=None, score=None):
|
||||
"""Handle bomb explosion and chain reactions."""
|
||||
score = 10
|
||||
print("BOOM")
|
||||
if not unit:
|
||||
unit = self
|
||||
target_unit = unit if unit else self
|
||||
self.game.play_sound("BOMB.WAV")
|
||||
|
||||
# Use base class cleanup with error handling
|
||||
try:
|
||||
self.game.units.pop(unit.id)
|
||||
if target_unit.id in self.game.units:
|
||||
self.game.units.pop(target_unit.id)
|
||||
except:
|
||||
print(f"Unit {unit.id} already dead")
|
||||
self.game.spawn_unit(Explosion, unit.position)
|
||||
print(f"Unit {target_unit.id} already dead")
|
||||
|
||||
# Bomb-specific behavior: create explosion
|
||||
self.game.spawn_unit(Explosion, target_unit.position)
|
||||
for direction in ["N", "S", "E", "W"]:
|
||||
x, y = unit.position
|
||||
while True:
|
||||
|
||||
Reference in New Issue
Block a user