Snake Game in Python

Overview

Snake-Game-in-Python

Introdction:

Snake game is one of the most popular arcade games of all time. In this game, the main objective of the player is to catch the maximum number of fruits without hitting the wall or itself.It is one of the best beginner-friendly projects that every novice programmer should take as a challenge. Learning to build a video game is kinda interesting and fun learning. We will be using Pygame to create this snake game. Pygame is an open-source library that is designed for making video games. It has inbuilt graphics and sounds libraries. It is also beginner-friendly, and cross-platform.


Dependecies:

  • Pygame
  • How to download : use code: pip install pygame, in your terminal
  • Make sure you have pip installed

Step 1: Firstly we are importing the necessary libraries.

  • After that, we are defining the width and height of the window in which the game will be played.
  • And define the color in RGB format that we are going to use in our game for displaying text.
# importing libraries
import pygame
import time
import random

snake_speed = 15

# Window size
window_x = 720
window_y = 480

# defining colors
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)

Step 2: After importing libraries we need to initialize Pygame using pygame.init() method.

  • Create a game window using the width and height defined in the previous step.
  • Here pygame.time.Clock() will be used further in the main logic of the game to change the speed of the snake.
# Initialising pygame
pygame.init()

# Initialise game window
pygame.display.set_caption('Snake Game by: Pavan Ananth Sharma')
game_window = pygame.display.set_mode((window_x, window_y))

# FPS (frames per second) controller
fps = pygame.time.Clock()

Step 3: Initialize snake position and its size.

  • After initializing snake position, initialize the fruit position randomly anywhere in the defined height and width.
  • By setting direction to RIGHT we ensure that, whenever a user runs the program/game, the snake must move right to the screen.
# defining snake default position
snake_position = [100, 50]

# defining first 4 blocks of snake
# body
snake_body = [ [100, 50],
				[90, 50],
				[80, 50],
				[70, 50]
			]
