Python Snake The Snake Head
The snake head is a single square that stands for the front of the snake. This lesson creates that square with a turtle and places it in the centre of the window.
Making the head
Turtle makes a new drawing object. Speed 0 is the fastest setting, so the square appears at once.
head = turtle.Turtle()
head.speed(0)A white square
Shape sets the form to a square. Colour sets the square to white.
head.shape("square")
head.color("white")Placing the head
Penup lifts the pen, so the move draws no line. Goto places the head at the centre, at (0, 0).
head.penup()
head.goto(0, 0)Example
import turtle
# The game window
screen = turtle.Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake")
# The snake head: one square in the middle of the window
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("white")
head.penup()
head.goto(0, 0)
screen.exitonclick()Key points
- A Turtle is a drawing object that can move around the window
- Speed(0) is the fastest setting, so the square appears at once with no drawing animation
- Shape square gives the head its square form
- Colour white makes the head stand out on the black board
- Penup lifts the pen, so moving draws no line
- Goto (0, 0) places the head at the centre
In this example, the program opens the black Snake window from the last lesson. It makes a turtle for the head and sets its speed to the fastest setting. It sets the shape to a square and the colour to white. It lifts the pen and moves the head to (0, 0). A click then closes the window.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Why does the program call head.penup() before head.goto(0, 0)?