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 import pygame
from src.entities.Interactable import Interactable from src.entities.Interactable import Interactable
@ -14,6 +16,8 @@ class EventManager:
self.game = gameObject self.game = gameObject
self.player = player self.player = player
self.keyTimer = pygame.time.Clock() self.keyTimer = pygame.time.Clock()
self.turnOff = False
# Player controls # Player controls
# TODO # TODO
@ -23,6 +27,10 @@ class EventManager:
def handleEvents(self): def handleEvents(self):
pygame.event.pump() pygame.event.pump()
if self.turnOff:
sleep(5)
exit(0)
self.game.screen.ui.updateTime() self.game.screen.ui.updateTime()
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
@ -32,43 +40,48 @@ class EventManager:
self.game.screen.ui.updateBasedOnPygameEvent(event) self.game.screen.ui.updateBasedOnPygameEvent(event)
self.keyTimeout += self.keyTimer.tick() self.keyTimeout += self.keyTimer.tick()
if self.keyTimeout >= TIMEOUT: if self.keyTimeout >= TIMEOUT:
self.handlePlayerControls(keys) if self.player.alive:
self.keyTimeout = 0 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): def handlePlayerControls(self, keys):
# Key names are temporary # Key names are temporary
# TODO: Load key bindings from JSON # TODO: Load key bindings from JSON
if self.player.alive: # Picking up items
# Picking up items if keys[pygame.K_SPACE]:
if keys[pygame.K_SPACE]: object = self.game.map.getEntityOnCoord(self.player.getFacingCoord())
object = self.game.map.getEntityOnCoord(self.player.getFacingCoord()) # Picked up item gets removed from the map
# Picked up item gets removed from the map if type(object) is Pickupable:
if type(object) is Pickupable: object.on_interaction(self.player)
object.on_interaction(self.player) self.game.screen.ui.updateOnPlayerPickup(self.player.statistics, object)
self.game.map.removeSpriteFromMap(object) self.game.map.removeSpriteFromMap(object)
elif type(object) is Interactable: elif type(object) is Interactable:
object.on_interaction(self.player) object.on_interaction(self.player)
self.game.screen.ui.updateOnPlayerInteraction(self.player.statistics, object)
# Movement # Movement
if keys[pygame.K_w]: if keys[pygame.K_w]:
self.player.rotate(Rotations.NORTH) self.player.rotate(Rotations.NORTH)
if not self.game.map.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w): if not self.game.map.collision(self.player.rect.x, self.player.rect.y - self.player.rect.w):
self.player.move(Rotations.NORTH) self.player.move(Rotations.NORTH)
if keys[pygame.K_s]: if keys[pygame.K_s]:
self.player.rotate(Rotations.SOUTH) self.player.rotate(Rotations.SOUTH)
if not self.game.map.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w): if not self.game.map.collision(self.player.rect.x, self.player.rect.y + self.player.rect.w):
self.player.move(Rotations.SOUTH) self.player.move(Rotations.SOUTH)
if keys[pygame.K_d]: if keys[pygame.K_d]:
self.player.rotate(Rotations.EAST) self.player.rotate(Rotations.EAST)
if not self.game.map.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y): if not self.game.map.collision(self.player.rect.x + self.player.rect.w, self.player.rect.y):
self.player.move(Rotations.EAST) self.player.move(Rotations.EAST)
if keys[pygame.K_a]: if keys[pygame.K_a]:
self.player.rotate(Rotations.WEST) self.player.rotate(Rotations.WEST)
if not self.game.map.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y): if not self.game.map.collision(self.player.rect.x - self.player.rect.w, self.player.rect.y):
self.player.move(Rotations.WEST) self.player.move(Rotations.WEST)

View File

@ -2,6 +2,7 @@ from enum import Enum
import pygame import pygame
from src.entities.Player import StatisticNames
from src.entities.Statistics import Statistics from src.entities.Statistics import Statistics
from src.ui.UiBar import UiBar from src.ui.UiBar import UiBar
from src.ui.UiConsole import UiConsole from src.ui.UiConsole import UiConsole
@ -62,26 +63,16 @@ class Ui():
screenHeight - self.timerTextView.rect.h - self.isDayTextView.rect.h), screenHeight - self.timerTextView.rect.h - self.isDayTextView.rect.h),
font=self.font) font=self.font)
def updateBasedOnPlayerStats(self, statistics: Statistics): def updateConsoleBasedOnPlayerStats(self, statistics: Statistics):
consoleLines = [] consoleLines = ["Health: " + str(statistics.hp), "Hunger: " + str(statistics.hunger),
if self.healthBar.value != statistics.hp: "Stamina: " + str(statistics.stamina), "Thirst: " + str(statistics.thirst)]
self.healthBar.updateFill(statistics.hp) self.console.addLinesToConsoleAndScrollToDisplayThem(consoleLines)
consoleLines.append(self.timer.getPrettyTime() + " - Health: " + str(statistics.hp))
if self.hungerBar.value != statistics.hunger: def updateBarsBasedOnPlayerStats(self, statistics: Statistics):
self.hungerBar.updateFill(statistics.hunger) self.healthBar.updateFill(statistics.hp)
consoleLines.append(self.timer.getPrettyTime() + " - Hunger: " + str(statistics.hunger)) self.hungerBar.updateFill(statistics.hunger)
self.staminaBar.updateFill(statistics.stamina)
if self.staminaBar.value != statistics.stamina: self.thirstBar.updateFill(statistics.thirst)
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:
self.console.addLinesToConsoleAndScrollToDisplayThem(consoleLines)
def updateBasedOnPygameEvent(self, event: pygame.event): def updateBasedOnPygameEvent(self, event: pygame.event):
if event.type == pygame.MOUSEBUTTONDOWN: if event.type == pygame.MOUSEBUTTONDOWN:
@ -91,6 +82,36 @@ class Ui():
elif event.button == 5: elif event.button == 5:
console.writeConsoleLines(console.topWrittenLineInd - 1) 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): def updateTime(self):
self.timerTextView.changeText(self.timer.getPrettyTime()) self.timerTextView.changeText(self.timer.getPrettyTime())
if self.timer.isItDay(): if self.timer.isItDay():