# fruit position
fruit_position = [random.randrange(1, (window_x//10)) * 10,
				random.randrange(1, (window_y//10)) * 10]
fruit_spawn = True

# setting default snake direction
# towards right
direction = 'RIGHT'
change_to = direction

Step 4: Create a function to display the score of the player.

  • In this function, firstly we’re creating a font object i.e. the font color will go here.
  • Then we are using render to create a background surface that we are going to change whenever our score updates.
  • Create a rectangular object for the text surface object (where text will be refreshed)
  • Then, we are displaying our score using blit. blit takes two argument screen.blit(background,(x,y))
# initial score
score = 0

# displaying Score function
def show_score(choice, color, font, size):

	# creating font object score_font
	score_font = pygame.font.SysFont(font, size)
	
	# create the display surface object
	# score_surface
	score_surface = score_font.render('Score : ' + str(score), True, color)
	
	# create a rectangular object for the
	# text surface object
	score_rect = score_surface.get_rect()
	
	# displaying text
	game_window.blit(score_surface, score_rect)

Step 5: Now create a game over function that will represent the score after the snake is hit by a wall or itself.

  • In the first line, we are creating a font object to display scores.
  • Then we are creating text surfaces to render scores.
  • After that, we are setting the position of the text in the middle of the playable area.
  • Display the scores using blit and updating the score by updating the surface using flip().
  • We are using sleep(2) to wait for 2 seconds before closing the window using quit().
# game over function
def game_over():

	# creating font object my_font
	my_font = pygame.font.SysFont('times new roman', 50)
	
	# creating a text surface on which text
	# will be drawn
	game_over_surface = my_font.render('Your Score is : ' + str(score), True, red)
	
	# create a rectangular object for the text
	# surface object
	game_over_rect = game_over_surface.get_rect()
	
	# setting position of the text
	game_over_rect.midtop = (window_x/2, window_y/4)
	
	# blit wil draw the text on screen
	game_window.blit(game_over_surface, game_over_rect)
	pygame.display.flip()
	
	# after 2 seconds we will quit the
	# program
	time.sleep(2)
	
	# deactivating pygame library
	pygame.quit()
	
	# quit the program
	quit()

Step 6: Now we will be creating our main function that will do the following things:

  • We will be validating the keys that will be responsible for the movement of the snake, then we will be creating a special condition that the snake should not be allowed to move in the opposite direction instantaneously.
  • After that, if snake and fruit collide we will be incrementing the score by 10 and new fruit will be spanned.
  • After that, we are checking that is the snake hit with a wall or not. If a snake hits a wall we will call game over function.
  • If the snake hits itself, the game over function will be called.
  • And in the end, we will be displaying the scores using the show_score function created earlier.
# Main Function
while True:

	# handling key events
	for event in pygame.event.get():
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_UP:
				change_to = 'UP'
			if event.key == pygame.K_DOWN:
				change_to = 'DOWN'
			if event.key == pygame.K_LEFT:
				change_to = 'LEFT'
			if event.key == pygame.K_RIGHT:
				change_to = 'RIGHT'

	# If two keys pressed simultaneously
	# we don't want snake to move into two directions
	# simultaneously
	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_position[1] -= 10
	if direction == 'DOWN':
		snake_position[1] += 10
	if direction == 'LEFT':
		snake_position[0] -= 10
	if direction == 'RIGHT':
		snake_position[0] += 10

	# Snake body growing mechanism
	# if fruits and snakes collide then scores will be
	# incremented by 10
	snake_body.insert(0, list(snake_position))
	if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:
		score += 10
		fruit_spawn = False
	else:
		snake_body.pop()
		
	if not fruit_spawn:
		fruit_position = [random.randrange(1, (window_x//10)) * 10,
						random.randrange(1, (window_y//10)) * 10]
		
	fruit_spawn = True
	game_window.fill(black)
	
	for pos in snake_body:
		pygame.draw.rect(game_window, green, pygame.Rect(
		pos[0], pos[1], 10, 10))
		
	pygame.draw.rect(game_window, white, pygame.Rect(
	fruit_position[0], fruit_position[1], 10, 10))

	# Game Over conditions
	if snake_position[0] < 0 or snake_position[0] > window_x-10:
		game_over()
	if snake_position[1] < 0 or snake_position[1] > window_y-10:
		game_over()
	
	# Touching the snake body
	for block in snake_body[1:]:
		if snake_position[0] == block[0] and snake_position[1] == block[1]:
			game_over()
	
	# displaying score countinuously
	show_score(1, white, 'times new roman', 20)
	
	# Refresh game screen
	pygame.display.update()

	# Frame Per Second /Refres Rate
	fps.tick(snake_speed)

Conclusion: if you have followd all the steps the game should be ready.

  • Special thanks to GKG
  • 0x592eF244F8924be9F3F8803f8d639392f465Ac51
Owner
Pavan Ananth Sharma
Ethereum 2.0
Pavan Ananth Sharma
Jogo da velha com interface gráfica desenvolvida em Python utilizando pygame e IA(Inteligência Artificial)

Jogo-da-Velha Sobre o projeto Jogo da velha com interface gráfica desenvolvida em Python utilizando pygame e IA(Inteligência Artificial) Layout tela d

Matheus Maia Alvarez 6 Apr 09, 2022
OpenGL experiments with Pygame & ModernGL

pygame-opengl OpenGL experiments with Pygame & ModernGL TODO Skybox & Reflections Post-process effects (motion blur, color correction, etc..) Normal m

Kadir Aksoy 4 Oct 28, 2022
Simulate a Monopoly game to generate the probabilities of landing on each square.

Monopoly-Probabilities Simulate a Monopoly game to generate the probabilities of landing on each square. While this project originally was inspired by

George Waters 2 Jan 04, 2023
A "finish the lyrics" game using Spotify, YouTube Transcript, and YouTube Search APIs, coupled with visual machine learning

Singify Introducing Singify, the party game! Challenge your friend to who knows songs better. Play random songs from your very own Spotify playlist an

Josh Wong 4 Nov 19, 2021
Chess - A python gui application

Chess Python version 3.10 or greater is required to play. Note This is a gui application, and as such will not run inside WSL.

Jonxslays 1 Dec 16, 2021
An all-inclusive Python framework for the Riot Games League of Legends API. We focus on making the data easy and fun to work with, while providing all the tools necessary to create a website or do data analysis.

Cassiopeia A Python adaptation of the Riot Games League of Legends API (https://developer.riotgames.com/). Cassiopeia is the sister library to Orianna

Meraki Analytics 473 Jan 07, 2023
A chess engine with basic AI capabilities (search for best move using MinMax algorithm)

A chess engine with basic AI capabilities (search for best move using MinMax algorithm)

Ken Wu 1 Feb 02, 2022
Pyxel is a retro game engine for Python.

Pyxel is open source and free to use. Let's start making a retro game with Pyxel!

Takashi Kitao 11.2k Jan 09, 2023
Discord based board game, Sneks and Ladders

sneks-and-ladders Discord based board game, Sneks and Ladders intro This is the code-base for the Discord based game, Sneks and Ladders, as used for s

Yohei Nakajima 3 Nov 04, 2022
The Bowling Club (Facebook Game) get all strikes.

TheBowlingClubBot The Bowling Club (Facebook Game) get all strikes. FAQ Q: What is this? A: TheBowlingClubBot is a automation bot with 99.99% guarante

#~Rith 1 Jan 19, 2022
TicTacToc - Simple TicTacToc game played by minimax algorithm

TicTacToc simple TicTacToc game played by minimax algorithm. This app is based o

5 Apr 05, 2022
Made by Ashish and Avinash-sord12k. Powered by pygame

Spook_alle About -Made by Ashish (Github: Ashish-Github193) and Avinash-sord12k Version - BETA v_1.0 /1-11-2021/ (game is at its base version more ite

Ashish Kumar Jha 1 Nov 01, 2021
Box - a world simulator written in python with pygame

Box is a world simulator written in python with pygame. Features A world generation system A world editor Simulates creatures called boxlanders. You c

1up Community 3 Nov 14, 2022
This is a basic virtual quiz game using opencv-python

Basic Virtual-Quiz-Game This is a basic structure of a virtual quiz game using opencv-python. As the camera window opens up we can see the questions a

2 Dec 11, 2021
A solution program of 24. game.

A solution program of 24. game.

1 Dec 12, 2022
Tic-Tac-Toe - Tic-Tac-Toe game build With Python

Tic Tac Toe This game is very popular amongst all of us and even fun to build as

PyLaboratory 0 Feb 06, 2022
Mandaw 2 Mar 01, 2022
This is a simple Tic-Tac-Toe game.

Tic-Tac-Toe Nosso famoso e tradicional Jogo da Velha, mas agora em Python. Development setup Para rodar o programa, basta instalar python em sua maqui

João Assalim 1 Oct 10, 2022
Console 2D GameEngine {C2DGE} [0.1.0]

Console 2D GameEngine {C2DGE} [0.1.0] By Grosse pastèque#6705 The Project's Goal : This projects was just a challenge so if you have bad reviews, it's

Big watermelon 1 Nov 06, 2021
Super Mario Kart November 1991 Prototype Repair by MrL314

Super Mario Kart November 1991 Prototype Repair by MrL314

MrL314 51 Dec 26, 2022