Browse Source

Modifica la gestione dei tasti nella classe GameWindow; aggiungi supporto per il rilascio dei tasti e migliora la logica di scrolling

master
Matteo Benedetto 1 year ago
parent
commit
2e4d0b8726
  1. 2
      engine/controls.py
  2. 15
      engine/sdl2.py
  3. 29
      rats.py

2
engine/controls.py

@ -38,3 +38,5 @@ class KeyBindings:
# adjusted_coords = (coords[0]//self.cell_size*2, coords[1]//self.cell_size*2)
# self.pointer = adjusted_coords
# self.scroll_cursor()
def key_released(self, key):
self.stop_scrolling()

15
engine/sdl2.py

@ -32,7 +32,7 @@ class GameWindow:
self.factory = sdl2.ext.SpriteFactory(renderer=self.renderer)
self.fonts = self.generate_fonts("assets/decterm.ttf")
self.running = True
self.key_callback = key_callback
self.key_down, self.key_up = key_callback
self.performance = 0
def load_joystick(self):
@ -140,17 +140,18 @@ class GameWindow:
for event in events:
if event.type == sdl2.SDL_QUIT:
self.running = False
elif event.type == sdl2.SDL_KEYDOWN and self.key_callback:
elif event.type == sdl2.SDL_KEYDOWN and self.key_down:
key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8')
self.key_callback(key)
elif event.type == sdl2.SDL_KEYUP and self.key_callback:
self.key_down(key)
elif event.type == sdl2.SDL_KEYUP and self.key_down:
key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8') + "Release"
self.key_callback(key)
self.key_up(key)
print(key)
elif event.type == sdl2.SDL_MOUSEMOTION:
self.key_callback("mouse", coords=(event.motion.x, event.motion.y))
self.key_down("mouse", coords=(event.motion.x, event.motion.y))
elif event.type == sdl2.SDL_JOYBUTTONDOWN:
key = event.jbutton.button
self.key_callback(key)
self.key_down(key)
# Disegna qui gli sprite
self.renderer.present()

29
rats.py

@ -14,7 +14,8 @@ class MiceMaze(controls.KeyBindings):
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.engine = engine.GameWindow(self.map.width, self.map.height,
self.cell_size, "Mice!", key_callback=(self.key_pressed, self.key_released))
self.pointer = (random.randint(1, self.map.width-2), random.randint(1, self.map.height-2))
self.scroll_cursor()
self.points = 0
@ -106,6 +107,7 @@ class MiceMaze(controls.KeyBindings):
unit.collisions()
unit.draw()
self.engine.update_status(f"Mice: {len(self.units)} - Points: {self.points}")
self.scroll()
self.engine.new_cycle(50, self.update_maze)
@ -155,23 +157,24 @@ class MiceMaze(controls.KeyBindings):
def start_scrolling(self, direction):
self.scrolling_direction = direction
self.scrolling = True
self.scroll()
if not self.scrolling:
self.scrolling = 1
def stop_scrolling(self):
self.scrolling = False
self.scrolling = 0
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 not self.scrolling % 5:
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.scrolling += 1
if __name__ == "__main__":

Loading…
Cancel
Save