Implemented proper player movement

This commit is contained in:
Marcin Kostrzewski 2020-04-05 14:49:37 +02:00
parent e184e6417b
commit 6efd5652dd
2 changed files with 20 additions and 6 deletions

View File

@ -21,10 +21,18 @@ class Player(Entity):
self.rotate(rotation)
# Otherwise, move one tile to a given direction
else:
return 1
# TODO: Collision checks
if rotation.value == Rotations.NORTH.value:
self.rect.y -= self.rect.w
elif rotation.value == Rotations.EAST.value:
self.rect.x += self.rect.w
elif rotation.value == Rotations.SOUTH.value:
self.rect.y += self.rect.w
elif rotation.value == Rotations.WEST.value:
self.rect.x -= self.rect.w
def rotate(self, rotation):
self.image = pygame.transform.rotate(self.image, (abs(self.rotation.value - rotation.value) * 90))
self.image = pygame.transform.rotate(self.image, ((self.rotation.value - rotation.value) * 90))
self.rotation = rotation

View File

@ -2,12 +2,15 @@ import pygame
from src.entities.Player import Rotations
# Player can move every given milliseconds
TIMEOUT = 100
class EventManager:
keyTimeout = 0
def __init__(self, gameObject, player):
self.game = gameObject
self.player = player
self.keyTimer = pygame.time.Clock()
# Player controls
# TODO
@ -21,12 +24,15 @@ class EventManager:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game.running = False
self.handlePlayerControls(keys)
self.keyTimeout += self.keyTimer.tick()
if self.keyTimeout >= TIMEOUT:
self.handlePlayerControls(keys)
self.keyTimeout = 0
def handlePlayerControls(self, keys):
# Key names are temporary
# TODO: Load key bindings from JSON
if keys[pygame.K_w]:
self.player.move(Rotations.NORTH)
if keys[pygame.K_s]: