Snake Game In Python Code Free Download

The Snake Game In Python Code is written in Python Language. In this Snake Game Python Code is the way to enhance your skills and talent in creating and designing how to create a snake game using Python.

A Snake Game in Python Tkinter is a simple and clean GUI provided for easy gameplay. The gameplay design is so simple that the user won’t find it difficult to use and understand.

Different images are used in the development of this Snake Game Python project.

Snake Game in Python: Project Information

Project Name: Snake Game In Python
Language/s Used:Python (GUI) Based
Python version (Recommended):2.x or 3.x
Database:None
Type:Python App
Developer:IT SOURCECODE
Updates:0

This Snake Game Using Python also includes the downloadable Snake Game using Python Source Code for free and easy to download, just find the downloadable source code below to click and start downloading with no worries.

Anyway, if you want to level up your knowledge in programming especially games in Python, try this new article I’ve made for you Code For Game in Python: Python Game Projects With Source Code.

Now sit back and relax because I will show you how to make this project, to start creating this Snake Game In Python Code make sure that you have PyCharm IDE installed on your computer.

By the way, if you are new to Python programming and don’t know what Python IDE to use, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS that will suit you.

I also have here How to Download and Install the Latest Version of Python on Windows.

How to Create a Simple Snake Game in Python? A step-by-step Guide with Source Code

Time needed: 5 minutes

Below is the step-by-step guide on how to perform the Code.

  • Step 1: Create a project name.

    First, open Pycharm IDE and then create a “project name.” After creating a project name click the “create” button.Snake Game Python Code Project Name

  • Step 2: Create a python file.

    Second, after creating a project name, “right-click” your project name and then click “new.” After that click the “python file“.
    Snake Game Python Python File

  • Step 3: Name your Python file.

    Third, after creating a Python file, Name your Python file after that click “enter“.
    Snake Game Python Python File Name

  • Step 4: The actual code of snake game using Python

    You are free to copy the code given below and download the full source code below.

Code Explanation

1. The Code Give Below Is For Importing Modules

Code:

import pygame
import sys
import random

Explanation:

In the code given which is importing all the modules to be called in executing the game.

2. The Code Given Below Is For the Declaration of Variables

Code:

pygame.init()
FPS = 15
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
VELOCITY = 10
SNAKE_WIDTH = 15
APPLE_SIZE = 20
TOP_WIDTH = 40
small_font = pygame.font.SysFont('Courier New', 25)
medium_font = pygame.font.SysFont('Courier New', 20, True)
large_font = pygame.font.SysFont('Courier New', 40, True, True)
clock = pygame.time.Clock()

canvas = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Snake Game')
snake_img = pygame.image.load('head.png')
apple_img = pygame.image.load('apple2.png')
tail_img = pygame.image.load('tail1.png')
apple_img_rect = apple_img.get_rect()

Explanation:

The code given is a declaration of the variables to be called in executing the process of the game.

3. The Code Given Below Is For The Module At the start of The Game

Code:

def start_game():
    canvas.fill(BLACK)
    start_font1 = large_font.render("Welcome to snake game", True, BLUE)
    start_font2 = medium_font.render("Play Game", True, BLACK, GREEN)
    start_font3 = medium_font.render("Instructions", True, BLACK, GREEN)
    start_font4 = medium_font.render("Quit", True, RED, GREEN)

    start_font1_rect = start_font1.get_rect()
    start_font2_rect = start_font2.get_rect()
    start_font3_rect = start_font3.get_rect()
    start_font4_rect = start_font4.get_rect()

    start_font1_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2 - 100)
    start_font2_rect.center = (WINDOW_WIDTH/2 + 100, WINDOW_HEIGHT/2 + 50)
    start_font3_rect.center = (WINDOW_WIDTH/2 + 100, WINDOW_HEIGHT / 2 + 100)
    start_font4_rect.center = (WINDOW_WIDTH/2 + 100, WINDOW_HEIGHT/2 + 150)

    canvas.blit(start_font1, start_font1_rect)
    canvas.blit(start_font2, start_font2_rect)
    canvas.blit(start_font3, start_font3_rect)
    canvas.blit(start_font4, start_font4_rect)
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    gameloop()
                if event.key == pygame.K_q:
                    pygame.quit()
                    sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if x > start_font3_rect.left and x < start_font3_rect.right:
                    if y > start_font3_rect.top and y < start_font3_rect.bottom:
                        start_inst(start_font1, start_font1_rect)
                if x > start_font2_rect.left and x < start_font2_rect.right:
                    if y > start_font2_rect.top and y < start_font2_rect.bottom:
                        gameloop()
                if x > start_font4_rect.left and x < start_font4_rect.right:
                    if y > start_font4_rect.top and y < start_font4_rect.bottom:
                        pygame.quit()
                        sys.exit()

        pygame.display.update()




    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

Explanation:

In this module which is the interface of the starting a snake game.

This will be the output

Snake-Game-In-Python-Code-First-Output
Snake-Game-In-Python-Code-First-Output

