This Snake Game is a classic arcade game implemented using Python and the Pygame library. The game involves controlling a snake to eat food and grow longer while avoiding collisions with the walls and the snake's own body. It is a single-player game where the objective is to achieve the highest possible score by consuming as much food as possible. Enjoy playing the Snake Game, a classic arcade experience brought to life with Python and Pygame! Challenge yourself to beat your high score and master the art of controlling the ever-growing snake.
- Developer: Shabir Mahfudz Prahono - @shabir-mp
- Application creation date: 28 June 2024
- Simple Graphics: The game uses basic rectangular graphics for the snake and food, with different colors to distinguish them.
- Keyboard Controls: The snake is controlled using the arrow keys on the keyboard.
- Score Tracking: The game keeps track of and displays the player's score based on the number of food items consumed.
- Game Over and Restart: When the game is over, the player can choose to quit or restart the game.
To play the Snake Game, you need:
- Python 3.x: Python is the programming language used to run the game.
- Pygame Library: This Python library is required for handling graphics, events, and sound in the game.
-
Install Python:
- Download and install Python 3.x from the official website.
-
Install Pygame:
-
Open a command prompt or terminal.
-
Use the following command to install Pygame using pip, Python's package installer:
pip install pygame
-
-
Download the Snake Game Code:
- Save the provided Python code in a file, e.g.,
snake_game.py
.
- Save the provided Python code in a file, e.g.,
-
Run the Game:
-
Open a command prompt or terminal.
-
Navigate to the directory where
snake_game.py
is saved. -
Run the game by executing the following command:
python snake_game.py
-
-
Game Controls:
- Use the arrow keys on your keyboard to control the snake:
- Up Arrow: Move the snake upwards.
- Down Arrow: Move the snake downwards.
- Left Arrow: Move the snake to the left.
- Right Arrow: Move the snake to the right.
- Use the arrow keys on your keyboard to control the snake:
-
Game Objective:
- Guide the snake to eat the green food squares that randomly appear on the screen.
- Each time the snake eats food, it grows longer.
- Avoid collisions with the walls and the snake's own body, as these will end the game.
-
Scoring:
- The score increases with each piece of food consumed.
- Try to achieve the highest possible score before the snake collides with an obstacle.
-
Game Over:
- The game ends when the snake collides with a wall or its own body.
- Upon game over, you can press Q to quit the game or C to play again.
1. View of the game. User can move the snake (black box) by clicking the up, down, right, left keys on the keyboard. The target is the green box.
import pygame
import time
import random
pygame.init()
- Explanation:
pygame
is a Python library used for game development.time
andrandom
are standard Python libraries.pygame.init()
initializes all the imported pygame modules and must be called before using any other Pygame functions.
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
blue = (50, 153, 213)
- Explanation:
- These variables store RGB tuples representing different colors used in the game interface.
- For example,
white
is pure white(255, 255, 255)
andblack
is pure black(0, 0, 0)
.
dis_width = 800
dis_height = 600
dis = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('Snake Game')
- Explanation:
dis_width
anddis_height
set the dimensions of the game window to 800 pixels wide and 600 pixels high.pygame.display.set_mode((dis_width, dis_height))
creates a Pygame display surface with the specified dimensions.pygame.display.set_caption('Snake Game')
sets the title of the game window to 'Snake Game'.
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 15
- Explanation:
clock
is an instance ofpygame.time.Clock()
used to control the game's frame rate.snake_block
determines the size of each segment of the snake.snake_speed
controls how fast the snake moves; it represents the number of pixels the snake moves per frame update.
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
- Explanation:
font_style
andscore_font
are Pygame Font objects created usingpygame.font.SysFont
.- They are used for rendering text on the game screen with specified fonts ("bahnschrift" and "comicsansms") and sizes (25 and 35, respectively).
-
Your_score(score):
def Your_score(score): value = score_font.render("Your Score: " + str(score), True, black) dis.blit(value, [0, 0])
- Explanation:
Your_score()
function renders and displays the player's current score on the game screen.score_font.render("Your Score: " + str(score), True, black)
creates a text surface with the score rendered in black color.dis.blit(value, [0, 0])
draws the rendered text (value
) at the top-left corner of the screen.
- Explanation:
-
our_snake(snake_block, snake_List):
def our_snake(snake_block, snake_List): for x in snake_List: pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
- Explanation:
our_snake()
function draws the snake on the game screen based on its current segments.- It iterates through
snake_List
, which contains coordinates of each segment of the snake. pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
draws a rectangle (snake segment) at position(x[0], x[1])
with dimensionssnake_block x snake_block
in black color (black
) on thedis
surface.
- Explanation:
-
message(msg, color):
def message(msg, color): mesg = font_style.render(msg, True, color) dis.blit(mesg, [dis_width / 6, dis_height / 3])
- Explanation:
message()
function displays a message on the game screen.- It renders the text
msg
using thefont_style
and with the specifiedcolor
. dis.blit(mesg, [dis_width / 6, dis_height / 3])
draws the rendered message (mesg
) at a specific position on the screen, which is(dis_width / 6, dis_height / 3)
.
- Explanation:
def gameLoop():
game_over = False
game_close = False
x1 = dis_width / 2
y1 = dis_height / 2
x1_change = 0
y1_change = 0
snake_List = []
Length_of_snake = 1
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
while not game_over:
while game_close == True:
dis.fill(blue)
message("You Lost! Press Q-Quit or C-Play Again", red)
Your_score(Length_of_snake - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
dis.fill(white)
pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
our_snake(snake_block, snake_List)
Your_score(Length_of_snake - 1)
pygame.display.update()
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
- Explanation:
gameLoop()
function manages the main game logic and loop.- Initialization:
game_over
andgame_close
control the game state (running or ended).x1
andy1
represent the initial position of the snake's head.x1_change
andy1_change
store the velocity of the snake in the x and y directions.snake_List
keeps track of the snake's body segments.Length_of_snake
tracks the current length of the snake.foodx
andfoody
store the coordinates of the food that the snake eats.
- Main Loop:
while not game_over:
runs the game until the player quits or loses.while game_close == True:
manages the game over screen:- Fills the screen with blue (
dis.fill(blue)
). - Displays a message indicating the player lost and instructions to quit or play again.
- Updates the display and waits for player input (
pygame.event.get()
).
- Fills the screen with blue (
- Event handling (
for event in pygame.event.get()
):- Processes events such as quitting the game (
pygame.QUIT
event) or controlling the snake's direction (pygame.KEYDOWN
events).
- Processes events such as quitting the game (
- Game Logic:
- Checks if the snake collides with boundaries (
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
) and setsgame_close
toTrue
if true. - Updates the snake's position (
x1 += x1_change
,y1 += y1_change
) based on user input
- Checks if the snake collides with boundaries (
This project is licensed under the MIT License. See the LICENSE file for more details.