Python Snake Score on Screen
The score on screen is a text line at the top of the board. The line shows the current score and the high score.
The pen
The pen is a hidden Turtle at the top of the board. Its only job is writing text.
# The pen writes the score at the top of the board
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)Writing the score line
show_score() clears the old line first. It then writes the new line with the current numbers inside it.
def show_score():
pen.clear()
pen.write(f"Score: {score} High Score: {high_score}", align="center", font=("Courier", 20, "bold"))Keeping the high score
reset_game() updates high_score before score returns to 0. A finished run keeps its score in the high score.
if score > high_score:
high_score = score
score = 0
show_score()A fresh line after a bite
A bite raises the score by 10. show_score() then redraws the line at once.
score = score + 10
show_score()Example
import turtle
import time
import random
# The game window
screen = turtle.Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake")
screen.tracer(0)
# The snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
head.direction = "right"
# The food: one red circle on the board
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
# The body starts empty; the scores start at zero
segments = []
score = 0
high_score = 0
# The pen writes the score at the top of the board
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
def move():
if head.direction == "up":
head.sety(head.ycor() + 20)
if head.direction == "down":
head.sety(head.ycor() - 20)
if head.direction == "left":
head.setx(head.xcor() - 20)
if head.direction == "right":
head.setx(head.xcor() + 20)
def go_up():
if head.direction != "down":
head.direction = "up"
def go_down():
if head.direction != "up":
head.direction = "down"
def go_left():
if head.direction != "right":
head.direction = "left"
def go_right():
if head.direction != "left":
head.direction = "right"
def new_food_spot():
x = random.randint(-14, 14) * 20
y = random.randint(-14, 14) * 20
food.goto(x, y)
def show_score():
pen.clear()
pen.write(f"Score: {score} High Score: {high_score}", align="center", font=("Courier", 20, "bold"))
def reset_game():
global score, high_score
time.sleep(0.5)
head.goto(0, 0)
head.direction = "stop"
for segment in segments:
segment.hideturtle()
segments.clear()
if score > high_score:
high_score = score
score = 0
show_score()
new_food_spot()
show_score()
# The keyboard steers the snake
screen.listen()
screen.onkey(go_up, "Up")
screen.onkey(go_down, "Down")
screen.onkey(go_left, "Left")
screen.onkey(go_right, "Right")
# The game loop: redraw, follow, move, check the rules, eat, wait
while True:
screen.update()
# The body follows: each segment takes the place of the one ahead
for index in range(len(segments) - 1, 0, -1):
x = segments[index - 1].xcor()
y = segments[index - 1].ycor()
segments[index].goto(x, y)
if len(segments) > 0:
segments[0].goto(head.xcor(), head.ycor())
move()
# Walls: the head left the board, so the run ends
if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
reset_game()
# Self: the head ran into the body, so the run ends
for segment in segments:
if segment.distance(head) < 20:
reset_game()
break
# Eat: the head reached the food
if head.distance(food) < 20:
new_food_spot()
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)
score = score + 10
show_score()
time.sleep(0.1)Key points
- The pen is a hidden Turtle whose only job is writing text.
- The pen sits at the top of the board.
- write() draws the score line on the board.
- clear() wipes the old writing before the new line.
- The score line uses an f-string to place the current numbers inside the text.
- high_score is updated inside reset_game() before score returns to 0.
- show_score() runs after a bite and after a reset.
In this example, a hidden pen Turtle sits at the top of the board. show_score() clears the pen and writes the score line with an f-string. A bite adds 10 to the score and calls show_score() again. A reset first saves the score into high_score if it is higher. The reset then sets score to 0 and redraws the line.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Why does show_score() call clear() before write()?