Open links in new tab
  1. Below is a simple implementation of the classic Snake game using the Pygame library. This code demonstrates basic game mechanics such as movement, collision detection, and score tracking.

    import pygame
    import time
    import random

    # Initialize Pygame
    pygame.init()

    # Screen dimensions
    WIDTH, HEIGHT = 800, 600

    # Colors
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)
    RED = (213, 50, 80)
    GREEN = (0, 255, 0)
    BLUE = (50, 153, 213)

    # Game settings
    SNAKE_BLOCK = 10
    SNAKE_SPEED = 15

    # Initialize screen
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Snake Game")

    # Clock for controlling the frame rate
    clock = pygame.time.Clock()

    # Fonts for displaying score
    font_style = pygame.font.SysFont("bahnschrift", 25)
    score_font = pygame.font.SysFont("comicsansms", 35)

    def display_score(score):
    """Display the current score."""
    value = score_font.render(f"Your Score: {score}", True, WHITE)
    screen.blit(value, [10, 10])

    def draw_snake(snake_block, snake_list):
    """Draw the snake on the screen."""
    for block in snake_list:
    pygame.draw.rect(screen, GREEN, [block[0], block[1], snake_block, snake_block])

    def game_loop():
    """Main game loop."""
    game_over = False
    game_close = False

    # Initial position of the snake
    x1, y1 = WIDTH // 2, HEIGHT // 2
    x1_change, y1_change = 0, 0

    # Snake body and length
    snake_list = []
    length_of_snake = 1

    # Food position
    foodx = round(random.randrange(0, WIDTH - SNAKE_BLOCK) / 10.0) * 10.0
    foody = round(random.randrange(0, HEIGHT - SNAKE_BLOCK) / 10.0) * 10.0

    while not game_over:
    while game_close:
    screen.fill(BLACK)
    message = font_style.render("Game Over! Press C-Play Again or Q-Quit", True, RED)
    screen.blit(message, [WIDTH // 6, HEIGHT // 3])
    display_score(length_of_snake - 1)
    pygame.display.update()

    for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_q:
    game_over = True
    game_close = False
    if event.key == pygame.K_c:
    game_loop()

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    game_over = True
    if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT and x1_change == 0:
    x1_change = -SNAKE_BLOCK
    y1_change = 0
    elif event.key == pygame.K_RIGHT and x1_change == 0:
    x1_change = SNAKE_BLOCK
    y1_change = 0
    elif event.key == pygame.K_UP and y1_change == 0:
    y1_change = -SNAKE_BLOCK
    x1_change = 0
    elif event.key == pygame.K_DOWN and y1_change == 0:
    y1_change = SNAKE_BLOCK
    x1_change = 0

    # Check boundaries
    if x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:
    game_close = True

    x1 += x1_change
    y1 += y1_change

    screen.fill(BLACK)
    pygame.draw.rect(screen, BLUE, [foodx, foody, SNAKE_BLOCK, SNAKE_BLOCK])

    # Update snake's head position and body length
    snake_head = [x1, y1]
    snake_list.append(snake_head)
    if len(snake_list) > length_of_snake:
    del snake_list[0]

    # Check collision with itself
    for block in snake_list[:-1]:
    if block == snake_head:
    game_close = True

    draw_snake(SNAKE_BLOCK, snake_list)
    display_score(length_of_snake - 1)

    # Check collision with food
    if x1 == foodx and y1 == foody:
    foodx = round(random.randrange(0, WIDTH - SNAKE_BLOCK) / 10.0) * 10.0
    foody = round(random.randrange(0, HEIGHT - SNAKE_BLOCK) / 10.0) * 10.0
    length_of_snake += 1

    pygame.display.update()
    clock.tick(SNAKE_SPEED)

    pygame.quit()
    quit()

    # Start the game loop
    game_loop()
    Copied!
    Feedback
  2. python snake game - Python Tutorial

    Learn how to create a classic arcade game with Python and Pygame. Follow the steps to draw the snake, handle the events, and add the apple.

  3. How to Make a Snake Game in Python - The Python Code

    Snake is a classic arcade game where the player controls a line that grows in length, with the game's primary objective being to guide the snake towards consuming items and avoid colliding with itself. In this comprehensive guide, we will walk through each step involved in creating this game from scratch. We will explore the key aspects of Pygame, ...
    See more on thepythoncode.com
  4. Snake Game in Python - Using Pygame module

    Jul 23, 2025 · Creating a snake game can be taken as a challenge while learning Python or Pygame. It is one of the best beginner-friendly projects that every novice …

  5. Build Snake Game in Python Using Turtle Module

    • See More

    Jun 26, 2025 · In this tutorial, I’ll walk you through creating a complete Snake game from scratch using Python’s Turtle module. The game includes all the essential features, a snake that grows when it eats …

  6. Implementing the Snake Game in Python - Towards Data Science

    • See More

    Feb 10, 2026 · In this tutorial, we have successfully implemented the snake game in Python. We have used our understanding of Python basics, such as defining and calling functions, using lists and …

  7. Snake Game in Python: Pygame Tutorial with Step-by-Step Code

    Jan 9, 2026 · Learn how to build a classic Snake game in Python using the Pygame library. This step-by-step tutorial covers game loops, movement, and collision detection for beginners.

  8. Python - Snake Game Using pygame Module - Online …

    In this tutorial, we'll learn to develop a simple snake game with start screen, bordered play area and working.

  9. Pythonade - Building a Snake Game with Python and Pygame

    In this tutorial, we'll build a classic Snake game using Python's Pygame library, focusing on vector graphics for a clean, retro aesthetic. We'll break this down into four progressive stages, each adding …

  10. Python Snake Game Tutorial with Pygame

    Jun 25, 2024 · Learn to create a classic Snake game using Python and Pygame with this step-by-step tutorial. Perfect for beginners in game development

  11. Pygame Tutorial - Snake Tutorial Pygame - Tech with Tim

    This series will show you how to create the famous Snake Game using python and pygame. We will use a standard grid system to represent the play area and move objects around the grid.

  12. People also ask
    Loading
    Unable to load answer