Aggiungi la classe Point e implementa la gestione delle esplosioni nel gioco

This commit is contained in:
2024-12-17 00:14:02 +01:00
parent db4cff34b0
commit acbd943b27
10 changed files with 260 additions and 24 deletions
Binary file not shown.
+105
View File
@@ -0,0 +1,105 @@
from .unit import Unit
from .points import Point
import uuid
# Costanti
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
self.fight = False
def move(self):
pass
def collisions(self):
pass
def die(self, unit=None):
if not unit:
unit = self
self.game.units.pop(unit.id)
def draw(self):
n = self.age // 40
n= 3 -n +1
if n < 0:
n = 0
print(self.age)
image = self.game.bomb_assets[n]
image_size = self.game.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")
class Timer(Bomb):
def move(self):
self.age += self.speed
if self.age == AGE_THRESHOLD:
self.die()
def die(self, unit=None):
print("BOOM")
if not unit:
unit = self
self.game.play_sound("BOMB.WAV")
self.game.units.pop(unit.id)
self.game.spawn_unit(Explosion, unit.position)
for direction in ["N", "S", "E", "W"]:
x, y = 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:
victim.die()
self.game.spawn_unit(Point, (x, y), value=10)
for victim in self.game.unit_positions_before.get((x, y), []):
if victim.id in self.game.units:
if victim.partial_move < 0.5:
victim.die()
self.game.spawn_unit(Point, (x, y), value=10)
else:
break
if direction == "N":
y -= 1
elif direction == "S":
y += 1
elif direction == "E":
x += 1
elif direction == "W":
x -= 1
class Explosion(Bomb):
def move(self):
self.age += self.speed*5
if self.age == AGE_THRESHOLD:
self.die()
def draw(self):
image = self.game.assets["BMP_EXPLOSION"]
image_size = self.game.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")
+48
View File
@@ -0,0 +1,48 @@
from .unit import Unit
import random
import uuid
# Costanti
AGE_THRESHOLD = 200
class Point(Unit):
def __init__(self, game, position=(0,0), id=None, value=5):
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
self.fight = False
self.value = value
self.game.add_point(self.value)
def move(self):
self.age += self.speed
if self.age == AGE_THRESHOLD:
self.die()
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[f"BMP_BONUS_{self.value}"]
image_size = self.game.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")
+3 -4
View File
@@ -1,4 +1,5 @@
from .unit import Unit
import random
import uuid
@@ -21,7 +22,7 @@ class Rat(Unit):
self.speed = .10
self.partial_move = 0
self.position_before = position
self.fight = True
self.fight = False
def calculate_rat_direction(self):
x, y = self.position
@@ -97,10 +98,8 @@ class Rat(Unit):
def die(self, unit=None):
if not unit:
unit = self
self.game.play_sound("POISON.WAV")
self.game.units.pop(unit.id)
def draw(self):
direction = self.calculate_rat_direction()
sex = self.sex if self.age > AGE_THRESHOLD else "BABY"
@@ -119,7 +118,7 @@ class Rat(Unit):
self.game.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.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, map, position=(0,0), id=None):