-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoreboard.py
68 lines (57 loc) · 1.82 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
import pygame
from pygame.locals import *
from constants import *
# Scoreboard Object
class Scoreboard:
# Preset values for a scoreboard
score_one = 0
score_two = 0
# Initialization of scoreboard with max score
def __init__(self, max):
self.max = max
#end
# Counts each time a ball is scored
def count(self, ball):
if ball.scored():
self.add(ball.possession)
ball.switchPossession()
ball.reset(ball.possession)
#end
# Returns the score
def score(self):
return self.score_one, self.score_two, self.winner()
#end
# Adds a point to a player
def add(self, player):
if player == 2:
self.score_one = self.score_one + 1
elif player == 1:
self.score_two = self.score_two + 1
#end
# Returns 1 or 2 when a player has won, otherwise 0
def winner(self):
winner = 0
if self.score_one == self.max:
winner = 1
elif self.score_two == self.max:
winner = 2
return winner
#end
# Resets the score
def reset(self):
self.score_one = 0
self.score_two = 0
#end
# Renders the score to the screen
def render(self, screen):
font = pygame.font.Font('fonts/RobotoMono.ttf', 40)
player_one = font.render(str(self.score_one), True, WHITE)
player_two = font.render(str(self.score_two), True, WHITE)
player_one = pygame.transform.flip(player_one, False, True)
player_two = pygame.transform.flip(player_two, False, True)
x1 = len(str(abs(self.score_one))) - 1
x2 = len(str(abs(self.score_two))) - 1
screen.blit(player_one, (CENTER_X - 50 - x1 * 14, HEIGHT - 48))
screen.blit(player_two, (CENTER_X + 28 - x2 * 14, HEIGHT - 48))
#end
#end Scoreboard