added labels to ui
This commit is contained in:
46
Game_Client/Classes/System/Components/InputHandler.py
Normal file
46
Game_Client/Classes/System/Components/InputHandler.py
Normal file
@ -0,0 +1,46 @@
|
||||
import pygame
|
||||
|
||||
from Classes.Objects.World import World
|
||||
from Classes.Objects.BoardField import BoardField
|
||||
|
||||
class InputHandler:
|
||||
# returns pressed key
|
||||
def getPressed():
|
||||
return pygame.key.get_pressed()
|
||||
|
||||
# takes in movement inputs and maps them to x and y axis
|
||||
def getInputAxis() -> tuple:
|
||||
xvel:int = 0
|
||||
yvel:int = 0
|
||||
|
||||
# construct x and y velocity input axis
|
||||
if InputHandler.getPressed()[pygame.K_a] or InputHandler.getPressed()[pygame.K_LEFT]:
|
||||
xvel = -1
|
||||
if InputHandler.getPressed()[pygame.K_d] or InputHandler.getPressed()[pygame.K_RIGHT]:
|
||||
xvel = 1
|
||||
if InputHandler.getPressed()[pygame.K_w] or InputHandler.getPressed()[pygame.K_UP]:
|
||||
yvel = -1
|
||||
if InputHandler.getPressed()[pygame.K_s] or InputHandler.getPressed()[pygame.K_DOWN]:
|
||||
yvel = 1
|
||||
|
||||
return tuple((xvel, yvel))
|
||||
|
||||
def getMousePos(self) -> pygame.Vector2:
|
||||
return pygame.Vector2(pygame.mouse.get_pos())
|
||||
|
||||
# get field under mousbutton
|
||||
def getMouseHover(self, world:World) -> BoardField:
|
||||
mouse_pos = self.getMousePos()
|
||||
x_pos = mouse_pos.x / world.getCardWidth()
|
||||
y_pos = mouse_pos.y / world.getCardHeight()
|
||||
|
||||
for field in world.getBoardFields():
|
||||
field_x = field.getPos().x
|
||||
field_y = field.getPos().y
|
||||
field_width = world.getCardWidth() # Annahme: Jedes Feld hat eine Breite von 1 Einheit
|
||||
field_height = world.getCardHeight() # Annahme: Jedes Feld hat eine Höhe von 1 Einheit
|
||||
|
||||
if field_x <= x_pos < field_x + field_width and field_y <= y_pos < field_y + field_height:
|
||||
return field
|
||||
|
||||
return None
|
Reference in New Issue
Block a user