Introduction
The Snake game is a timeless arcade classic enjoyed by people of all ages. This report
explores a Python implementation of this game, analyzing its functionalities, structure, and
potential for enhancement. Understanding this code provides valuable insights into game
development principles and the capabilities of the Pygame library.
The objective of The Hungry Snake is to control a snake and navigate it around a
designated playing field to consume food items. Each eaten food increases the snake's
length and the player's score. The core gameplay mechanics involve:
Snake Movement: The player controls the snake's direction using keyboard arrows. The
snake moves continuously in the chosen direction, updating its position on each frame.
Food Generation: Food items are randomly positioned on the screen at the beginning of
the game and after each successful food consumption.
Collision Detection: The game checks for collisions between the snake's head and various
elements:
Walls: If the snake touches the screen's boundaries, it's considered a game over.
Snake Body: If the snake collides with its own body (excluding the head's immediate
previous position), it's game over.
Scorekeeping and High Score: Points are awarded for each eaten food item. The game
keeps track of the current score and compares it to a stored high score. If the current score
surpasses the high score, it's updated and saved for future playthroughs.
CODE
import pygame
import random
import os
pygame.mixer.init()
pygame.init()
# Colors
white = (255, 255, 255)
red = (255, 0, 0)
black = (0, 0, 0)
# Creating window
screen_width = 900
screen_height = 600
gameWindow = pygame.display.set_mode((screen_width,
screen_height))
# Background Image
bgimg = pygame.image.load("C:/Users/chowd/Downloads/bgt
snake gam,e.png")
bgimg = pygame.transform.scale(bgimg, (screen_width,
screen_height)).convert_alpha()
# Welcome Image
welcome_img =
pygame.image.load("C:/Users/chowd/OneDrive/Pictures/first
interface snake 2 .png")
welcome_img = pygame.transform.scale(welcome_img,
(screen_width, screen_height)).convert_alpha()
if snake_x < 0 or snake_x > screen_width or snake_y < 0
or snake_y > screen_height:
game_over = True
pygame.mixer.music.load('C:/Users/chowd/Downloads/CRYING
LAST .mp3')
pygame.mixer.music.play()
plot_snake(gameWindow, snk_list)
pygame.display.update()
clock.tick(fps)
pygame.quit()
quit()
welcome()
EXPLANATION OF THE CODE
Imports and Initialization:
- Libraries like pygame, random, and os are imported for functionalities like graphics,
random number generation, and file handling respectively.
- Pygame is initialized to use its features for the game.
Colors and Display:
- Colors like white, red, and black are defined for later use in the game window.
- The game window size is set (900x600 pixels) and a caption "THE HUNGRY SNAKE" is
displayed.
- A clock object is created to regulate the game's speed.
- A font object is created to display text on the screen.
Loading Images and Sounds:
- Background, welcome, and game over images are loaded from specified paths.
- Music and sound effects are loaded for background music, game start, and food
eaten events.
- Background music starts playing on a loop with a set volume.
Loading Snake Images:
- Snake body and face images are loaded from specified paths and resized for the
game.
Helper Functions:
- text_screen function takes text, color, x and y coordinates as input and displays
the text on the screen at the specified position. - plot_snake function iterates through a
snake list and displays the snake body and
face images based on their positions.
Welcome Screen Function:
- welcome function displays the welcome image and waits for user input (space
key). - Upon pressing space, it plays the game start sound and calls the gameloop
function to start the actual game.
Main Game Loop:
- gameloop function handles the core gameplay logic.
- It initializes game variables like snake position, velocity, score, list to store snake
segments, etc.
- It reads the high score from a text file named "hiscore.txt".
- The main loop runs until the user exits the game.
- Inside the loop: - If the game is over, it displays the game over image, reads the high
score again, and waits for user input (enter key) to restart the game.
- Otherwise, it checks for user input (arrow keys) to control the snake's direction.
It also allows a cheat mode using the 'q' key (increases score).
- The snake's position is updated based on its velocity.
- If the snake eats the food (collision detection), the score increases, snake
lengthens, new food position is generated, and high score is updated if needed.
- The game window is filled with white color, background image is drawn.
- Score and high score are displayed on the screen.
- Food image is drawn at its current position.
- The snake list is updated by adding the new head position and optionally
removing the tail if the snake length hasn't increased.
- The game checks for collisions (snake hitting itself or the edges) and sets game
over if true.
- The plot_snake function is called to draw the snake on the screen.
- The display is updated, and the clock ticks to maintain the game speed.
- Finally, pygame quits and the program exits. Overall, this code demonstrates a well-
structured implementation of the classic
Snake game using Pygame library. It includes features like a welcome screen, background
music, sound effects, scorekeeping, and high score tracking