Skip to content

Commit fa57407

Browse files
Merge pull request #38 from Stephenson-Software/energy-bar
Energy Bar
2 parents a4ef06e + 0dcba9c commit fa57407

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/energyBar.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from graphik import Graphik
2+
from player import Player
3+
4+
# @author Daniel McCoy Stephenson
5+
# @since August 16th, 2022
6+
class EnergyBar:
7+
def __init__(self, graphik: Graphik, player: Player):
8+
self.graphik = graphik
9+
self.player = player
10+
11+
def draw(self):
12+
x, y = self.graphik.getGameDisplay().get_size()
13+
xpos = 0
14+
ypos = y - y/64
15+
width = x * (self.player.getEnergy()/self.player.getMaxEnergy())
16+
height = y/64
17+
color = (255, 215, 73)
18+
self.graphik.drawRectangle(xpos, ypos, width, height, color)

src/player.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def getColor(self):
2323

2424
def getEnergy(self):
2525
return self.energy
26+
27+
def getMaxEnergy(self):
28+
return self.maxEnergy
2629

2730
def addEnergy(self, energy):
2831
self.energy += energy

src/worldScreen.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pygame
55
from apple import Apple
66
from config import Config
7+
from energyBar import EnergyBar
78
from food import Food
89
from graphik import Graphik
910
from grass import Grass
@@ -31,6 +32,7 @@ def __init__(self, graphik: Graphik, config: Config, status: Status, tick: int):
3132
self.numApplesEaten = 0
3233
self.numDeaths = 0
3334
self.running = True
35+
self.energyBar = EnergyBar(self.graphik, self.player)
3436

3537
def initializeLocationWidthAndHeight(self):
3638
x, y = self.graphik.getGameDisplay().get_size()
@@ -269,17 +271,6 @@ def respawnPlayer(self):
269271
self.status.set("respawned", self.tick)
270272
pygame.display.set_caption(("Roam - " + str(self.currentRoom.getName())))
271273

272-
def displayInfo(self):
273-
x, y = self.graphik.getGameDisplay().get_size()
274-
startingX = x/8
275-
startingY = y/8
276-
size = 30
277-
self.displayEnergy(startingX, startingY, size)
278-
279-
def displayEnergy(self, startingX, startingY, size):
280-
self.graphik.drawText("Energy:", startingX, startingY - size/2, size, self.config.black)
281-
self.graphik.drawText(str(floor((self.player.getEnergy()))), startingX, startingY + size/2, size, self.config.black)
282-
283274
def displayInventoryTopItem(self):
284275
x, y = self.graphik.getGameDisplay().get_size()
285276
xpos = x - x/8
@@ -331,9 +322,9 @@ def run(self):
331322
self.graphik.getGameDisplay().fill(self.currentRoom.getBackgroundColor())
332323
self.currentRoom.draw(self.locationWidth, self.locationHeight)
333324
self.status.draw()
325+
self.energyBar.draw()
334326

335327
# display
336-
self.displayInfo()
337328
self.displayInventoryTopItem()
338329

339330
# update

0 commit comments

Comments
 (0)