How to Create Snake in Pygame — Part 2

Brock Byrd
Python in Plain English
4 min readJun 10, 2021

--

Photo by Alfonso Castro on Unsplash

I left off my last blog post about creating Snake in Pygame with moving your player around the display screen. The next steps will be to create boundaries for the player, create the food, and make the snake grow on food consumption. When the snake runs out of bounds the game should end because it has hit a “wall” that ends the game.

import pygame
import time
pygame.init()
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
display_width = 800
display_height = 600
display = pygame.display.set_mode((display_height, display_width))x1 = display_width/2
y1 = display_height/2
snake_block = 10x1_change = 0
y1_change = 0
clock = pygame.time.Clock()
snake_speed = 30
pygame.display.update()game_over = Falsefont_style = pygame.font.SysFont(None, 50)def message(msg, color):
mesg = font_style.render(msg, True, color)
display.blit(mesg, [diplay_width/2, display_height/2])
while not game_over:
for event in pygame.event.get():
if event == pyagme.QUIT():
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -10
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = 10
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -10
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = 10
x1_change = 0
if x1>=display_width or x1<0 or y1>=display_height or y1<0:
game_over = True
x1 += x1_change
y1 += y1_change
display.fill(white)
pygame.draw(display, black, [x1, y1, snake_block, snake_block])
pygame.display.update()
clock.tick(snake_speed)message("You Lost", red)
pygame.display.update()
time.sleep(2)
pygame.quit()
quit()

You may notice some things have been changed around with variables, all I have done is created reusable variables to have my application be flexible if I wanted to change the display size, now all I have to do is change two variables rather than go throughout the entire application and change multiple things.

In the if statement that I added, I checked if the position of the snake was larger than the display size or smaller than 0, and if true set the game_over variable to True therefore ending the game. When the game ends, it will display a message saying “You Lost”, the next step will to add the food.

import pygame
import random
import time
pygame.init()
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
display_width = 800
display_height = 600
display = pygame.display.set_mode((display_height, display_width)snake_block = 10clock = pygame.time.Clock()
snake_speed = 30
pygame.display.update()game_over = Falsefont_style = pygame.font.SysFont(None, 50)def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(display, black, [x[0], x[1], snake_block, snake_block])
def message(msg, color):
mesg = font_style.render(msg, True, color)
display.blit(mesg, [diplay_width/2, display_height/2])
def gameLoop():
game_over = False
game_close = Flase
x1 = display_width/2
y1 = display_height/2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1

foodx=round(random.randrange(0,display_width-snake_block)/10.0)*10
foody=round(random.randrange(0,display_width-snake_block)/10.0)10
while not game_over: while game_close = True:
display.fill(white)
message("You Lost! Press Q-Quit or C-Play Again", 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()

for event in pygame.event.get():
if event == pyagme.QUIT():
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -10
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = 10
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -10
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = 10
x1_change = 0
if x1>=display_width or x1<0 or y1>=display_height or y1<0:
game_over = True
x1 += x1_change
y1 += y1_change
display.fill(white)
pygame.draw(display, blue, [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
our_snake(snake_block, snake_List)
pygame.display.update() if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, display_width-snake_block)/10)*10
foody = round(random.randrange(0, display_height-snake_block)/10)*10
Length_of_snake += 1
clock.tick(snake_speed)pygame.quit()
quit()
gameLoop()

Creating the food, you will need to take the length of the snake and add one to it, you can do this by creating a rectangle every time the snake goes over the food. Above that is what I have done, I created a method that keeps track of the length of the snake and draws a rectangle for every length of the snake. The if statement at the bottom is what checks if the head of the snake has run over the food or not, and if it has it will generate new food somewhere randomly and increase the length of the snake.

I have also created a method that keeps track if the game is over or has been closed. This method also allows a user to replay the game with a keypress, rather than the window closing on the game closing. You now have a functioning snake game that can be replayed on a keypress and doesn’t have to be reopened after the game has ended.

If you would like to display a score on the screen, you can create a method that keeps track of the length of the snake to see how many pieces of food the user has eaten and display that minus one to show the length minus the snakehead.

These are the resources I used to help me create this small application:

More content at plainenglish.io

--

--