From 8c1dad010139f516b7c506ab4ceb4e27408bfe52 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sat, 4 Apr 2020 21:33:24 +0200 Subject: [PATCH 1/6] Added move and rotate function --- src/entities/Player.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/entities/Player.py b/src/entities/Player.py index 84f9775..c51f272 100644 --- a/src/entities/Player.py +++ b/src/entities/Player.py @@ -16,6 +16,19 @@ class Player(Entity, pygame.sprite.Sprite): # 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: + return 1 + + def rotate(self, rotation): + self.image = pygame.transform.rotate(self.image, (abs(self.rotation.value - rotation.value) * 90)) + self.rotation = rotation + class Rotations(Enum): NORTH = 0 From 277a84b72e74e942c6f54c7dc4913de2acc1cac3 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sat, 4 Apr 2020 21:33:39 +0200 Subject: [PATCH 2/6] Added player movement --- src/game/EventManager.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/game/EventManager.py b/src/game/EventManager.py index 3cc4e99..5d51510 100644 --- a/src/game/EventManager.py +++ b/src/game/EventManager.py @@ -1,11 +1,38 @@ import pygame +from src.entities.Player import Rotations + class EventManager: - def __init__(self, gameObject): + def __init__(self, gameObject, player): self.game = gameObject + self.player = player + + # 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.handlePlayerControls(keys) + + 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) + From e184e6417be536120283aca7f1e93868fa4b4b9a Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 5 Apr 2020 14:06:56 +0200 Subject: [PATCH 3/6] Entity now inherits from pygame.sprite.Sprite --- src/entities/Entity.py | 3 ++- src/entities/Player.py | 6 ++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/entities/Entity.py b/src/entities/Entity.py index cda0aed..98dff66 100644 --- a/src/entities/Entity.py +++ b/src/entities/Entity.py @@ -2,10 +2,11 @@ from pathlib import Path import pygame -class Entity: +class Entity(pygame.sprite.Sprite): nextId = 1 def __init__(self, texture, pos): + super().__init__() self.image = texture self.rect = self.image.get_rect() self.rect.x = pos[0] diff --git a/src/entities/Player.py b/src/entities/Player.py index c51f272..667372b 100644 --- a/src/entities/Player.py +++ b/src/entities/Player.py @@ -5,14 +5,12 @@ 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__(self.image, spawnpoint) # Where the player is facing, 0 - north, 1 self.rotation = Rotations.NORTH From 6efd5652dd4314be6c79f3345e52821c556684e8 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 5 Apr 2020 14:49:37 +0200 Subject: [PATCH 4/6] Implemented proper player movement --- src/entities/Player.py | 12 ++++++++++-- src/game/EventManager.py | 14 ++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/entities/Player.py b/src/entities/Player.py index 667372b..6fda5bf 100644 --- a/src/entities/Player.py +++ b/src/entities/Player.py @@ -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 diff --git a/src/game/EventManager.py b/src/game/EventManager.py index 5d51510..d2a9a13 100644 --- a/src/game/EventManager.py +++ b/src/game/EventManager.py @@ -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]: From d88670f456342bf8ad89d75e4879c879581854b3 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 5 Apr 2020 14:52:19 +0200 Subject: [PATCH 5/6] Fixed retarded Entity implementation --- src/entities/Entity.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/entities/Entity.py b/src/entities/Entity.py index 98dff66..857f664 100644 --- a/src/entities/Entity.py +++ b/src/entities/Entity.py @@ -5,10 +5,9 @@ import pygame class Entity(pygame.sprite.Sprite): nextId = 1 - def __init__(self, texture, pos): + def __init__(self, texture, size, pos): super().__init__() - self.image = texture - self.rect = self.image.get_rect() + self.image, self.rect = self.getTexture(texture, size) self.rect.x = pos[0] self.rect.y = pos[1] self.id = self.getId() @@ -20,8 +19,7 @@ class Entity(pygame.sprite.Sprite): 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: From a264ac7cb19edfeda4b57a98e603ed0a484706be Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 5 Apr 2020 14:52:30 +0200 Subject: [PATCH 6/6] Refactor to fixed Entity class --- src/entities/Player.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/entities/Player.py b/src/entities/Player.py index 6fda5bf..ed6f38a 100644 --- a/src/entities/Player.py +++ b/src/entities/Player.py @@ -7,10 +7,7 @@ import pygame class Player(Entity): def __init__(self, spawnpoint, size): - self.statistics = Statistics(100, 0, 0, 100) - - self.image, self.rect = Entity.getTexture("player.jpg", size) - super().__init__(self.image, spawnpoint) + super().__init__("player.jpg", size, spawnpoint) # Where the player is facing, 0 - north, 1 self.rotation = Rotations.NORTH