4. The Code Given Below Is For The Instruction

Code:

def start_inst(start_font1, start_font1_rect):
    canvas.fill(BLACK)
    canvas.blit(start_font1, start_font1_rect)
    start_inst1 = small_font.render("--> Do not cross the edges, else you will be dead", True, BLUE)
    start_inst2 = small_font.render("--> Keep eating the red apples", True, BLUE)
    start_inst3 = small_font.render("--> Do not cross over yourself", True, BLUE)
    start_inst4 = small_font.render("--> Keep playing......", True, BLUE)
    start_inst5 = medium_font.render("<<BACK", True, RED, YELLOW)
    start_inst5_rect = start_inst5.get_rect()
    start_inst5_rect.center = (WINDOW_WIDTH-100, WINDOW_HEIGHT - 100)

    canvas.blit(start_inst1, (WINDOW_WIDTH/8, WINDOW_HEIGHT/2))
    canvas.blit(start_inst2, (WINDOW_WIDTH/8, WINDOW_HEIGHT/2 + 30))
    canvas.blit(start_inst3, (WINDOW_WIDTH/8, WINDOW_HEIGHT/2 + 60))
    canvas.blit(start_inst4, (WINDOW_WIDTH/8, WINDOW_HEIGHT/2 + 90))
    canvas.blit(start_inst5, start_inst5_rect)
    pygame.display.update()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if x > start_inst5_rect.left and x < start_inst5_rect.right:
                    if y > start_inst5_rect.top and y < start_inst5_rect.bottom:
                        start_game()
        pygame.display.update()

Explanation:

In this module which is the interface of the game instructions.

This will be the output

Snake-Game-In-Python-Code-Second-Output
Snake-Game-In-Python-Code-Second-Output

5. The Code Given Below Is For Game Over

Code:

def gameover():
    #canvas.fill(BLACK)

    font_gameover1 = large_font.render('GAME OVER', True, GREEN)
    font_gameover2 = medium_font.render("Play Again", True, RED, YELLOW)
    font_gameover3 = medium_font.render("Quit", True, RED, YELLOW)

    font_gameover1_rect = font_gameover1.get_rect()
    font_gameover2_rect = font_gameover2.get_rect()
    font_gameover3_rect = font_gameover3.get_rect()


    font_gameover1_rect.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2 - 100)
    font_gameover2_rect.center = (WINDOW_WIDTH / 2 + 150, WINDOW_HEIGHT / 2 + 20)
    font_gameover3_rect.center = (WINDOW_WIDTH / 2 + 150, WINDOW_HEIGHT / 2 + 70)


    canvas.blit(font_gameover1, font_gameover1_rect)
    canvas.blit(font_gameover2, font_gameover2_rect)
    canvas.blit(font_gameover3, font_gameover3_rect)
    pygame.display.update()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                if x > font_gameover2_rect.left and x < font_gameover2_rect.right:
                    if y > font_gameover2_rect.top and y < font_gameover2_rect.bottom:
                        gameloop()
                if x > font_gameover3_rect.left and x < font_gameover3_rect.right:
                    if y > font_gameover3_rect.top and y < font_gameover3_rect.bottom:
                        pygame.quit()
                        sys.exit()


        pygame.display.update()

Explanation:

In this module which is the code for the game over.

This will be the output

Snake-Game-In-Python-Code-Third-Output
Snake-Game-Third-Output

6. The Code Given Below Is For The Movement Of The Snake

Code:

def snake(snakelist, direction):

    if direction == 'right':
        head = pygame.transform.rotate(snake_img, 270)
        tail = pygame.transform.rotate(tail_img, 270)
    if direction == 'left':
        head = pygame.transform.rotate(snake_img, 90)
        tail = pygame.transform.rotate(tail_img, 90)
    if direction == 'up':
        head = pygame.transform.rotate(snake_img, 0)
        tail = pygame.transform.rotate(tail_img, 0)
    if direction == 'down':
        head = pygame.transform.rotate(snake_img, 180)
        tail = pygame.transform.rotate(tail_img, 180)

    canvas.blit(head, snakelist[-1])
    canvas.blit(tail, snakelist[0])

    for XnY in snakelist[1:-1]:
        pygame.draw.rect(canvas, BLUE, (XnY[0], XnY[1], SNAKE_WIDTH, SNAKE_WIDTH))

Explanation:

In this module which is the movement of the snake that is being used by the user.

7. The Code Given Below Is For The “Pause” Function

Code:

def game_paused():
    # canvas.fill(BLACK)
    paused_font1 = large_font.render("Game Paused", True, RED)
    paused_font_rect1 = paused_font1.get_rect()
    paused_font_rect1.center = (WINDOW_WIDTH/2, WINDOW_HEIGHT/2)
    canvas.blit(paused_font1, paused_font_rect1)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    pygame.quit()
                    sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                pause_xy = event.pos
                if pause_xy[0] > (WINDOW_WIDTH - 50) and pause_xy[0] < WINDOW_WIDTH:
                    if pause_xy[1] > 0 and pause_xy[1] < 50:
                        return
        pygame.display.update()

