45 lines
1.5 KiB
Python

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()
xPos = [int(v//world.getCardWidth()) for v in mouse_pos.x]
yPos = [int(v//world.getCardHeifht()) for v in mouse_pos.y]
try:
for field in world.getBoardFields():
for x in xPos:
for y in yPos:
if x == field.getPos().x and y == field.getPos().y:
return field
except IndexError: pass
return None