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

This commit is contained in:
Matteo Benedetto
2024-12-17 00:14:02 +01:00
parent db4cff34b0
commit acbd943b27
10 changed files with 260 additions and 24 deletions
+53 -8
View File
@@ -2,17 +2,21 @@
import cProfile
import pstats
import random
from units import rat
from units import rat, bomb
import uuid
import subprocess
from engine import maze, sdl2 as engine
import os
class MiceMaze:
def __init__(self, maze_file):
self.map = maze.Map(maze_file)
self.pointer = (2,2)
self.audio = True
self.cell_size = 40
self.full_screen = False
self.engine = engine.GameWindow(self.map.width, self.map.height, self.cell_size, "Mice!", key_callback=self.key_pressed)
self.points = 0
self.graphics_load()
self.units = {}
self.unit_positions = {}
@@ -26,7 +30,15 @@ class MiceMaze:
id = uuid.uuid4()
rat_class = rat.Male if random.random() < 0.5 else rat.Female
self.units[id] = rat_class(self, position, id)
def spawn_bomb(self, position):
id = uuid.uuid4()
self.units[id] = bomb.Timer(self, position, id)
def spawn_unit(self, unit, position, **kwargs):
id = uuid.uuid4()
self.units[id] = unit(self, position, id, **kwargs)
def choose_start(self):
if not hasattr(self, '_valid_positions'):
self._valid_positions = [
@@ -45,6 +57,8 @@ class MiceMaze:
def update_maze(self):
self.engine.delete_tag("unit")
self.engine.delete_tag("effect")
self.engine.draw_pointer(self.pointer[0] * self.cell_size, self.pointer[1] * self.cell_size)
self.unit_positions.clear()
self.unit_positions_before.clear()
for unit in self.units.values():
@@ -54,7 +68,7 @@ class MiceMaze:
unit.move()
unit.collisions()
unit.draw()
self.engine.update_status(f"Mice: {len(self.units)}")
self.engine.update_status(f"Mice: {len(self.units)} - Points: {self.points}")
self.engine.new_cycle(50, self.update_maze)
@@ -65,7 +79,7 @@ class MiceMaze:
def key_pressed(self, key):
print(key)
if key == "Q":
self.engine.window.close()
self.engine.close()
elif key == "Return":
self.new_rat()
elif key == "D":
@@ -73,10 +87,30 @@ class MiceMaze:
self.units[random.choice(list(self.units.keys()))].die()
elif key == "M":
self.audio = not self.audio
elif key == "S":
profiler.disable()
stats = pstats.Stats(profiler).sort_stats('cumtime')
stats.print_stats()
elif key == "F":
self.full_screen = not self.full_screen
self.engine.full_screen(self.full_screen)
elif key == "Up":
self.scroll_cursor(y=-1)
elif key == "Down":
self.scroll_cursor(y=1)
elif key == "Left":
self.scroll_cursor(x=-1)
elif key == "Right":
self.scroll_cursor(x=1)
elif key == "Space":
self.play_sound("BOMB.wav")
self.spawn_bomb(self.pointer)
def scroll_cursor(self, x=0, y=0):
if self.pointer[0] + x > self.map.width or self.pointer[1] + y > self.map.height:
return
self.pointer = (
max(1, min(self.map.width-2, self.pointer[0] + x)),
max(1, min(self.map.height-2, self.pointer[1] + y))
)
self.engine.scroll_view(self.pointer)
def play_sound(self, sound_file):
if self.audio:
@@ -86,10 +120,21 @@ class MiceMaze:
self.tunnel = self.engine.load_image("Rat/BMP_TUNNEL.png")
self.grasses = [self.engine.load_image(f"Rat/BMP_1_GRASS_{i+1}.png") for i in range(4)]
self.rat_assets = {}
self.bomb_assets = {}
for sex in ["MALE", "FEMALE", "BABY"]:
self.rat_assets[sex] = {}
for direction in ["UP", "DOWN", "LEFT", "RIGHT"]:
self.rat_assets[sex][direction] = self.engine.load_image(f"Rat/BMP_{sex}_{direction}.png", transparent_color=(128, 128, 128))
for n in range(5):
self.bomb_assets[n] = self.engine.load_image(f"Rat/BMP_BOMB{n}.png", transparent_color=(128, 128, 128))
self.assets = {}
for file in os.listdir("assets/Rat"):
if file.endswith(".png"):
self.assets[file[:-4]] = self.engine.load_image(f"Rat/{file}")
def add_point(self, value):
self.points += value
if __name__ == "__main__":
profiler = cProfile.Profile()
profiler.enable()