Skip to main content
This page documents every function defined in SnakeGame.py and every module-level variable that controls game behavior.

Functions

arriba()

Sets the snake’s direction to 'up' unless it is currently moving 'down'.
def arriba():
    if serpiente.direction != 'down':
        serpiente.direction = 'up'
Parameters: none Returns: nothing Guard condition: The check serpiente.direction != 'down' prevents the player from immediately reversing the snake into its own body when it is traveling downward. If the snake is stopped (direction == 'stop'), the guard passes and the direction is set to 'up' normally. Binding: Called by screen.onkeypress(arriba, "Up").

abajo()

Sets the snake’s direction to 'down' unless it is currently moving 'up'.
def abajo():
    if serpiente.direction != 'up':
        serpiente.direction = 'down'
Parameters: none Returns: nothing Guard condition: The check serpiente.direction != 'up' prevents reversal when traveling upward. If the snake is stopped, the guard passes and the direction is set to 'down' normally. Binding: Called by screen.onkeypress(abajo, "Down").

derecha()

Sets the snake’s direction to 'right' unless it is currently moving 'left'.
def derecha():
    if serpiente.direction != 'left':
        serpiente.direction = 'right'
Parameters: none Returns: nothing Guard condition: The check serpiente.direction != 'left' prevents reversal when traveling left. If the snake is stopped, the guard passes and the direction is set to 'right' normally. Binding: Called by screen.onkeypress(derecha, "Right").

izquierda()

Sets the snake’s direction to 'left' unless it is currently moving 'right'.
def izquierda():
    if serpiente.direction != 'right':
        serpiente.direction = 'left'
Parameters: none Returns: nothing Guard condition: The check serpiente.direction != 'right' prevents reversal when traveling right. If the snake is stopped, the guard passes and the direction is set to 'left' normally. Binding: Called by screen.onkeypress(izquierda, "Left").

movimiento()

Moves the snake head 20 pixels in the current direction by reading serpiente.direction and calling .setx() or .sety().
def movimiento():
    if serpiente.direction == 'up':
        y = serpiente.ycor()
        serpiente.sety(y + 20)
    if serpiente.direction == 'down':
        y = serpiente.ycor()
        serpiente.sety(y - 20)
    if serpiente.direction == 'right':
        x = serpiente.xcor()
        serpiente.setx(x + 20)
    if serpiente.direction == 'left':
        x = serpiente.xcor()
        serpiente.setx(x - 20)
Parameters: none Returns: nothing Behavior: Reads the current value of serpiente.direction and applies an offset:
DirectionAxisOffset
'up'Y+20
'down'Y-20
'right'X+20
'left'X-20
'stop'no movement
When serpiente.direction is 'stop' (the initial state), none of the four if branches match and the function returns without moving the head. The first key press the player makes sets the direction to a non-'stop' value, so the next call to movimiento() begins movement. movimiento() is called once per game loop iteration, after body segment positions have been propagated. This ordering ensures that each segment copies the position of the segment ahead of it before the head advances.
The 20-pixel step size matches the square turtle size, keeping the snake on a consistent grid. Changing the step size without adjusting the turtle size or collision thresholds will break grid alignment and collision detection.

Variables

retraso
float
Frame delay in seconds. Passed to time.sleep(retraso) at the end of each game loop iteration to throttle the game speed. The default value of 0.1 produces approximately 10 frames per second. Increasing this value slows the game; decreasing it speeds the game up.
retraso = 0.1
puntaje
int
Current score. Starts at 0. Increments by 10 each time the snake eats food. Resets to 0 on a wall collision or self-collision. Never exceeds puntajeAlto for the life of a session (because puntajeAlto is updated whenever puntaje surpasses it).
puntaje = 0
# After eating food:
puntaje += 10
# After a collision:
puntaje = 0
puntajeAlto
int
Session high score. Starts at 0. Updated whenever puntaje exceeds it after eating food. Never resets during the session — only a full program restart returns it to 0.
puntajeAlto = 0
# Updated in the food-collision branch:
if puntaje > puntajeAlto:
    puntajeAlto = puntaje
serpiente
turtle.Turtle
The snake head object. Created with turtle.Turtle() and configured with shape 'square' and color 'lightgreen'. Carries the custom attribute serpiente.direction (a string: 'up', 'down', 'left', 'right', or 'stop') which the direction functions and movimiento() read and write.
serpiente = turtle.Turtle()
serpiente.shape('square')
serpiente.color('lightgreen')
serpiente.penup()
serpiente.goto(0, 0)
serpiente.direction = 'stop'
comida
turtle.Turtle
The food item. Created with turtle.Turtle() and configured with shape 'circle' and color 'red'. Repositioned to a random coordinate within ±250 pixels each time the snake head comes within 20 pixels of it.
comida = turtle.Turtle()
comida.shape('circle')
comida.color('red')
comida.penup()
comida.goto(0, 100)
cuerpo
list
A list of turtle.Turtle objects representing the snake’s body segments. Each element is a dark green square turtle. A new segment is appended whenever the snake eats food. The entire list is cleared and each segment is hidden on a collision reset. The game loop propagates positions through the list each frame, from the tail toward the head.
cuerpo = []
# When food is eaten:
nuevoCuerpo = turtle.Turtle()
nuevoCuerpo.shape('square')
nuevoCuerpo.color('green')
nuevoCuerpo.penup()
cuerpo.append(nuevoCuerpo)
texto
turtle.Turtle
The score display object. Positioned at (0, -260), below the play area. Uses texto.write() to render the score string and texto.clear() to erase the previous string before writing a new one. The turtle cursor is hidden with texto.hideturtle() so only the text is visible.
texto = turtle.Turtle()
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"))
screen
turtle.Screen
The game window object. Created with turtle.Screen(). Configured with a 650×650 pixel size, black background, and tracer(0) to disable automatic redraws. Handles keyboard event registration via screen.listen() and screen.onkeypress(). screen.update() must be called at the start of each game loop iteration to flush pending drawing operations to the display.
screen = turtle.Screen()
screen.setup(650, 650)
screen.bgcolor('black')
screen.tracer(0)

Build docs developers (and LLMs) love