Pong
Tennis for two: paddles W/S and arrows, the ball bounces by where it hits. Teaches telling when the paddle hits the ball, functions and scoring.
Open in Tokbit Level: Expert. Free in the browser, nothing to install, no account needed.
Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.
Flowchart
Python program
The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.
"""Pong for two players, first to five points. Teaches functions and ball bounces."""
# Tokbit screen functions, not in plain Python:
from tokbit import (
cls, at, color, background, border, key, sleep, beep, note, sprite,
sprite_at, sprite_color, sprite_hide, sprite_hit, sprite_shape, BLACK,
WHITE, CYAN, YELLOW, LIGHT_RED, GREY,
)
def move_paddle(y: int, k: str, up: str, down: str) -> int:
"""Moves a paddle up or down by the key, staying inside the court."""
if k == up and y > 8:
return y - 8
if k == down and y < 176:
return y + 8
return y
def bounce(ball: int, paddle: int) -> int:
"""Vertical direction after a hit: angled off the paddle edge, straight off the middle."""
d: int = ball - paddle
if d < -5:
return -3
if d > 5:
return 3
return 0
def draw_court():
"""Draws the court with the help line and sets up the paddles and the ball."""
background(BLACK)
border(BLACK)
cls()
color(GREY)
at(1, 24)
print("Left W/S, right arrow keys up and down", end="")
paddle: list[str] = ["......####......", "......####......", "......####......", "......####......", "......####......", "......####......", "......####......", "......####......", "......####......", "......####......", "......####......", "......####......", "......####......", "......####......", "......####......", "......####......"]
sprite_shape(0, paddle)
sprite_shape(1, paddle)
sprite(2, "ball")
sprite_color(0, CYAN)
sprite_color(1, LIGHT_RED)
sprite_color(2, YELLOW)
def move(ball: list[int]):
"""Moves the ball by its step; it bounces off the top and bottom edges."""
ball[0] = ball[0] + ball[2]
ball[1] = ball[1] + ball[3]
if ball[1] <= 8 or ball[1] >= 176:
ball[3] = -ball[3]
beep(330, 20)
def hit(ball: list[int], paddle: int):
"""The paddle returns the ball: flips the horizontal direction and sets the vertical one by where it hit."""
ball[2] = -ball[2]
ball[3] = bounce(ball[1], paddle)
beep(660, 20)
def serve(ball: list[int], direction: int):
"""Puts the ball back in the middle, flying in the given direction (4 right, -4 left)."""
ball[0] = 152
ball[1] = 92
ball[3] = 0
ball[2] = direction
def draw(ly: int, py: int, ball: list[int]):
"""Moves the paddle and ball sprites to their places."""
sprite_at(0, 2, ly)
sprite_at(1, 302, py)
sprite_at(2, ball[0], ball[1])
def show_score(lb: int, pb: int):
"""Prints the score at the top centre."""
color(WHITE)
at(16, 0)
print(str(lb) + " : " + str(pb), end="")
def game_over(lb: int):
"""End of the game: hides the ball, prints the winner and plays a fanfare."""
sprite_hide(2)
color(WHITE)
if lb == 5:
at(12, 11)
print("The left player wins!", end="")
else:
at(12, 11)
print("The right player wins!", end="")
note("C5", 120)
note("G5", 300)
at(12, 13)
playing: str = "a"
while True:
draw_court()
ly: int = 92
py: int = 92
lb: int = 0
pb: int = 0
# The ball: x, y and step dx, dy.
ball: list[int] = [152, 92, 4, 0]
k: str = ""
# Game loop: paddles, ball, bounces and points until someone has five.
while lb < 5 and pb < 5:
k = key()
ly = move_paddle(ly, k, "w", "s")
py = move_paddle(py, k, "up", "down")
move(ball)
draw(ly, py, ball)
if ball[2] < 0 and sprite_hit(2, 0):
hit(ball, ly)
if ball[2] > 0 and sprite_hit(2, 1):
hit(ball, py)
if ball[0] < -16:
pb = pb + 1
beep(150, 200)
serve(ball, -4)
if ball[0] > 320:
lb = lb + 1
beep(150, 200)
serve(ball, 4)
show_score(lb, pb)
sleep(20)
game_over(lb)
playing = input("Play again (y/n)?")
if not (playing == "a" or playing == "y"):
break