Snake Game Uv
Snake Game Uv
ON
SNAKE GAME
Submitted in partial fulfillment of the requirement
Trade :- C.S.E
1
Acknowledgment
INTRODUCTION.......................................................................................................................1
GAME DESIGN........................................................................................................................... 2
TESTING...........................................................................................................................................6
ALGORITHEM .......................................................................................................................... 9
SDLC...................................................................................................................................................12
GAME CODE...............................................................................................................................14
Project Report: Snake Game in Python
1. Introduction
1.1 Overview of the Project: The snake game is one of the classical examples of a
game that comes in an arcade, where a player maneuvers a snake that changes directions
with every food it eats and grows. A snake will be considered dead if it clashes with itself or
the walls. The objective is to create a game using Python and the Pygame library with a
graphical interface, where one can control the snake with keyboard entries.
1.2 Objectives
1. Create a Snake game: Create the necessary functions to program the basic Snake game
mechanics with Python using Pygame.
2. Game Design
a. Use the arrow keys (up, down, left, right) to control the snake.
b. The snake keeps on moving continually in the direction of the last arrow key press.
c. The game can be won by eating the food that appears randomly on the screen.
e. The game is over when the snake hits a wall or its own body.
f. For every food eaten, the player increases his score by one.
2.2 User Interface
The game is displayed in a windowed graphical interface using Pygame. The screen shows
the snake, the food, and the player's current score. A "Game Over" message appears when
the game ends, along with the player's final score.
3. Implementation
Pygame: This is the main library used to create the game. It handles the game's graphics,
sound, and input.
Time: Used to manage time-related functions, though in this script, it's mainly used to quit
the game after a delay.
Random: This library is used to generate random positions for the food on the screen.
pygame.init(): Initializes all the Pygame modules. This step is essential to use any Pygame
functionality.
The width and height of the game window are set to 600x400 pixels.
pygame.font.SysFont(): Creates font objects for rendering text on the screen. font_style is
used for messages, and score_font is used for displaying the score.
your_score(score): This function renders the current score on the screen. The score is
displayed in the top left corner.
message(msg, color): This function displays a message at the center of the screen, typically
used for the 'Game Over' message.
gameLoop(): This function contains the main game loop, which keeps the game running
until the player loses or closes the window.
game_over and game_close: Boolean flags used to manage the game state.
x1 = dis_width / 2
y1 = dis_height / 2
x1_change = 0
y1_change = 0
snake_list = []
length_of_snake = 1
- **Purpose:** Test individual components of the game, such as functions and methods.
- **Focus Areas:**
- **Movement Logic:** Verify that the snake moves correctly in response to key presses.
- **Boundary Detection:** Ensure the game detects when the snake hits the walls.
- **Food Consumption:** Check that the snake grows and the score increases when the snake
eats food.
- **Self-Collision Detection:** Test if the game correctly detects when the snake collides with
itself.
- **Tools:** You can use Python's built-in `unittest` module to automate these tests.
- **Focus Areas:**
- **Game Loop:** Ensure the game loop correctly handles events, updates game state, and
renders the display.
- **Collision Handling:** Test the integration between movement, collision detection, and game-
over conditions.
- **Score Display:** Verify that the score is accurately displayed and updated during gameplay.
- **Focus Areas:**
- **User Inputs:** Verify that the arrow keys correctly control the snake.
- **Game Over Condition:** Ensure that the game ends properly when the snake hits a wall or
itself.
- **Restart and Quit:** Check that the game restarts when the user chooses to play again and
quits as expected.
6
- **Purpose:** Ensure the game is user-friendly and enjoyable.
- **Focus Areas:**
- **User Experience:** Get feedback from users about the controls, difficulty, and overall
experience.
- **Game Instructions:** Verify that the game provides clear instructions or feedback to the
player.
- **Focus Areas:**
- **Frame Rate:** Test the game's performance on different hardware to ensure smooth
gameplay.
- **Resource Usage:** Monitor CPU and memory usage during gameplay to identify potential
performance bottlenecks.
- **Purpose:** Ensure that new changes or bug fixes do not introduce new issues.
- **Approach:** After making changes or updates, re-run all the previous tests to ensure
everything still works as expected.
- **Purpose:** Validate the game against the initial requirements and user expectations.
- **Focus Areas:**
- **Gameplay:** Ensure the game meets all specified requirements, including functionality,
performance, and user experience.
- **User Feedback:** Gather feedback from potential users or stakeholders to ensure the game
meets their needs.
- **Manual Testing:** Play the game yourself or have others play it to catch any issues not covered
by automated tests.
- **Automated Testing:** Use tools like `unittest`, `pytest`, or Pygame's event handling to automate
repetitive tests.
7
Here’s an algorithm for the game logic of the Snake game. This algorithm outlines the basic steps the
game follows from initialization to game-over conditions and restarting the game.
1. **Initialize Game:**
- Define colors using RGB values for the snake, food, background, and text.
- Initialize variables for snake movement (`x1_change`, `y1_change`), snake size (`snake_block`),
and speed (`snake_speed`).
- Create an empty list `snake_list` to store the positions of the snake segments.
- Initialize `length_of_snake` to 1.
- If the player presses 'Q', set `game_over = True` and `game_close = False`.
- If the player presses 'C', restart the game by calling the game loop function.
8
- Update `x1_change` and `y1_change` based on the arrow key pressed:
- Update the snake's `x1` and `y1` position based on `x1_change` and `y1_change`.
- **Wall Collision:**
- If the snake's `x1` or `y1` is outside the display boundaries, set `game_close = True`.
- **Self-Collision:**
- Loop through the `snake_list` (excluding the head). If the snake's head position matches any
segment in `snake_list`, set `game_close = True`.
- If the snake's head reaches the position of the food (`x1 == foodx` and `y1 == foody`):
9
- Use `clock.tick(snake_speed)` to control the frame rate and thus the speed of the snake.
- If `game_over` is `True`, exit the main loop and quit the game with `pygame.quit()` and `quit()`.
- If the game ends and the player chooses to play again, restart the game by calling the game loop
function.
10
To create a Software Development Life Cycle (SDLC) diagram for the Snake game, I will outline the
stages and then provide you with a visual representation.
1. Planning:
2. Requirements Analysis:
o Gather user expectations and gameplay features (e.g., snake movement, scoring,
game over conditions).
3. Design:
o Create flowcharts and diagrams to represent game logic, including snake movement,
food generation, and collision detection.
4. Implementation (Coding):
o Write the code for the game using Python and Pygame.
o Implement game logic, including movement, collision detection, and score tracking.
5. Testing:
6. Deployment:
o Deploy the game for use, either locally or on a platform for users to download.
7. Maintenance:
11
GAME CODE
import time
import random
import pygame
# Initialize Pygame pygame.init()
# Define colors
white = (255, 200, 255)
yellow = (255, 205, 202)
black = (0, 0, 0)
red = (213, 50, 80)
green = (100, 255, 0)
blue = (150, 153, 213)
12
pygame.display.set_caption('Snake Game by Karanveer')
x1 = dis_width / 2
y1 = dis_height / 2
x1_change = 0
y1_change = 0
snake_list = []
length_of_snake = 1
14
while game_close == True:
dis.fill(black)
message("You Lost! Press C-Play Again or Q-Quit",
red)
your_score(length_of_snake - 1)
pygame.display.update()
our_snake(snake_block, snake_list)
your_score(length_of_snake - 1)
pygame.display.update()
clock.tick(snake_speed)
pygame.quit()
quit()
17
18