INTRODUCTION
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
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")
screen_height)).convert_alpha()
# Welcome Image
welcome_img =
pygame.image.load("C:/Users/chowd/OneDrive/Pictures/first
(screen_width, screen_height)).convert_alpha()
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.
- 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.
- 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.
- 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
- 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
- It initializes game variables like snake position, velocity, score, list to store snake
segments, etc.
- 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).
- 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.
- The snake list is updated by adding the new head position and optionally
- The game checks for collisions (snake hitting itself or the edges) and sets game
over if true.
- 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