Let’s learn how to make a Snake game in Python using Pygame! You’ll see the snake grow when it eats food and learn how to stop it from crashing.
Pygame makes it easy to handle the graphics and the game’s rules. It’s a fun way to start coding games, even if you’re new!
Why does building a snake game in Python need Pygame?
Pygame is a powerful Python library designed for game development. It provides easy-to-use modules for handling graphics, sounds, and user inputs. With Pygame, you can create interactive games with just a few lines of code

Let’s see how to Set Up Pygame :
Note: Before starting Snake game in Python using Pygame, must install the Pygame in your system, type the below command in your terminal.
"pip install pygame"
If this does not work then try this one–
"py -m pip install -U pygame --user"

Snake Game in Python Source Code
import pygame import time import random # Initialize pygame pygame.init() # Game window dimensions WIDTH, HEIGHT = 600, 400 # Colors WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (213, 50, 80) BLACK = (0, 0, 0) # Snake block size BLOCK_SIZE = 10 # Set up display screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Snake Game") # Clock for controlling game speed clock = pygame.time.Clock() SNAKE_SPEED = 15 # Font font = pygame.font.SysFont("bahnschrift", 25) def draw_snake(snake_body): for block in snake_body: pygame.draw.rect(screen, GREEN, [block[0], block[1], BLOCK_SIZE, BLOCK_SIZE]) def display_message(msg, color, x, y): text = font.render(msg, True, color) screen.blit(text, [x, y]) def game_loop(): game_over = False game_close = False x, y = WIDTH // 2, HEIGHT // 2 x_change, y_change = 0, 0 snake_body = [] snake_length = 1 food_x = random.randrange(0, WIDTH - BLOCK_SIZE, BLOCK_SIZE) food_y = random.randrange(0, HEIGHT - BLOCK_SIZE, BLOCK_SIZE) while not game_over: while game_close: screen.fill(BLACK) display_message("Game Over! Press Q-Quit or C-Play Again", RED, WIDTH // 6, HEIGHT // 3) 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 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_change = -BLOCK_SIZE y_change = 0 elif event.key == pygame.K_RIGHT: x_change = BLOCK_SIZE y_change = 0 elif event.key == pygame.K_UP: y_change = -BLOCK_SIZE x_change = 0 elif event.key == pygame.K_DOWN: y_change = BLOCK_SIZE x_change = 0 # Check boundaries if x >= WIDTH or x < 0 or y >= HEIGHT or y < 0: game_close = True x += x_change y += y_change screen.fill(BLACK) pygame.draw.rect(screen, RED, [food_x, food_y, BLOCK_SIZE, BLOCK_SIZE]) snake_body.append([x, y]) if len(snake_body) > snake_length: del snake_body[0] # Check collision with itself for block in snake_body[:-1]: if block == [x, y]: game_close = True draw_snake(snake_body) pygame.display.update() # Eating food if x == food_x and y == food_y: food_x = random.randrange(0, WIDTH - BLOCK_SIZE, BLOCK_SIZE) food_y = random.randrange(0, HEIGHT - BLOCK_SIZE, BLOCK_SIZE) snake_length += 1 clock.tick(SNAKE_SPEED) pygame.quit() quit() game_loop()
Conclusion:
And that’s it! You’ve just built a fun Snake game in Python using Pygame. Now you can try adding your features, like new levels or different speeds. Keep practicing, and you’ll get better at making games in no time!
Thanks for visiting codehelping.com. For more Python projects, visit here.