Project Oop Reprt
Project Oop Reprt
Introduction:
The Snake Game is a classic video game where the player controls a snake that moves around the
screen, collecting food items and avoiding collisions with its own body or the game boundaries. This
project aims to create a simple version of the Snake Game using C++.
Implementation :
The implementation of the Snake Game in C++ utilizes classes and basic game logic. The game
board is represented using a grid system, and the snake's movement is controlled by the player.
The game tracks the player's score and ends when the snake collides with its own body or the
boundaries of the game board.
Features:
The implemented Snake Game offers the following features:
Snake movement:
The player can control the snake's movement using the arrow keys or 'w', 'a', 's', 'd' keys.
Food generation:
Randomly generates food items on the game board for the snake to eat.
Scoring:
Keeps track of the player's score, increasing it when the snake eats food.
Collision detection:
Detects collisions between the snake's head and its own body or the game boundaries.
Game over:
Continuously updates and redraws the game board until the game is over.
Code Explanation:
The provided code consists of a SnakeGame class that encapsulates the game logic. It manages
the game state, including the snake's position, the food's position, the score, and the tail
segments.
class SnakeGame {
public:
SnakeGame() {
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
score = 0;
The Setup function initializes the game state, setting the initial positions of the snake and food.
void Setup() {
system("cls");
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
score = 0;
The Draw function renders the game board on the console, displaying the snake, food, and
score
void Draw() {
system("cls");
if (j == 0)
if (i == y && j == x)
else {
if (!printTail)
if (j == width - 1)
The Logic function updates the game state, moving the snake, checking for collisions, and
updating the score.
void Logic() {
tailX[0] = x;
tailY[0] = y;
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
The Run function starts the game loop, repeatedly calling the Draw, Input, and Logic functions
until the game is over.
The game loop also includes a delay using the Sleep function, which controls the speed of the
snake.
Usage:
To play the Snake Game, compile and run the provided C++ code. The game will start
automatically. Use the arrow keys or 'w', 'a', 's', 'd' keys to control the snake's movement. Try to
eat the food without colliding with the snake's own body or the game boundaries. The game
ends when a collision occurs, and the final score is displayed.
Conclusion:
In conclusion, the simple Snake Game implementation in C++ using classes and object-oriented
programming provides an enjoyable gaming experience. It demonstrates the use of
fundamental programming concepts such as data structures, input handling, and game logic.
This project serves as a starting point for further development and customization, allowing for
endless possibilities to enhance the game and create unique features.
*****************************