Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple snake game written in Python using the PyGame library (https://github.com/rajatdiptabiswas/snake-pygame)
"""
Snake Eater
Made with PyGame
"""
import pygame, sys, time, random
# Difficulty settings
# Easy -> 10
# Medium -> 25
# Hard -> 40
# Harder -> 60
# Impossible-> 120
difficulty = 25
# Window size
frame_size_x = 720
frame_size_y = 480
# Checks for errors encountered
check_errors = pygame.init()
# pygame.init() example output -> (6, 0)
# second number in tuple gives number of errors
if check_errors[1] > 0:
print(f'[!] Had {check_errors[1]} errors when initialising game, exiting...')
sys.exit(-1)
else:
print('[+] Game successfully initialised')
# Initialise game window
pygame.display.set_caption('Snake Eater')
game_window = pygame.display.set_mode((frame_size_x, frame_size_y))
# Colors (R, G, B)
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
# FPS (frames per second) controller
fps_controller = pygame.time.Clock()
# Game variables
snake_pos = [100, 50]
snake_body = [[100, 50], [100-10, 50], [100-(2*10), 50]]
food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]
food_spawn = True
direction = 'RIGHT'
change_to = direction
score = 0
# Game Over
def game_over():
my_font = pygame.font.SysFont('times new roman', 90)
game_over_surface = my_font.render('YOU DIED', True, red)
game_over_rect = game_over_surface.get_rect()
game_over_rect.midtop = (frame_size_x/2, frame_size_y/4)
game_window.fill(black)
game_window.blit(game_over_surface, game_over_rect)
show_score(0, red, 'times', 20)
pygame.display.flip()
time.sleep(3)
pygame.quit()
sys.exit()
# Score
def show_score(choice, color, font, size):
score_font = pygame.font.SysFont(font, size)
score_surface = score_font.render('Score : ' + str(score), True, color)
score_rect = score_surface.get_rect()
if choice == 1:
score_rect.midtop = (frame_size_x/10, 15)
else:
score_rect.midtop = (frame_size_x/2, frame_size_y/1.25)
game_window.blit(score_surface, score_rect)
# pygame.display.flip()
# Main logic
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Whenever a key is pressed down
elif event.type == pygame.KEYDOWN:
# W -> Up; S -> Down; A -> Left; D -> Right
if event.key == pygame.K_UP or event.key == ord('w'):
change_to = 'UP'
if event.key == pygame.K_DOWN or event.key == ord('s'):
change_to = 'DOWN'
if event.key == pygame.K_LEFT or event.key == ord('a'):
change_to = 'LEFT'
if event.key == pygame.K_RIGHT or event.key == ord('d'):
change_to = 'RIGHT'
# Esc -> Create event to quit the game
if event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
# Making sure the snake cannot move in the opposite direction instantaneously
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
if change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
# Moving the snake
if direction == 'UP':
snake_pos[1] -= 10
if direction == 'DOWN':
snake_pos[1] += 10
if direction == 'LEFT':
snake_pos[0] -= 10
if direction == 'RIGHT':
snake_pos[0] += 10
# Snake body growing mechanism
snake_body.insert(0, list(snake_pos))
if snake_pos[0] == food_pos[0] and snake_pos[1] == food_pos[1]:
score += 1
food_spawn = False
else:
snake_body.pop()
# Spawning food on the screen
if not food_spawn:
food_pos = [random.randrange(1, (frame_size_x//10)) * 10, random.randrange(1, (frame_size_y//10)) * 10]
food_spawn = True
# GFX
game_window.fill(black)
for pos in snake_body:
# Snake body
# .draw.rect(play_surface, color, xy-coordinate)
# xy-coordinate -> .Rect(x, y, size_x, size_y)
pygame.draw.rect(game_window, green, pygame.Rect(pos[0], pos[1], 10, 10))
# Snake food
pygame.draw.rect(game_window, white, pygame.Rect(food_pos[0], food_pos[1], 10, 10))
# Game Over conditions
# Getting out of bounds
if snake_pos[0] < 0 or snake_pos[0] > frame_size_x-10:
game_over()
if snake_pos[1] < 0 or snake_pos[1] > frame_size_y-10:
game_over()
# Touching the snake body
for block in snake_body[1:]:
if snake_pos[0] == block[0] and snake_pos[1] == block[1]:
game_over()
show_score(1, white, 'consolas', 20)
# Refresh game screen
pygame.display.update()
# Refresh rate
fps_controller.tick(difficulty)
@rajatdiptabiswas

This comment has been minimized.

Copy link
Owner Author

@rajatdiptabiswas rajatdiptabiswas commented Dec 12, 2017

Screenshots

1
Written using PyCharm

2
Active game screen

3
Game over screen

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Feb 8, 2019

Nice game dude.
I know a fair bit about python maybe we can collab sometime?
cheers,
Josh.

@ankur965

This comment has been minimized.

Copy link

@ankur965 ankur965 commented Nov 28, 2019

very good game

@Suhail2002

This comment has been minimized.

Copy link

@Suhail2002 Suhail2002 commented Jan 1, 2020

how do you extract the score in order to display it separately?

@rajatdiptabiswas

This comment has been minimized.

Copy link
Owner Author

@rajatdiptabiswas rajatdiptabiswas commented Jan 5, 2020

@Suhail2002 The score is just a global variable that is being incremented. Check line 59 for the variable declaration. Cheers!

@rajatdiptabiswas

This comment has been minimized.

Copy link
Owner Author

@rajatdiptabiswas rajatdiptabiswas commented Jan 5, 2020

Thanks Josh! Wish I saw that comment earlier. Literally took me a decade to respond.

@Yaduraj-eipaca

This comment has been minimized.

Copy link

@Yaduraj-eipaca Yaduraj-eipaca commented May 12, 2020

Wow-what I great and beautiful code. Well done

@CookiePie-cell

This comment has been minimized.

Copy link

@CookiePie-cell CookiePie-cell commented Sep 1, 2020

Would you explain me why use list on line 132 and not snake_body.insert(0, snake_pos)? Thank you

@rajatdiptabiswas

This comment has been minimized.

Copy link
Owner Author

@rajatdiptabiswas rajatdiptabiswas commented Sep 1, 2020

Hi @CookiePie-cell

I'm guessing list(snake_pos) would still just return snake_pos.
I think snake_body.insert(0, snake_pos) should work just fine. Kindly try using that and checking.

I made this a long time back and I'm certain I made more mistakes just like that.
Thanks for pointing it out!

@Cheran-1

This comment has been minimized.

Copy link

@Cheran-1 Cheran-1 commented Nov 30, 2020

May I know how to add background to this game.
EDIT:- I want to add a picture as background

Thank you in advance

@rajatdiptabiswas

This comment has been minimized.

Copy link
Owner Author

@rajatdiptabiswas rajatdiptabiswas commented Dec 2, 2020

Hi @Cheran-1

I guess changing the game_window.fill(black) at line 68 to use an image should work.
I'm not exactly sure, so the PyGame Docs should be your friend.

However, take a look at this StackOverflow page Using a background image in pygame window.
I guess it should have the answer you're looking for.

Good luck.

@2016103056

This comment has been minimized.

Copy link

@2016103056 2016103056 commented Jun 2, 2021

Is there anyway where instead of just a color we can create a custom face for the snake? like a character image?

@rajatdiptabiswas

This comment has been minimized.

Copy link
Owner Author

@rajatdiptabiswas rajatdiptabiswas commented Jun 11, 2021

Hi Sanjana,

I believe there should be a way to do this. pygame.image.load() might be what you are looking for. The Pygame Documentation should have more info.

Take a look at this article PyGame Tutorial: Working with Images. It might help.

@Gugan-web

This comment has been minimized.

Copy link

@Gugan-web Gugan-web commented Jun 26, 2021

I can't control it how to do that

@2016103056

This comment has been minimized.

Copy link

@2016103056 2016103056 commented Jun 26, 2021

Thank you! will take a look into these links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment