- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
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 pygameimport timeimport random# Initialize Pygamepygame.init()# Screen dimensionsWIDTH, HEIGHT = 800, 600# ColorsWHITE = (255, 255, 255)BLACK = (0, 0, 0)RED = (213, 50, 80)GREEN = (0, 255, 0)BLUE = (50, 153, 213)# Game settingsSNAKE_BLOCK = 10SNAKE_SPEED = 15# Initialize screenscreen = pygame.display.set_mode((WIDTH, HEIGHT))pygame.display.set_caption("Snake Game")# Clock for controlling the frame rateclock = pygame.time.Clock()# Fonts for displaying scorefont_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 = Falsegame_close = False# Initial position of the snakex1, y1 = WIDTH // 2, HEIGHT // 2x1_change, y1_change = 0, 0# Snake body and lengthsnake_list = []length_of_snake = 1# Food positionfoodx = round(random.randrange(0, WIDTH - SNAKE_BLOCK) / 10.0) * 10.0foody = round(random.randrange(0, HEIGHT - SNAKE_BLOCK) / 10.0) * 10.0while 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 = Truegame_close = Falseif event.key == pygame.K_c:game_loop()for event in pygame.event.get():if event.type == pygame.QUIT:game_over = Trueif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT and x1_change == 0:x1_change = -SNAKE_BLOCKy1_change = 0elif event.key == pygame.K_RIGHT and x1_change == 0:x1_change = SNAKE_BLOCKy1_change = 0elif event.key == pygame.K_UP and y1_change == 0:y1_change = -SNAKE_BLOCKx1_change = 0elif event.key == pygame.K_DOWN and y1_change == 0:y1_change = SNAKE_BLOCKx1_change = 0# Check boundariesif x1 >= WIDTH or x1 < 0 or y1 >= HEIGHT or y1 < 0:game_close = Truex1 += x1_changey1 += y1_changescreen.fill(BLACK)pygame.draw.rect(screen, BLUE, [foodx, foody, SNAKE_BLOCK, SNAKE_BLOCK])# Update snake's head position and body lengthsnake_head = [x1, y1]snake_list.append(snake_head)if len(snake_list) > length_of_snake:del snake_list[0]# Check collision with itselffor block in snake_list[:-1]:if block == snake_head:game_close = Truedraw_snake(SNAKE_BLOCK, snake_list)display_score(length_of_snake - 1)# Check collision with foodif x1 == foodx and y1 == foody:foodx = round(random.randrange(0, WIDTH - SNAKE_BLOCK) / 10.0) * 10.0foody = round(random.randrange(0, HEIGHT - SNAKE_BLOCK) / 10.0) * 10.0length_of_snake += 1pygame.display.update()clock.tick(SNAKE_SPEED)pygame.quit()quit()# Start the game loopgame_loop()Copied!✕Copy 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.
How to Make a Snake Game in Python - The Python Code
See more on thepythoncode.comSnake 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, ...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 …
Build Snake Game in Python Using Turtle Module
Implementing the Snake Game in Python - Towards Data Science
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.
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.
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 …
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
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.
- People also ask