Skip to main content
The game runs on a continuous while True: loop. Each iteration of the loop handles screen updates, collision detection, body movement, and a fixed time delay to control speed.

Screen setup

The canvas is 650×650 pixels with a black background. screen.tracer(0) disables automatic screen updates, and screen.update() is called manually each loop iteration. This gives smooth animation by rendering only once per tick.
screen = turtle.Screen()
screen.setup(650, 650)
screen.bgcolor('black')
screen.tracer(0)

Core objects

Three main objects make up the game:
  • serpiente — the snake head, a light green square starting at (0, 0)
  • comida — the food, a red circle starting at (0, 100)
  • cuerpo — a list of green square Turtle objects, one added each time food is eaten
A fourth object, texto, is a hidden Turtle used exclusively to display the score at position (0, -260):
texto = turtle.Turtle()
texto.speed(0)
texto.color('white')
texto.penup()
texto.hideturtle()
texto.goto(0, -260)
texto.write("Puntaje: 0\tPuntaje más alto: 0", align="center", font=("verdana", 12, "normal"))

Movement delay

At the end of each loop iteration, time.sleep(retraso) pauses execution. retraso is set to 0.1 seconds, giving the game a tick rate of 10 frames per second.
retraso = 0.1
# ...
time.sleep(retraso)

Game loop sequence

Each iteration of while True: follows this sequence:
1

Update the screen

screen.update() flushes all pending drawing operations to the canvas.
screen.update()
2

Check for wall collision

If serpiente moves beyond ±300 on either axis, the game resets: the snake returns to the origin, all body segments are hidden and cleared, direction is set to 'stop', and puntaje resets to 0.
if (serpiente.xcor() > 300 or
serpiente.xcor() < -300 or
serpiente.ycor() > 300 or
serpiente.ycor() < -300):
    time.sleep(2)
    for i in cuerpo:
        i.clear()
        i.hideturtle()
    serpiente.home()
    serpiente.direction = 'stop'
    cuerpo.clear()
    puntaje = 0
3

Check for food collision

If the distance between serpiente and comida is less than 20 pixels, the food teleports to a random position, a new body segment is appended to cuerpo, and the score increases by 10.
if serpiente.distance(comida) < 20:
    x = random.randint(-250, 250)
    y = random.randint(-250, 250)
    comida.goto(x, y)
    nuevoCuerpo = turtle.Turtle()
    nuevoCuerpo.shape('square')
    nuevoCuerpo.color('green')
    nuevoCuerpo.penup()
    nuevoCuerpo.goto(0, 0)
    nuevoCuerpo.speed(0)
    cuerpo.append(nuevoCuerpo)
    puntaje += 10
4

Update body segment positions

Body segments follow the snake by propagating positions backward through the list. Each segment takes the position of the one in front of it, and the first segment (cuerpo[0]) copies the snake head’s position.
total = len(cuerpo)
for i in range(total - 1, 0, -1):
    x = cuerpo[i - 1].xcor()
    y = cuerpo[i - 1].ycor()
    cuerpo[i].goto(x, y)
if total > 0:
    x = serpiente.xcor()
    y = serpiente.ycor()
    cuerpo[0].goto(x, y)
The loop runs from total - 1 down to 1 (inclusive) so that each segment moves to where the previous one was, not where the next one is going.
5

Move the snake head

movimiento() reads serpiente.direction and shifts the head by 20 pixels in that direction.
movimiento()
6

Check for self-collision

Each body segment checks whether its distance from serpiente is less than 20 pixels. If so, the game resets identically to the wall-collision reset.
for i in cuerpo:
    if i.distance(serpiente) < 20:
        for i in cuerpo:
            i.clear()
            i.hideturtle()
        serpiente.home()
        cuerpo.clear()
        serpiente.direction = 'stop'
        puntaje = 0
7

Sleep until the next tick

time.sleep(retraso) pauses the loop for 100ms before the next iteration begins.
time.sleep(retraso)

Build docs developers (and LLMs) love