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

View File

@ -5,17 +5,33 @@ from src.entities.Statistics import Statistics
import pygame import pygame
class Player(Entity, pygame.sprite.Sprite): class Player(Entity):
def __init__(self, spawnpoint, size): def __init__(self, spawnpoint, size):
pygame.sprite.Sprite.__init__(self) super().__init__("player.jpg", size, spawnpoint)
self.statistics = Statistics(100, 0, 0, 100)
self.image, self.rect = Entity.getTexture("player.jpg", size)
super(Player, self).__init__(self.image, spawnpoint)
# Where the player is facing, 0 - north, 1 # Where the player is facing, 0 - north, 1
self.rotation = Rotations.NORTH 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): class Rotations(Enum):
NORTH = 0 NORTH = 0

View File

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