Snake Game Using Python With Source Code
Introduction:
In this project, we have created a snake game using a python module named “Pygame”. Basically, in this game, the user will control the movement of the snake through the keyboard arrows and direct the snake in the direction of food, as the snake eats the food the size of the snake will get increase. While playing the game if the snake hits the boundaries or the snake head hits its own body then the game will get over and accordingly the score will get displayed.
Installation:
To work with this project you need to install pygame module via pip installation.
To install the module open your terminal and run the command below:
$ python -m pip install pygame
Source Code:
# Importing the libraries codewithcurious.com
import pygame
import time
import random
# Initializing the pygame
pygame.init()
# Dimension of window
width = 600
height = 400
# Creating the game window
screen = pygame.display.set_mode((width, height))
# Setting th Title and icon
pygame.display.set_caption('Snake Game')
# Frames per second controller
c = pygame.time.Clock()
snake_block = 10
snake_speed = 15
# Font style
font_style = pygame.font.SysFont("calibri", 50)
score_font = pygame.font.SysFont("calibri", 20)
# function to display the score
def Your_score(score):
value = score_font.render("Your Score: " + str(score), True, (0, 0, 0))
screen.blit(value, [0, 0])
# Function to draw snakes
def snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(screen, (0, 255, 0), [x[0], x[1], snake_block, snake_block])
# Function to print the message
def message(msg, color):
mesg = font_style.render(msg, True, color)
screen.blit(mesg, [width / 6, height / 3])
# Function for game loop
def gameLoop():
game_over = False
game_close = False
x1 = width / 2
y1 = height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
# Defining food parameters
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
# Game Loop
running = True
while running:
while game_close == True:
screen.fill((0, 0, 0))
message("You Lost!", (255, 0, 0))
pygame.display.update()
#Loop for events
for event in pygame.event.get():
# Quit event
if event.type == pygame.QUIT:
running = False
#Keyboard arrow event
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
#Setting the boundaries
if x1 >= width or x1 < 0 or y1 >= height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
screen.fill((255, 255, 255))
#Drawing the food
pygame.draw.rect(screen, (0, 0, 0), [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 snake to not hit it's own body
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
snake(snake_block, snake_List)
Your_score(Length_of_snake - 1)
pygame.display.update()
#Checking same coordinates
if x1 == foodx and y1 == foody:
#Making to appear food at random position
foodx = round(random.randrange(0, width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, height - snake_block) / 10.0) * 10.0
#increasing the length of snake
Length_of_snake += 1
#setting the frames per second
c.tick(snake_speed)
#quit event
pygame.quit()
quit()
gameLoop()
Explanation:
First, we will import the “pygame” module using the import keyboard. If it’s not there in your system then follow the particular command “pip install pygame” in your cmd. After importing the next step is to initialize the pygame with the .init() method, this is the most important step to execute.
→For a game, the basic need is to create a game window and this is the next step to be followed in this project with the help of “display.set_mode((width, height))” [In this, the parameters describe the size of the window]. With the help of “.fill(color)” we will fill the specific color in our window.
→ To make it attentive, we have set the title and icon with the help of “display.set_caption()” and “display.set_icon()” respectively for the game window.
→ Now, we have set the frames per second to be shown with the help of “pygame.time.Clock()” which is available in the “time” module in python
→ We have initialized the two variables i.e. snake_block and snake_speed. Since to add any text we will need a font type and its size and that will be our next step via “pygame. font.SysFont(“Font name”,” size”)”, this function is provided by the “Sys” module
→ Now onwards, every step of this program is a part of our game and so to execute it will need a loop, and in game development, the theory works like that. So we will need a game loop and each step is known as an “event” under this, so we will execute each event under this game loop
→ For this loop, will make use of a while loop in which with the help of “pygame.event.get()”, we will run a for loop to see the events running. With the help of the “if condition” we will check the type of event by “.type()” and then we will execute the statements according to our need
→ Firstly, we have checked whether the event type is quit or not, if it is then close the window with the help of the “running” variable by making it false
→ Now the main element of this particular game is a snake and for this game, a rectangle will be considered as a part of the snake and for drawing so will make use of “pygame.draw.rect(surface, color,[left, top, width, height])”, in which surface is the game window and color will be in the format of RGB (red, green, blue)
→ The next most important step is to move the snake and for doing so we will use the “KEYDOWN” event, that is whenever the user will press down the keyboard arrows then due to that the snake will follow a particular direction and for doing so we have checked the event type under the if condition as “pygame. KEYDOWN”, and under that, we have checked the directions with the help of “K_LEFT”, “K_RIGHT”, “K_UP”, “K_DOWN”.If we will see correctly then this movement’s basic logic is to increase and decrease the values of x and y coordinates according to the directions
→ To keep all of this information in an updated way for our game window we have used “pygame. the display.update()”. This method will make sure to add up every process change.
→ In the next step, we have to set the boundaries of our game and by if condition we are doing so, we will just provide our ending coordinates as the conditions and give the event loop the command to over the game
→ The next step is to draw the food and we are again considering a rectangle as the food and will follow the same process as before. Now the logic is if the snake eats the food then the length of the snake should increase and the food should appear randomly at another location. For doing so, first of all, we will check the coordinates of the snake and food if they are equal then the food will again appear randomly at any position, and with the help of the “randrange” function in the “random” module and also will increase the length of snake by 1
→ The final step is to declare the score and for that, we have defined a function in which firstly we will render the value by “.render()”. Then we will draw it on the screen by the “.blit(value, color)” method.
This is how this pygame-based project works!!
Output:
Find More Projects
Complain Management using Python with a Graphical User Interface (GUI) Introduction: The Complain Management using Python program designed to manage complaints effectively …
COVID 19 Hospital Management Using Python [Django Framework] Introduction: The COVID-19 Hospital Management is a Python-based application that tracks web applications for Hospitals. …
Drawing Ganesha Using Python Turtle Graphics[Drawing Ganapati Using Python] Introduction In this blog post, we will learn how to draw Lord Ganesha …
Contact Management System in Python with a Graphical User Interface (GUI) Introduction: The Contact Management System is a Python-based application designed to …
KBC Game using Python with Source Code Introduction : Welcome to this blog post on building a “Kaun Banega Crorepati” (KBC) game …
Basic Logging System in C++ With Source Code Introduction : It is one of the most important practices in software development. Logging …