Snake Game In Python With Source Code
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 on How To Create Snake Game In Python.
A Snake Game Python Tkinter is a simple and clean GUI is provided for easy game play. The game play 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.
This Snake Game Using Python also includes the downloadable Snake Game In 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 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
And now sit back and relax because i will show you on How To Make Snake Game Python, To start creating this Snake Game In Python Code make sure that you have PyCharm IDE installed in your computer.
By the way if you are new to python programming and you don’t know what would be the the Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, Mac OS that will suit for you. I also have here How to Download and Install Latest Version of Python on Windows.
Steps on how to create a Simple Snake Game In Python With Source Code
Below are the step by step guide on how to perform the Code Snake Game In Python
- 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.
- 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“.
- Step 3: Name your python file.
Third after creating a python file, Name your python file after that click “enter“.
- Step 4: The actual code of snake game in Python
You are free to copy the code given below and download the full source code below.
The Code Give Below Is For Importing Modules.
1 2 3 |
import pygame import sys import random |
In the code given which is importing all the modules to be call in executing the game.
The Code Given Below Is For Declaration of Variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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() |
The code given which is declaration of the variables to be called in executing the process of the game.
The Code Given Below Is For The Module In Starting Of The Game
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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() |
In this module which is the interface of the starting a snake game.
This will be the output

The Code Given Below Is For The Instruction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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() |
In this module which is the interface of the game instructions.
This will be the output

The Code Given Below Is For Game Over
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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() |
In This module which is the code for the game over.
This will be the output

The Code Given Below Is For The Movement Of The Snake
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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)) |
In this module which is the movement of the snake that being used by the user.
The Code Given Below Is For The “Pause” Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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() |
In this module which is the game paused function.
This will be the output

The code of snake game in python for Game loop Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
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() |
In this module which is the function in loop a snake game.
Complete Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# 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() |
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 |
Run Quick Virus Scan for secure Download
Run Quick Scan for secure DownloadDownloadable Python Snake Game Code
I have here the list of Best Python Project 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 should 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 for education purpose only! For the project demo, have a look at the video below.
Related Article Below
- Best Python Books for Beginners 2021
- Best Python Course Online | Python Course Free 2021
- Best Python Compiler Online and Offline 2021
- Best Python IDE for Windows, Linux, Mac OS 2021
- Best Python Projects for Beginners
- Best Python Projects With Source Code 2021
- How To Make A Point Of Sale System In Python
- Python PIP Command Set-up / Fix: Step by Step Guide
- Student Management System Project in Python with Source Code
- Hotel Management System Project in Python With Source Code
Inquiries
If you have any questions or suggestions about Snake Game In Python code , please feel free to leave a comment below.