0% found this document useful (0 votes)
12 views

Here A Simple 2d Game

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Here A Simple 2d Game

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Here a simple 2d

game

Here’s an example of a simple snake game in C++ with some added


comments to make it clear and easier to understand.

This basic snake game runs in the console and uses ASCII characters to
display the game grid, the snake, and the food.

Explanation

Setup: Initializes the game settings, including the snake’s starting position
and the fruit’s random position.

Draw: Clears the console and redraws the game board, including the walls,
snake, and fruit.

Input: Detects key presses to change the direction of the snake (w, a, s, d for
movement and x to exit).

Logic: Moves the snake, handles collision detection (wall and self), and
updates the score if the snake eats the fruit.
In put:

#include <iostream>

#include <conio.h> // for _kbhit() and _getch() - check keypresses

#include <windows.h> // for Sleep() - delay function

using namespace std;

bool gameOver;

const int width = 20; // Width of game board

const int height = 17; // Height of game board

int x, y, fruitX, fruitY, score;

int tailX[100], tailY[100]; // Stores snake's tail positions

int nTail; // Current length of the tail

enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; // Directions

eDirection dir;

void Setup() {

gameOver = false;

dir = STOP; // Snake starts stationary

x = width / 2; // Initial snake head X position

y = height / 2; // Initial snake head Y position

fruitX = rand() % width; // Random fruit X position

fruitY = rand() % height; // Random fruit Y position


score = 0;

void Draw() {

system("cls"); // Clear the console

// Top wall

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

cout << "#";

cout << endl;

// Draw the game board with snake and fruit

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

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

if (j == 0)

cout << "#"; // Left wall

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

cout << "O"; // Snake's head

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

cout << "F"; // Fruit

else {

bool print = false;

for (int k = 0; k < nTail; k++) { // Print snake's tail

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

cout << "o";

print = true;

}
}

if (!print)

cout << " "; // Empty space

if (j == width - 1)

cout << "#"; // Right wall

cout << endl;

// Bottom wall

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

cout << "#";

cout << endl;

// Display the score

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

void Input() {

if (_kbhit()) { // Check if a key is pressed

switch (_getch()) { // Get the pressed key

case 'a':

dir = LEFT;

break;

case 'd':
dir = RIGHT;

break;

case 'w':

dir = UP;

break;

case 's':

dir = DOWN;

break;

case 'x':

gameOver = true; // Exit game

break;

void Logic() {

// Update the tail positions

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;

// Move the head

switch (dir) {

case LEFT:

x--;

break;

case RIGHT:

x++;

break;

case UP:

y--;

break;

case DOWN:

y++;

break;

default:

break;

// Check if the snake hits the wall

if (x >= width) x = 0; else if (x < 0) x = width - 1;

if (y >= height) y = 0; else if (y < 0) y = height - 1;

// Check if the snake hits itself


for (int i = 0; i < nTail; i++)

if (tailX[i] == x && tailY[i] == y)

gameOver = true;

// Check if snake eats the fruit

if (x == fruitX && y == fruitY) {

score += 10;

fruitX = rand() % width; // Generate new fruit position

fruitY = rand() % height;

nTail++; // Increase snake size

int main() {

Setup();

while (!gameOver) {

Draw();

Input();

Logic();

Sleep(100); // Game speed

return 0;

}
Out put :

How to Play

Use W, A, S, and D to move the snake up, left, down, and right.
Try to eat the fruit (F) to grow the snake and increase your score.

Avoid hitting the walls or your own tail to prevent the game from ending.

Requirements

This game needs Windows due to Sleep() from <windows.h> and _kbhit()
from <conio.h>. If you're using a non-Windows system, you may need
alternative libraries for those functions.

You might also like