VOOZH about

URL: https://www.geeksforgeeks.org/python/create-a-snake-game-using-turtle-in-python/

⇱ Create a Snake-Game using Turtle in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Create a Snake-Game using Turtle in Python

Last Updated : 18 Feb, 2026

The Snake Game is a classic arcade game first released in 1976 by Gremlin Industries and published by Sega. The goal is simple to control the snake using arrow keys, collect food to grow longer and avoid hitting the walls or yourself. We’ll build this game in Python using the following modules:

  • Turtle: A built-in Python library that provides a virtual canvas for drawing shapes and animations.
  • Time: Used to control game speed.
  • Random: Helps generate random positions for food on the screen.

Steps to Implement Snake Game

Step 1: Setup Modules, Window, Snake, Food and Scoreboard

In this step, we initialize the game environment and create the main objects required for the Snake Game.

  • Modules: Import turtle for graphics, random for food position and appearance, and time for controlling game speed.
  • Game Variables: Initialize delay to control snake speed, score to track current points, high_score to store the highest score, and run to control the main game loop safely.
  • Window: Create a 600×600 game window with a light blue background (#add8e6) and disable automatic animation using tracer(0) for smooth manual updates.
  • Snake Head: Create a white square turtle positioned at the center (0, 0) and set its initial direction to "Stop".
  • Food: Create a turtle with random shape and color and place it at position (0, 100).
  • Scoreboard: Create a hidden turtle to display the current score and high score at the top of the window.

Step 2: Define Snake Movement and Keyboard Controls

In this step, we create functions to control the snake’s direction and movement using keyboard keys.

Direction Functions: These functions change the snake’s direction when an arrow key is pressed. Each function also prevents the snake from moving in the opposite direction instantly to avoid self-collision.

  1. up() changes direction to "up" if the snake is not moving down
  2. down() changes direction to "down" if the snake is not moving up
  3. left() changes direction to "left" if the snake is not moving right
  4. right() changes direction to "right" if the snake is not moving left

Move Function: This function moves the snake head by 20 pixels in the current direction.

Key Binding: The sc.listen() function enables keyboard input and sc.onkeypress() connects arrow keys to their respective direction functions.

Step 3: Implement Main Game Loop and Game Logic

In this step, we create the main loop that runs the game continuously, handles collisions, updates the score and moves the snake.

  • Main Loop: Runs continuously using the run variable and updates the screen.
  • Wall Collision: If the snake hits the boundary, the snake resets to the center, body clears, and score resets.
  • Food Collision: When the snake touches the food, the food moves to a new random position, a new body segment is added, and the score increases.
  • Body Movement: Each body segment follows the position of the segment in front of it.
  • Self Collision: If the snake hits its own body, the game resets.
  • Speed Control: time.sleep(d) controls the game speed.
  • Error Handling: try-except prevents errors when the window is closed.

Output

The game window opens showing the snake, food and score and the snake moves based on arrow key input while the score updates during gameplay.

Related Articles:

Comment