forked from mattboan/Galtron
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathscoreboard.py
97 lines (81 loc) · 3.84 KB
/
scoreboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pygame as pg
import pygame.font
import utilityFunctions
getInvertedRGB = utilityFunctions.getInvertedRGB
class Scoreboard():
"""A class for scorekeeping"""
def __init__(self, setting, screen, stats):
self.screen = screen
self.screenRect = screen.get_rect()
self.setting = setting
self.stats = stats
self.active = False
# Font settings for scoring information
self.textColor = (255, 255, 255)
self.font = pygame.font.Font('Fonts/Square.ttf', 20)
self.lifeImage = pg.image.load('gfx/player_ship/red/player_ship.png')
self.lifeImage = pg.transform.scale(self.lifeImage, (30, 30))
self.lifeImageRect = self.lifeImage.get_rect()
# Prepare the initial score image
self.prepScore()
self.prepHighScore()
self.prepLevel()
def invertColor(self):
self.textColor = getInvertedRGB(self.textColor)
def prepScore(self):
"""Turn the score into a rendered image"""
roundedScore = int(self.stats.score)
scoreStr = "Score: "
scoreStr += "{:,}".format(roundedScore)
self.scoreImg = self.font.render(scoreStr, True, self.textColor,
self.setting.bgColor)
# Display the score at the top left corner
self.scoreRect = self.scoreImg.get_rect()
self.scoreRect.right = self.screenRect.right - 20
self.scoreRect.top = 10
def prepHighScore(self):
"""Turn the high score into a rendered image"""
highScore = int(self.stats.highScore)
highScoreStr = "HS: "
highScoreStr += "{:,}".format(highScore)
self.highScoreImg = self.font.render(highScoreStr, True, self.textColor,
self.setting.bgColor)
# Center the highscore
self.highScoreRect = self.highScoreImg.get_rect()
self.highScoreRect.x = 20
self.highScoreRect.top = self.scoreRect.top
def prepLevel(self):
"""Turn the level into a rendered image."""
# self.stats.level = "LVL " + str(self.stats.level)
self.levelImg = self.font.render("Level: " + str(self.stats.level), True, self.textColor,
self.setting.bgColor)
# position below the score
self.levelRect = self.levelImg.get_rect()
self.levelRect.right = self.scoreRect.right
self.levelRect.top = self.scoreRect.bottom + 2
def drawLife(self):
"""Show how many lives are left/ships"""
self.lifeImage = pg.image.load('gfx/player_ship/' + self.setting.playerShipColor + '/player_ship.png')
self.lifeImage = pg.transform.scale(self.lifeImage, (30, 30))
self.lifeImageRect = self.lifeImage.get_rect()
self.lifeImageRect.x = 10
self.lifeImageRect.y = self.scoreRect.bottom + 2
for i in range(self.stats.shipsLeft):
self.screen.blit(self.lifeImage, self.lifeImageRect)
self.lifeImageRect.x += self.lifeImageRect.width + 10
lifefont = pg.font.Font('Fonts/Square.ttf', 20)
lifeStr = lifefont.render("Life", True, (255, 255, 255), self.setting.bgColor)
lifeStrpos = (self.lifeImageRect.x - 35, self.lifeImageRect.y + 40)
self.screen.blit(lifeStr, lifeStrpos)
def prepCounter(self, active):
self.counterImg = self.font.render(str(self.stats.counter), True, self.textColor,
self.setting.bgColor)
self.counterRect = self.counterImg.get_rect()
self.counterRect.center = self.screenRect.center
self.active = active
def showScore(self):
"""Draw the score to screen"""
self.screen.blit(self.scoreImg, self.scoreRect)
self.screen.blit(self.highScoreImg, self.highScoreRect)
self.screen.blit(self.levelImg, self.levelRect)
self.drawLife()