Aggiungi gestione dei controlli da tastiera e migliora la logica di spawn delle unità
This commit is contained in:
@@ -5,35 +5,36 @@ import random
|
||||
from units import rat, bomb
|
||||
import uuid
|
||||
import subprocess
|
||||
from engine import maze, sdl2 as engine
|
||||
from engine import maze, sdl2 as engine, controls
|
||||
import os
|
||||
|
||||
class MiceMaze:
|
||||
class MiceMaze(controls.KeyBindings):
|
||||
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.pointer = (random.randint(1, self.map.width-2), random.randint(1, self.map.height-2))
|
||||
self.scroll_cursor()
|
||||
self.points = 0
|
||||
self.graphics_load()
|
||||
self.units = {}
|
||||
self.unit_positions = {}
|
||||
self.unit_positions_before = {}
|
||||
self.scrolling_direction = None
|
||||
self.scrolling = False
|
||||
for _ in range(5):
|
||||
self.new_rat()
|
||||
|
||||
def new_rat(self, position=None):
|
||||
if position is None:
|
||||
position = self.choose_start()
|
||||
id = uuid.uuid4()
|
||||
rat_class = rat.Male if random.random() < 0.5 else rat.Female
|
||||
self.units[id] = rat_class(self, position, id)
|
||||
self.spawn_unit(rat_class, position)
|
||||
|
||||
def spawn_bomb(self, position):
|
||||
id = uuid.uuid4()
|
||||
self.units[id] = bomb.Timer(self, position, id)
|
||||
self.spawn_unit(bomb.Timer, position)
|
||||
|
||||
def spawn_unit(self, unit, position, **kwargs):
|
||||
id = uuid.uuid4()
|
||||
@@ -75,32 +76,6 @@ class MiceMaze:
|
||||
def run(self):
|
||||
self.draw_maze()
|
||||
self.engine.mainloop(update=self.update_maze, bg_update=self.draw_maze)
|
||||
|
||||
def key_pressed(self, key):
|
||||
print(key)
|
||||
if key == "Q" or key == 12:
|
||||
self.engine.close()
|
||||
elif key == "Return" or key == 13:
|
||||
self.new_rat()
|
||||
elif key == "D":
|
||||
if self.units:
|
||||
self.units[random.choice(list(self.units.keys()))].die()
|
||||
elif key == "M":
|
||||
self.audio = not self.audio
|
||||
elif key == "F":
|
||||
self.full_screen = not self.full_screen
|
||||
self.engine.full_screen(self.full_screen)
|
||||
elif key == "Up" or key == 8:
|
||||
self.scroll_cursor(y=-1)
|
||||
elif key == "Down" or key == 9:
|
||||
self.scroll_cursor(y=1)
|
||||
elif key == "Left" or key == 10:
|
||||
self.scroll_cursor(x=-1)
|
||||
elif key == "Right" or key == 11:
|
||||
self.scroll_cursor(x=1)
|
||||
elif key == "Space" or key == 1:
|
||||
self.play_sound("PUTDOWN.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:
|
||||
@@ -114,7 +89,8 @@ class MiceMaze:
|
||||
|
||||
def play_sound(self, sound_file):
|
||||
if self.audio:
|
||||
subprocess.Popen(["aplay", f"sound/{sound_file}"])
|
||||
#subprocess.Popen(["aplay", f"sound/{sound_file}"])
|
||||
self.engine.play_sound(f"sound/{sound_file}")
|
||||
|
||||
def graphics_load(self):
|
||||
self.tunnel = self.engine.load_image("Rat/BMP_TUNNEL.png")
|
||||
@@ -134,6 +110,26 @@ class MiceMaze:
|
||||
|
||||
def add_point(self, value):
|
||||
self.points += value
|
||||
|
||||
def start_scrolling(self, direction):
|
||||
self.scrolling_direction = direction
|
||||
self.scrolling = True
|
||||
self.scroll()
|
||||
|
||||
def stop_scrolling(self):
|
||||
self.scrolling = False
|
||||
|
||||
def scroll(self):
|
||||
if self.scrolling:
|
||||
if self.scrolling_direction == "Up":
|
||||
self.scroll_cursor(y=-1)
|
||||
elif self.scrolling_direction == "Down":
|
||||
self.scroll_cursor(y=1)
|
||||
elif self.scrolling_direction == "Left":
|
||||
self.scroll_cursor(x=-1)
|
||||
elif self.scrolling_direction == "Right":
|
||||
self.scroll_cursor(x=1)
|
||||
self.engine.new_cycle(100, self.scroll)
|
||||
|
||||
if __name__ == "__main__":
|
||||
profiler = cProfile.Profile()
|
||||
|
||||
Reference in New Issue
Block a user