0% found this document useful (0 votes)
13 views6 pages

Project Oop Reprt

The document outlines a project for creating a simple Snake Game using C++. It details the game's implementation, features, and code structure, highlighting aspects such as snake movement, food generation, scoring, and collision detection. The project serves as an introduction to object-oriented programming concepts and provides a foundation for further development.

Uploaded by

Book Mart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Project Oop Reprt

The document outlines a project for creating a simple Snake Game using C++. It details the game's implementation, features, and code structure, highlighting aspects such as snake movement, food generation, scoring, and collision detection. The project serves as an introduction to object-oriented programming concepts and provides a foundation for further development.

Uploaded by

Book Mart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Fazaia Bilquis College of Education for Women

PAF Nur Khan


Submitted by:
Gul-e-Rana (17)
Alishba Shahid (1)
Class:
BS Computer Science
Section:
A
Course Name:
Object oriented programming
Submitted To:
Maam mehwish
Project
Snake game

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:

Ends the game when the snake collides.


Game loop:

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;

fruitX = rand() % width;

fruitY = rand() % height;

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;

fruitX = rand() % width;


fruitY = rand() % height;

score = 0;

The Draw function renders the game board on the console, displaying the snake, food, and
score

void Draw() {

system("cls");

for (int i = 0; i < width + 2; i++)

cout << "#";

cout << endl;

for (int i = 0; i < height; i++) {

for (int j = 0; j < width; j++) {

if (j == 0)

cout << "#";

if (i == y && j == x)

cout << "O";

else if (i == fruitY && j == fruitX)

cout << "F";

else {

bool printTail = false;

for (int k = 0; k < nTail; k++) {

if (tailX[k] == j && tailY[k] == i) {

cout << "o";


printTail = true;

if (!printTail)

cout << " ";

if (j == width - 1)

cout << "#";

cout << endl;

for (int i = 0; i < width + 2; i++)

cout << "#";

cout << endl;

cout << "Score:" << score << endl;

The Logic function updates the game state, moving the snake, checking for collisions, and
updating the score.

void Logic() {

int prevX = tailX[0];

int prevY = tailY[0];

int prev2X, prev2Y;

tailX[0] = x;
tailY[0] = y;

for (int i = 1; i < nTail; i++) {

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.

*****************************

You might also like