Allowed for user input using key arrows

main
Skyball2000 2023-03-29 18:17:18 +02:00
parent 95be2902b1
commit 969c040e88
1 changed files with 23 additions and 10 deletions

View File

@ -1,5 +1,7 @@
from typing import Optional
import pygame.time
from level.Level import Level
from level.LevelManager import LevelManager
from level.selection.ScreenManager import ScreenManager
@ -31,13 +33,15 @@ class LevelSelectionScreenManager(ScreenManager):
self.selected_theme = self.themes[0]
def select_next_theme(self, click: ClickEvent):
if click.is_click_down(ClickEvent.CLICK_LEFT):
self.key_input_delay = -1
def select_next_theme(self, click=None):
if click is None or click.is_click_down(ClickEvent.CLICK_LEFT):
index = (self.themes.index(self.selected_theme) + 1) % len(self.themes)
self.select_theme(self.themes[index])
def select_prev_theme(self, click):
if click.is_click_down(ClickEvent.CLICK_LEFT):
def select_prev_theme(self, click=None):
if click is None or click.is_click_down(ClickEvent.CLICK_LEFT):
index = self.themes.index(self.selected_theme) - 1
if index < 0:
index = len(self.themes) - 1
@ -53,14 +57,14 @@ class LevelSelectionScreenManager(ScreenManager):
def initialize(self):
arrow_left = StaticSprite(self.spritesheet_manager.get_sheet('ui_arrow'))
arrow_left.set_animation_state('left')
arrow_left.position_scale = PositionScale((OFFSET[0], OFFSET[1]), (3, 3))
arrow_left.set_animation_state('right')
arrow_left.position_scale = PositionScale((OFFSET[0], OFFSET[1]), (1.8, 1.8))
arrow_left.add_click_listener(self.select_prev_theme)
self.add_element(DrawLayers.UI, arrow_left)
arrow_right = StaticSprite(self.spritesheet_manager.get_sheet('ui_arrow'))
arrow_right.set_animation_state('right')
arrow_right.position_scale = PositionScale((OFFSET[0] + 50, OFFSET[1]), (3, 3))
arrow_right.set_animation_state('left')
arrow_right.position_scale = PositionScale((OFFSET[0] + 50, OFFSET[1]), (1.8, 1.8))
arrow_right.add_click_listener(self.select_next_theme)
self.add_element(DrawLayers.UI, arrow_right)
@ -146,5 +150,14 @@ class LevelSelectionScreenManager(ScreenManager):
super().destroy()
def tick(self, tick_data: TickData):
if self.key_input_delay >= 0:
self.key_input_delay -= tick_data.dt
if self.key_input_delay <= 0:
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_ESCAPE):
self.main_loop.select_main_menu()
elif tick_data.key_manager.is_keymap_down(KeyManager.KEY_LEFT):
self.select_prev_theme()
self.key_input_delay = 5
elif tick_data.key_manager.is_keymap_down(KeyManager.KEY_RIGHT):
self.select_next_theme()
self.key_input_delay = 5