Explanation:

In this module which is the game paused function.

This will be the output

Snake-Game-In-Python-Code-Fourth-Output
Snake-Game-Fourth-Output

8. The code of the snake game in Python for the Game loop Function

Code:

def gameloop():

    while True:

        LEAD_X = 0
        LEAD_Y = 100
        direction = 'right'
        score = small_font.render("Score:0", True, YELLOW)
        APPLE_X = random.randrange(0, WINDOW_WIDTH - 10, 10)
        APPLE_Y = random.randrange(TOP_WIDTH, WINDOW_HEIGHT - 10, 10)
        snakelist = []
        snakelength = 3
        pause_font = medium_font.render('II', True, RED)


        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        if direction == 'right':
                            pass
                        else:
                            direction = 'left'
                    if event.key == pygame.K_RIGHT:
                        if direction == 'left':
                            pass
                        else:
                            direction = 'right'
                    if event.key == pygame.K_UP:
                        if direction == 'down':
                            pass
                        else:
                            direction = 'up'
                    if event.key == pygame.K_DOWN:
                        if direction == 'up':
                            pass
                        else:
                            direction = 'down'
                if event.type == pygame.MOUSEBUTTONDOWN:
                    pause_xy = event.pos
                    if pause_xy[0] > (WINDOW_WIDTH - 50) and pause_xy[0] < WINDOW_WIDTH:
                        if pause_xy[1] > 0 and pause_xy[1] < 50:
                            game_paused()
            if direction == 'up':
                LEAD_Y -= VELOCITY
                if LEAD_Y < TOP_WIDTH:
                    gameover()
            if direction == 'down':
                LEAD_Y += VELOCITY
                if LEAD_Y > WINDOW_HEIGHT - SNAKE_WIDTH:
                    gameover()
            if direction == 'right':
                LEAD_X += VELOCITY
                if LEAD_X > WINDOW_WIDTH - SNAKE_WIDTH:
                    gameover()
            if direction == 'left':
                LEAD_X -= VELOCITY
                if LEAD_X < 0:
                    gameover()

            snakehead = []
            snakehead.append(LEAD_X)
            snakehead.append(LEAD_Y)
            snakelist.append(snakehead)

            snake_head_rect = pygame.Rect(LEAD_X, LEAD_Y, SNAKE_WIDTH, SNAKE_WIDTH)
            apple_rect = pygame.Rect(APPLE_X, APPLE_Y, APPLE_SIZE, APPLE_SIZE)


            if len(snakelist) > snakelength:
                del snakelist[0]
            for point in snakelist[:-1]:
                if point == snakehead:
                    gameover()

            canvas.fill(BLACK)

            snake(snakelist, direction)
            if snake_head_rect.colliderect(apple_rect):
                APPLE_X = random.randrange(0, WINDOW_WIDTH - 10, 10)
                APPLE_Y = random.randrange(TOP_WIDTH, WINDOW_HEIGHT - 10, 10)
                snakelength += 1
                score = small_font.render("Score:" + str(snakelength - 3), True, YELLOW)

            canvas.blit(score, (20, 10))
            pygame.draw.line(canvas, GREEN, (0, TOP_WIDTH), (WINDOW_WIDTH, TOP_WIDTH))
            pygame.draw.line(canvas, YELLOW, (WINDOW_WIDTH - 60, 0), (WINDOW_WIDTH - 60, TOP_WIDTH))
            pygame.draw.rect(canvas, YELLOW, (WINDOW_WIDTH - 60, 0, 60, TOP_WIDTH))
            canvas.blit(pause_font, (WINDOW_WIDTH - 45, 10))
            canvas.blit(apple_img, (APPLE_X, APPLE_Y))
            pygame.display.update()

            clock.tick(FPS)


start_game()
gameloop()

Explanation:

In this module which is the function in loop a snake game.

Downloadable Python Snake Game Code

I have here the list of Best Python Projects with Source code free to download for free, I hope this can help you a lot.

Summary

This simple snake game code in Python project is written in Python. The project file contains image files and Python scripts (game.py, duplicate.py). GUI pygame library.

Talking about the gameplay, it’s a single-player game, where the player (Snake) has to eat all the apples in order to grow longer.

The main objective of this game is to gain the highest score. While playing the game, make sure the snake does not cross the edges otherwise you’ll die.

A simple and clean GUI is provided for easy gameplay. The gameplay design is so simple that the user won’t find it difficult to use and understand.

Different images are used in the development of this game project. In order to run the project, you must have installed Python and Pygame on your PC.

This simple game project with source code is free to download. Use it for education purposes only! For the project demo, have a look at the video below.

Inquiries

If you have any questions or suggestions about Snake Game In Python Code, please feel free to leave a comment below.

1 thought on “Snake Game In Python Code Free Download”

Leave a Comment