Merge remote-tracking branch 'origin/Player' into MapNew

This commit is contained in:
Wirus 2020-04-05 14:54:59 +02:00
commit c338d6caa1
3 changed files with 62 additions and 14 deletions

View File

@ -2,12 +2,12 @@ from pathlib import Path
import pygame
class Entity:
class Entity(pygame.sprite.Sprite):
nextId = 1
def __init__(self, texture, pos):
self.image = texture
self.rect = self.image.get_rect()
def __init__(self, texture, size, pos):
super().__init__()
self.image, self.rect = self.getTexture(texture, size)
self.rect.x = pos[0]
self.rect.y = pos[1]
self.id = self.getId()
@ -19,8 +19,7 @@ class Entity:
return id
# A method that returns image and rect from a file
@staticmethod
def getTexture(textureName, tileSize):
def getTexture(self, textureName, tileSize):
texturesFolder = ""
textureFile = ""
try:

View File

@ -5,17 +5,33 @@ from src.entities.Statistics import Statistics
import pygame
class Player(Entity, pygame.sprite.Sprite):
class Player(Entity):
def __init__(self, spawnpoint, size):
pygame.sprite.Sprite.__init__(self)
self.statistics = Statistics(100, 0, 0, 100)
self.image, self.rect = Entity.getTexture("player.jpg", size)
super(Player, self).__init__(self.image, spawnpoint)
super().__init__("player.jpg", size, spawnpoint)
# Where the player is facing, 0 - north, 1
self.rotation = Rotations.NORTH
# Move in a desired direction
def move(self, rotation):
# If the player is not facing given direction, it will not move the first time, it will only get rotated
if self.rotation.value != rotation.value:
self.rotate(rotation)
# Otherwise, move one tile to a given direction
else:
# 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, ((self.rotation.value - rotation.value) * 90))
self.rotation = rotation
class Rotations(Enum):
NORTH = 0

View File

@ -1,11 +1,44 @@
import pygame
from src.entities.Player import Rotations
# Player can move every given milliseconds
TIMEOUT = 100
class EventManager:
def __init__(self, gameObject):
keyTimeout = 0
def __init__(self, gameObject, player):
self.game = gameObject
self.player = player
self.keyTimer = pygame.time.Clock()
# Player controls
# TODO
def loadKeyboardSettings(self):
pass
def handleEvents(self):
pygame.event.pump()
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game.running = False
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]:
self.player.move(Rotations.SOUTH)
if keys[pygame.K_d]:
self.player.move(Rotations.EAST)
if keys[pygame.K_a]:
self.player.move(Rotations.WEST)