Browse Source

Rimuovi la gestione del rilascio dei tasti e migliora la logica di scrolling nella classe KeyBindings; aggiorna la classe GameWindow per gestire correttamente gli eventi di rilascio dei tasti

master
Matteo Benedetto 1 year ago
parent
commit
7f18f7e46e
  1. 24
      engine/controls.py
  2. 5
      engine/sdl2.py
  3. 20
      rats.py

24
engine/controls.py

@ -30,8 +30,6 @@ class KeyBindings:
elif key == "Space" or key == 1:
self.play_sound("PUTDOWN.WAV")
self.spawn_bomb(self.pointer)
elif key == "UpRelease" or key == "DownRelease" or key == "LeftRelease" or key == "RightRelease":
self.stop_scrolling()
elif key == "P":
self.pause = not self.pause
# elif key == "mouse":
@ -39,4 +37,26 @@ class KeyBindings:
# self.pointer = adjusted_coords
# self.scroll_cursor()
def key_released(self, key):
if key in ["Up", "Down", "Left", "Right"]:
self.stop_scrolling()
def start_scrolling(self, direction):
self.scrolling_direction = direction
if not self.scrolling:
self.scrolling = 1
def stop_scrolling(self):
self.scrolling = 0
def scroll(self):
if self.scrolling:
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

5
engine/sdl2.py

@ -144,7 +144,7 @@ class GameWindow:
key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8')
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"
key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8')
self.key_up(key)
print(key)
elif event.type == sdl2.SDL_MOUSEMOTION:
@ -152,6 +152,9 @@ class GameWindow:
elif event.type == sdl2.SDL_JOYBUTTONDOWN:
key = event.jbutton.button
self.key_down(key)
elif event.type == sdl2.SDL_JOYBUTTONUP:
key = event.jbutton.button
self.key_up(key)
# Disegna qui gli sprite
self.renderer.present()

20
rats.py

@ -155,26 +155,6 @@ class MiceMaze(controls.KeyBindings):
def add_point(self, value):
self.points += value
def start_scrolling(self, direction):
self.scrolling_direction = direction
if not self.scrolling:
self.scrolling = 1
def stop_scrolling(self):
self.scrolling = 0
def scroll(self):
if self.scrolling:
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