-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.py
65 lines (56 loc) · 2.04 KB
/
score.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
# score.py
from turtle import Turtle
class Scoreboard(Turtle):
"""
Class for managing the game's scoreboard.
Attributes:
top_wall_y_value (int): Y-coordinate of the top wall for positioning the scoreboard.
r_score (int): Score of the right player.
l_score (int): Score of the left player.
Methods:
update_scoreboard(): Updates the display of scores.
game_over(): Displays the game over message.
"""
def __init__(self, top_wall_y_value, score_text_color="white"):
super().__init__()
self.top_wall_y_value = top_wall_y_value
self.score_text_color = score_text_color
self.r_score = 0
self.l_score = 0
self.setup_scoreboard()
def setup_scoreboard(self):
"""
Sets up the initial appearance of the scoreboard.
"""
self.color(self.score_text_color)
self.penup()
self.hideturtle()
self.update_scoreboard()
def update_scoreboard(self):
"""
Clears the current scores and writes the new scores on the screen.
"""
self.clear()
self.goto(-100, self.top_wall_y_value)
self.write(self.l_score, align="center", font=("Courier", 80, "bold"))
self.goto(100, self.top_wall_y_value)
self.write(self.r_score, align="center", font=("Courier", 80, "bold"))
def update_score(self, ball):
"""
Updates the score based on the ball's position. Increments the score for the left or right player
depending on which side the ball goes out of bounds.
Args:
ball (Ball): The ball object to check its x-coordinate.
"""
if ball.xcor() > 0:
self.l_score += 1
else:
self.r_score += 1
self.update_scoreboard()
def game_over(self):
"""
Displays the 'GAME OVER' message.
"""
self.clear()
self.goto(0, 0)
self.write(f"Final score - Left: {self.l_score}, Right: {self.r_score}", align="center", font=("Courier", 28, "bold"))