
Finishing up our game of Pong we build our scoreboard and adjust the ball speed when it hits a paddle, reset the ball speed after a score and add the scoreboard. Let’s start with the scoreboard.
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Arial", 20, "normal")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.penup()
self.color("white")
self.score_left = 0
self.score_right = 0
self.score_update()
def score_update(self):
self.goto(-30, 260)
self.write(f"{self.score_left}",False, ALIGNMENT, FONT)
self.goto(0, 262)
self.write("|", False, ALIGNMENT, FONT)
self.goto(30, 260)
self.write(f"{self.score_right}", False, ALIGNMENT, FONT)
def increase_left(self):
self.score_left += 1
self.clear()
self.score_update()
def increase_right(self):
self.score_right += 1
self.clear()
self.score_update()
We create our scoreboard file and class. We were able to reference the scoreboard we built for our Snake game to get a lot of this setup like that we need to inherit the Turtle class, how to use update and clear in the implementation, and that we should have a method for increasing the score. We add 2 counters to this scoreboard though and two increase score options, one for each player. Then we pull this into main.py
from turtle import Screen
from paddle import Paddle
from ball import Ball
from scoreboard import Scoreboard
import time
screen = Screen()
screen.setup(800, 600)
screen.bgcolor("black")
screen.title("Pong")
screen.tracer(0)
scoreboard = Scoreboard()
r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
ball = Ball()
screen.listen()
screen.onkey(r_paddle.go_up, "Up")
screen.onkey(r_paddle.go_down, "Down")
screen.onkey(l_paddle.go_up, "w")
screen.onkey(l_paddle.go_down, "s")
game_is_on = True
while game_is_on:
time.sleep(ball.b_speed)
screen.update()
ball.move_ball()
if ball.ycor() > 280 or ball.ycor() < -280:
ball.bounce_y()
if ball.xcor() > 380:
ball.reset_pos()
scoreboard.increase_left()
if ball.xcor() < -380:
ball.reset_pos()
scoreboard.increase_right()
if ball.distance(r_paddle) < 50 and ball.xcor() > 325 or ball.distance(l_paddle) < 50 and ball.xcor() < -325:
ball.bounce_x()
We import our scoreboard and call it before our paddles. We also call the the scoreboard increase_(left or right) methods with the if statements detecting when the ball goes out of bounds on the sides scoring a point for a player. The last thing we worked on is increasing the speed of the ball which we did by modifying the time.sleep with a attribute in the ball class and file.
from turtle import Turtle
class Ball(Turtle):
def __init__(self):
super().__init__()
self.penup()
self.shape("circle")
self.color("white")
self.x_move = 10
self.y_move = 5
self.b_speed = 0.1
def move_ball(self):
new_x = self.xcor() + self.x_move
new_y = self.ycor() + self.y_move
self.goto(new_x, new_y)
def reset_pos(self):
self.goto(0,0)
self.b_speed = 0.1
self.bounce_x()
def bounce_y(self):
self.y_move *= -1
def bounce_x(self):
self.x_move *= -1
self.b_speed *= 0.9
The b_speed gets reduced with our bounce_x method that is used when the ball hits a paddle. As the b_speed variable gets reduced, it makes the ball move faster as the time.sleep method in main gets reduced more and more. We reset this b_speed variable with the reset_pos method that is used after a player scores to reset the balls position, but now also its speed.