Merge remote-tracking branch 'origin/passingEvents' into passingEvents

This commit is contained in:
Marcin Kostrzewski 2020-04-05 23:53:25 +02:00
commit 05ce54933d
4 changed files with 28 additions and 1 deletions

View File

@ -6,6 +6,8 @@ import pygame
class Player(Entity):
statistics: Statistics
def __init__(self, spawnpoint, size):
super().__init__("player.jpg", size, (spawnpoint[0] * size, spawnpoint[1] * size))
# Where the player is facing, 0 - north, 1
@ -41,6 +43,9 @@ class Player(Entity):
return self.statistics.stamina
return None
def getStatistics(self):
return self.statistics
def rotate(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:

View File

@ -25,11 +25,14 @@ class EventManager:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game.running = False
self.game.screen.ui.updateBasedOnPygameEvent(event)
self.keyTimeout += self.keyTimer.tick()
if self.keyTimeout >= TIMEOUT:
self.handlePlayerControls(keys)
self.keyTimeout = 0
self.game.screen.ui.updateBasedOnPlayerStats(self.player.statistics)
def handlePlayerControls(self, keys):
# Key names are temporary
# TODO: Load key bindings from JSON

View File

@ -10,6 +10,7 @@ from game.Map import Map
from src.entities.Pickupable import Pickupable
from src.entities.Player import Player
from src.entities.Statistics import Statistics
from src.game.Timer import Timer
class Game:
@ -40,6 +41,11 @@ class Game:
print("The screen cannot be in a vertical orientation. Exiting...")
exit(1)
# Initialize timers
self.pgTimer = pygame.time.Clock()
self.ingameTimer = Timer()
self.ingameTimer.startClock()
self.screen = Screen(self, self.config["window"])
print("OK")
@ -49,11 +55,12 @@ class Game:
self.map.addEntity(self.player)
self.eventManager = EventManager(self, self.player)
self.mainLoop()
def mainLoop(self):
while self.running:
# Update ingame clock
self.ingameTimer.updateTime(self.pgTimer.tick())
self.eventManager.handleEvents()
self.spritesList.draw(self.screen.pygameScreen)
pygame.display.flip()

View File

@ -2,6 +2,7 @@ from enum import Enum
import pygame
from src.entities.Statistics import Statistics
from src.ui.UiBar import UiBar
from src.ui.UiConsole import UiConsole
from src.ui.UiText import UiText
@ -50,6 +51,17 @@ class Ui():
screenHeight - self.timerTextView.rect.h - self.isDayTextView.rect.h),
font=self.font)
def updateBasedOnPlayerStats(self, statistics: Statistics):
self.healthBar.updateFill(statistics.hp)
self.hungerBar.updateFill(statistics.hunger)
self.staminaBar.updateFill(statistics.stamina)
self.thirstBar.updateFill(statistics.thirst)
def updateBasedOnPygameEvent(self, event: pygame.event):
pass
class Colors(Enum):
RED = (255, 0, 0)