0% found this document useful (0 votes)
62 views1 page

PDF Snake Game CDR

The document is a C++ implementation of a simple snake game. It includes functions for input handling, game setup, rendering the game board, and the main game loop. The game allows the player to control the snake to eat fruit and grow while avoiding collisions with itself and the walls.

Uploaded by

suniyo gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views1 page

PDF Snake Game CDR

The document is a C++ implementation of a simple snake game. It includes functions for input handling, game setup, rendering the game board, and the main game loop. The game allows the player to control the snake to eat fruit and grow while avoiding collisions with itself and the walls.

Uploaded by

suniyo gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include < iostream > void Input ()

- Eat fruit → score++


#include < conio.h > {

- Collision check
- Move snake
- Wrap edges
while !gameover
using namespace std;z if (_kbhit ()) {

algorithm():
Start Game

End Game
- Place fruit
- Init vars
switch (_getch ())

Setup():

no
bool gameover; {
const int width = 20;

- Check keypress
- Update direction
case 'a':
const int height = 17; dir = LEFT;

yes

Input():
break;
int x, y, fruitX, fruitY, score; case 'd':
int tailX[100], tailY[100]; dir = RIGHT;

- Render game board


//snake coordinates break;

- Show score
case 'w':
int nTail; dir = UP;

Draw():
enum eDirecton break;
{STOP = 0, LEFT,RIGHT, UP, DOWN}; case 's':
// Controls dir = DOWN ;
break;
eDirecton dir; case 'x':
void Setup() { gameover = true;
gameover = false; break;
} } } +-----------start game-----------------+
dir = STOP; void algorithm() | Setup() |
x = width / 2; { | - Init vars, position |
y = height / 2; int prevX = tailX [0];
int prevY = tailY [0]; | - Generate fruit position |
fruitX = rand() % width;
//display fruit in a random place
int prev2X, prev2Y; +----------------------------+
tailX[0] = x; |
fruitY = rand() % height; score = 0; tailY[0] = y;
} for(int i = 1;i < nTail ; i++) { v
void Draw() { prev2X = tailX[i];z +----------------------------+
prev2Y = tailY[i]; | while !gameover |
system("cls");
for(int i = 0; i < width+2; i++) tailX[i] = prevX; +----------------------------+
cout << "#"; |
tailY[i] = prevY; v
cout << endl ;
for (int i = 0; i < height ; i++) { prevX = prev2X; +----------------------------+
for (int j = 0; j < width; j++) { | Draw() |
prevY = prev2Y ; | - Print walls, snake, fruit|
if (j == 0)
cout << "#"; //walls } | - Show score |
if (i == y && j == x) switch (dir) { +----------------------------+
case LEFT:
cout << "*"; // snake tale x--; |
else if (i == fruitY && j == fruitX )break; v
case RIGHT: +----------------------------+
cout << "%"; x++; | Input() |
// change it to change the fruit break; | - If key pressed, |
else { case UP:
y--; | update direction or exit |
bool print = false; break; +----------------------------+
for (int k = 0; k< nTail ; k++) { case DOWN: |
if (tailX [k] == j && tailY [k] == i) { y++; v
break;
cout << "*"; print = true; default:
+----------------------------+
} break; | algorithm() |
} } +----------------------------+
if (x >= width) x =0;else if (x <0) x = width -1;
if (y >= height) y = 0; else if (y < 0) y = height - |
1; - Move snake head/tail |
if (!print) cout << " "; for (int i =0; i< nTail ;i++) | - Change dir based on input|
} if (tailX[i] == x && tailY[i] == y) | - Wrap at edges |
if (j == width -1) gameover = true;
| - Check self-collision |
cout << "#"; if (x == fruitX && y == fruitY) { | - Eat fruit score + tail |
} score +=10; +----------------------------+
cout << endl; fruitX = rand() % width; |
fruitY = rand() % height; v
} nTail ++; +----------------------------+
for (int i = 0; i< width+2; i++) }} int main()
| Loop repeats until |
{
cout << "#"; Setup(); | gameover = true |
cout << endl; while (!gameover) { +----------------------------+
Draw (); |
cout << "Score:" Input ();
<< score << endl ; algorithm (); v
} +----------------------------+
} return 0; | End Game |
} +----------------------------+

You might also like