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.
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 squareTurtleobjects, one added each time food is eaten
texto, is a hidden Turtle used exclusively to display the score at position (0, -260):
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.
Game loop sequence
Each iteration ofwhile True: follows this sequence:
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.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.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 (The loop runs from
cuerpo[0]) copies the snake head’s position.total - 1 down to 1 (inclusive) so that each segment moves to where the previous one was, not where the next one is going.Move the snake head
movimiento() reads serpiente.direction and shifts the head by 20 pixels in that direction.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.