Creating A Snake Game With Python: A Step-By-Step Guide
Muhammad Rayyan
3 min
Nov. 22, 2023
77 View(s)
Python is a well-liked and simple to learn programming language. In this tutorial, we'll go over how to create a classic Snake game using the pygame library. This is how to make a custom interactive Snake game of your own.
Follow The Code As Given
# Step 1: Import Libraries
import pygame
import time
import random
# Step 2: Initialize Pygame
pygame.init()
# Step 3: Define 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)
# Step 4: Set Up the Display
dis_width = 800
dis_height = 600
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('Snake Game')
# Step 5: Set Up the Clock
clock = pygame.time.Clock()
# Step 6: Set Snake Block Size and Speed
snake_block = 10
snake_speed = 15
# Step 7: Set Up Font Style
font_style = pygame.font.SysFont(None, 50)
# Step 8: Define Snake Drawing Function
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, green, [x[0], x[1], snake_block, snake_block])
# Step 9: Define Message Display Function
def message(msg, color):
mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width / 6, dis_height / 3])
# Step 10: Define the Game Loop
def gameLoop():
game_over = False
game_close = False
# Step 11: Initialize Snake Position and Direction
x1 = dis_width / 2
y1 = dis_height / 2
x1_change = 0
y1_change = 0
# Step 12: Initialize Snake and Food
snake_List = []
Length_of_snake = 1
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
# Step 13: Main Game Loop
while not game_over:
# Step 14: Handle Game Over State
while game_close:
dis.fill(blue)
message("You Lost! Press C-Play Again or Q-Quit", red)
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:
gameLoop()
# Step 15: Handle User Input
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:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
# Step 16: Check for Collisions and Update Snake
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
dis.fill(blue)
pygame.draw.rect(dis, white, [foodx, foody, snake_block, snake_block])
snake_head = []
snake_head.append(x1)
snake_head.append(y1)
snake_List.append(snake_head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_head:
game_close = True
# Step 17: Draw Snake and Update Display
our_snake(snake_block, snake_List)
pygame.display.update()
# Step 18: Handle Food Consumption
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
Length_of_snake += 1
# Step 19: Set Game Speed
clock.tick(snake_speed)
# Step 20: Quit Pygame
pygame.quit()
quit()
# Step 21: Run the Game Loop
gameLoop()
We have created a simple Snake game using Python and the pygame library. Feel free to experiment with the code and add your own features to enhance the game.