The Snake Game In Python is one of the most popular arcade games of all time.
The player’s main aim in this game is to capture as many fruits as possible without striking the wall or himself.
While learning Python or PyGame, making a moving snake game might be a fun challenge.
It’s one of the most beginner-friendly projects out there, and it’s one that every new programmer should take on as a challenge. Learning to make a video game is a fascinating and enjoyable experience.
What is the logic behind Snake Game In Python?
This Snake Game In Python has a very simple logic in which every node in the snake will be represented by an object that tracks X and Y coordinates.
You’ll use the same logic, but each node will be moved to the preceding node’s position before the head is moved to the new place. There will be no need to depict anything using numbers.
Project Details and Technology
Project Name: | Snake Game In Python Code |
Abstract: | This Snake Game is one of the most well-known arcade games ever. In this game, the player’s main goal is to collect as many fruits as possible without hitting the wall or himself. |
Language/s Used: | Python (GUI Based) |
Python version (Recommended): | 3.8 or 3.9 |
Type: | Desktop Application |
Developer: | Source Code Hero |
Updates: | 0 |
About The Project
The Snake Game is a simple desktop application made using the Python programming language. We may also create highly fascinating games with the Python programming language.
A snake game is one of them. The project system file comprises resource files as well as a Python script. The graphics of the game are smooth, and the controls are simple.
The player controls a long, thin snake-like creature that moves around on a bordered plane, picking up the apple while avoiding hitting its own tail or the playing area’s edges.
Moreover, this Snake Game In Python Source Code includes a tutorial and a code development guide. This is a simple and basic-level little project for learning purposes.
Snake game is open source, so you can download the zip file and change it to fit your needs.
You can also customize this project to meet your needs and create a fantastic advanced-level project.
How To Make Snake Game In Python?
The code given below is the full source code on How To Make Snake Game In Python.
The given code below is a Python file for main.py
# includes pause import pygame import sys import random 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() 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() 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() 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() 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)) 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() 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()
Project Output:
This Snake Game In Python Code also includes a downloadable Project With Source Code for free, just find the downloadable source code below and click to start downloading.
By the way, if you are new to Python programming and don’t have any idea what Python IDE to use, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS for you.
Additionally, I also have here How to Download and Install the Latest Version of Python on Windows.
To start executing a Snake Game, make sure that you have installed Python on your computer.
Steps On How To Run The Project
Time needed: 5 minutes
These are the steps on how to run Snake Game In Python With Source Code
- Download Source Code
First, find the downloadable source code below and click to start downloading the source code file.
- Extract File
Next, after finished to download the file, go to the file location right-click the file and click extract.
- Open PyCharm
Next, open pycharm IDE and open the project you’ve downloaded.
- Run Project
Next, go to the Pycharm and click the run button to start executing the project.
Download the Source Code below!
Summary
This article is a way to enhance and develop our skills and logic ideas which is important in practicing the Python programming language which is the most well-known and most usable programming language in many companies.
Inquiries
If you have any questions or suggestions about Snake Game In Python, please feel free to leave a comment below.