Merge branch 'writingInfoToConsole'

This commit is contained in:
Michał Czekański 2020-04-06 13:50:01 +02:00
commit b0891c3e48
2 changed files with 83 additions and 49 deletions

View File

@ -1,3 +1,5 @@
from time import sleep
import pygame
from src.entities.Interactable import Interactable
@ -14,6 +16,8 @@ class EventManager:
self.game = gameObject
self.player = player
self.keyTimer = pygame.time.Clock()
self.turnOff = False
# Player controls
# TODO
@ -23,6 +27,10 @@ class EventManager:
def handleEvents(self):
pygame.event.pump()
if self.turnOff:
sleep(5)
exit(0)
self.game.screen.ui.updateTime()
keys = pygame.key.get_pressed()
@ -32,25 +40,30 @@ class EventManager:
self.game.screen.ui.updateBasedOnPygameEvent(event)
self.keyTimeout += self.keyTimer.tick()
if self.keyTimeout >= TIMEOUT:
if self.player.alive:
self.handlePlayerControls(keys)
self.keyTimeout = 0
else:
self.game.screen.ui.updateOnDeath(self.player)
self.turnOff = True
self.game.screen.ui.updateBasedOnPlayerStats(self.player.statistics)
self.game.screen.ui.updateBarsBasedOnPlayerStats(self.player.statistics)
def handlePlayerControls(self, keys):
# Key names are temporary
# TODO: Load key bindings from JSON
if self.player.alive:
# Picking up items
if keys[pygame.K_SPACE]:
object = self.game.map.getEntityOnCoord(self.player.getFacingCoord())
# Picked up item gets removed from the map
if type(object) is Pickupable:
object.on_interaction(self.player)
self.game.screen.ui.updateOnPlayerPickup(self.player.statistics, object)
self.game.map.removeSpriteFromMap(object)
elif type(object) is Interactable:
object.on_interaction(self.player)
self.game.screen.ui.updateOnPlayerInteraction(self.player.statistics, object)
# Movement
if keys[pygame.K_w]:

View File

@ -2,6 +2,7 @@ from enum import Enum
import pygame
from src.entities.Player import StatisticNames
from src.entities.Statistics import Statistics
from src.ui.UiBar import UiBar
from src.ui.UiConsole import UiConsole
@ -62,27 +63,17 @@ class Ui():
screenHeight - self.timerTextView.rect.h - self.isDayTextView.rect.h),
font=self.font)
def updateBasedOnPlayerStats(self, statistics: Statistics):
consoleLines = []
if self.healthBar.value != statistics.hp:
self.healthBar.updateFill(statistics.hp)
consoleLines.append(self.timer.getPrettyTime() + " - Health: " + str(statistics.hp))
if self.hungerBar.value != statistics.hunger:
self.hungerBar.updateFill(statistics.hunger)
consoleLines.append(self.timer.getPrettyTime() + " - Hunger: " + str(statistics.hunger))
if self.staminaBar.value != statistics.stamina:
self.staminaBar.updateFill(statistics.stamina)
consoleLines.append(self.timer.getPrettyTime() + " - Stamina: " + str(statistics.stamina))
if self.thirstBar.value != statistics.thirst:
self.thirstBar.updateFill(statistics.thirst)
consoleLines.append(self.timer.getPrettyTime() + " - Thirst: " + str(statistics.thirst))
if len(consoleLines) > 0:
def updateConsoleBasedOnPlayerStats(self, statistics: Statistics):
consoleLines = ["Health: " + str(statistics.hp), "Hunger: " + str(statistics.hunger),
"Stamina: " + str(statistics.stamina), "Thirst: " + str(statistics.thirst)]
self.console.addLinesToConsoleAndScrollToDisplayThem(consoleLines)
def updateBarsBasedOnPlayerStats(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):
if event.type == pygame.MOUSEBUTTONDOWN:
console = self.console
@ -91,6 +82,36 @@ class Ui():
elif event.button == 5:
console.writeConsoleLines(console.topWrittenLineInd - 1)
def updateOnPlayerPickup(self, playerStats, pickedObject):
self.console.addLinesToConsoleAndScrollToDisplayThem([self.timer.getPrettyTime() + " - Picked object " + str(pickedObject.id) + ":"])
self.updateConsoleBasedOnPlayerStats(playerStats)
def updateOnPlayerInteraction(self, playerStats, interactedObject):
self.console.addLinesToConsoleAndScrollToDisplayThem([self.timer.getPrettyTime() + " - Player interacted with " + str(interactedObject.id) + ":"])
self.updateConsoleBasedOnPlayerStats(playerStats)
def updateOnDeath(self, player):
consoleLines = []
deathReason: StatisticNames = player.deathReason
consoleLines.append(self.timer.getPrettyTime() + " - Game Over")
deathReasonString = ""
if deathReason is StatisticNames.HP:
deathReasonString = "Health issues"
elif deathReason is StatisticNames.HUNGER:
deathReasonString = "Hunger"
elif deathReason is StatisticNames.STAMINA:
deathReasonString = "Exhaustion"
elif deathReason is StatisticNames.THIRST:
deathReasonString = "Dehydration"
consoleLines.append("Death reason: " + deathReasonString)
consoleLines.append("Time alive: " + str(player.timeAlive / 1000))
self.console.addLinesToConsoleAndScrollToDisplayThem(consoleLines)
def updateTime(self):
self.timerTextView.changeText(self.timer.getPrettyTime())
if self.timer.isItDay():