Python Code for Snake Game: If you’re learning Python and want to build something fun and interactive, creating a Snake game is one of the best beginner-friendly projects. It helps you understand game loops, event handling, and graphics rendering using the Pygame library.

In this tutorial, you will learn how to create a fully functional Snake game in Python step by step. By the end, you’ll have your own working game and a solid understanding of basic game development concepts.
| Article Name | Python Code for Snake Game | Step-by-Step Guide with Pygame (Beginner Tutorial) |
| Publish Date | 9/5/2026 |
| What is | Sanake Game Code |
| Requirements | Python installed (3.x recommended) Pygame library installed |
| Author | Codeswithsam |
Requirements
Before starting, make sure you have the following:
- Python installed (3.x recommended)
- Pygame library installed
Install Pygame
Run the following command in your terminal:
pip install pygame
Snake Game Logic Overview
The Snake game works on simple logic:
- The snake moves continuously in one direction
- The player controls direction using arrow keys
- The snake grows when it eats food
- The game ends if the snake hits the wall or itself
Python Snake Game Code
import pygame
import time
import random
# Initialize pygame
pygame.init()
# Colors
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
# Display settings
width = 600
height = 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Snake Game by CodesWithSam')
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
def score_display(score):
value = score_font.render("Score: " + str(score), True, yellow)
screen.blit(value, [0, 0])
def draw_snake(block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, black, [x[0], x[1], block, block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, [width / 6, height / 3])
def game_loop():
game_over = False
game_close = False
x = width / 2
y = height / 2
x_change = 0
y_change = 0
snake_list = []
snake_length = 1
food_x = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
food_y = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close:
screen.fill(blue)
message("You Lost! Press Q-Quit or C-Play Again", red)
score_display(snake_length - 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
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -snake_block
y_change = 0
elif event.key == pygame.K_RIGHT:
x_change = snake_block
y_change = 0
elif event.key == pygame.K_UP:
y_change = -snake_block
x_change = 0
elif event.key == pygame.K_DOWN:
y_change = snake_block
x_change = 0
# Check wall collision
if x >= width or x < 0 or y >= height or y < 0:
game_close = True
x += x_change
y += y_change
screen.fill(blue)
# Draw food
pygame.draw.rect(screen, green, [food_x, food_y, snake_block, snake_block])
# Snake logic
snake_head = []
snake_head.append(x)
snake_head.append(y)
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
# Check self collision
for block in snake_list[:-1]:
if block == snake_head:
game_close = True
draw_snake(snake_block, snake_list)
score_display(snake_length - 1)
pygame.display.update()
# Food collision
if x == food_x and y == food_y:
food_x = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
food_y = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
snake_length += 1
clock.tick(snake_speed)
pygame.quit()
quit()
# Start the game
game_loop()

How to Run the Snake Game
Follow these steps:
- Install Python and Pygame
- Copy the code into a file named:
snake_game.py
- Open terminal or command prompt
- Run the file:
python snake_game.py
- Use arrow keys to control the snake 🎮
Final Thoughts
Building a Snake game in Python is a fun and practical way to improve your programming skills. It teaches you core concepts like loops, conditions, and event handling while giving you a playable result. If you’re just starting your coding journey, this project is a great addition to your portfolio and can be easily expanded with more features.
Keep practicing and building projects like this to become a better developer. Stay tuned to codeswithsam.com for more beginner-friendly coding tutorials and projects!
Happy Coding! 🚀
Important Links
| Our Website | Codeswithsam.com |
| Join Telegram | Click Here |
If we made a mistake or any confusion, please drop a comment to reply or help you in easy learning.
Thanks! 🙏 for visiting Codeswithsam.com ! Join telegram (link available in bottom) for source code files , pdf and
Any Promotion queries 👇
info@codeswithsam.com
