Python Snake Placing the Food
The food is a second Turtle that waits on the board for the snake. A function places it on a random square of the board.
The food turtle
The food is a second Turtle, built with the same setup steps as the head. It lifts its pen with penup(), so it leaves no trail when it moves.
# The food: one red circle on the board
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()A random spot on the grid
new_food_spot() picks one x and one y for the food. It then sends the food to that spot with goto.
def new_food_spot():
x = random.randint(-14, 14) * 20
y = random.randint(-14, 14) * 20
food.goto(x, y)Placing the food at the start
One call sits below the definition, outside the loop. The food is placed once, before the first move.
new_food_spot()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()
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)
new_food_spot()
# 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, move, wait, repeat
while True:
screen.update()
move()
time.sleep(0.1)Key points
- The food is a Turtle with shape "circle" and colour "red"
- random.randint(-14, 14) picks a whole number from -14 to 14
- Multiplying the pick by 20 lands it on the 20-pixel grid the head steps on
- 14 times 20 is 280, so the food always lands inside the board
- new_food_spot() places the food on one random square
In this example, the window, the head and the steering work as before, and the program adds a red circle Turtle named food. new_food_spot() picks x and y, and each one is a random whole number from -14 to 14 times 20. The food goes to that square, so it always sits on the grid the head steps on. The single call before the loop places the food once, and the loop then crawls the head to the right. The Eating and Growing lesson calls new_food_spot() again each time the snake reaches the food.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Why does new_food_spot() multiply the random number by 20?