Skip to content

Commit 69825b0

Browse files
authored
PR #3: Hungry-Serpent-Snake-Game
Hungry Serpent Snake Game in CPP Merge pull request #3 from AnilKumarTeegala/Hungry_Serpent
2 parents 4cfbb8a + fc5b6dc commit 69825b0

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

Hungry_Serpent/Hungry_Serpent.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include <conio.h>
2+
#include <iostream>
3+
#include <windows.h>
4+
5+
using namespace std;
6+
7+
bool gameover;
8+
const int width = 20;
9+
const int height = 10;
10+
int x, y, fruitX, fruitY, score;
11+
int tailX[100], tailY[100];
12+
int nTail;
13+
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN };
14+
eDirecton dir;
15+
16+
void Setup() {
17+
gameover = false;
18+
dir = STOP;
19+
x = width / 2;
20+
y = height / 2;
21+
fruitX = rand() % width;
22+
fruitY = rand() % height;
23+
score = 0;
24+
}
25+
26+
void Draw() {
27+
system("cls"); // Clear the console window
28+
29+
for (int i = 0; i < width + 2; i++)
30+
cout << "#";
31+
cout << endl;
32+
33+
for (int i = 0; i < height; i++) {
34+
for (int j = 0; j < width; j++) {
35+
if (j == 0)
36+
cout << "#"; // Left wall
37+
if (i == y && j == x)
38+
cout << "O"; // Snake's head
39+
else if (i == fruitY && j == fruitX)
40+
cout << "F"; // Fruit
41+
else {
42+
bool print = false;
43+
for (int k = 0; k < nTail; k++) {
44+
if (tailX[k] == j && tailY[k] == i) {
45+
cout << "o"; // Snake's tail
46+
print = true;
47+
}
48+
}
49+
if (!print)
50+
cout << " ";
51+
}
52+
53+
if (j == width - 1)
54+
cout << "#"; // Right wall
55+
}
56+
cout << endl;
57+
}
58+
59+
for (int i = 0; i < width + 2; i++)
60+
cout << "#";
61+
cout << endl;
62+
63+
cout << "Score:" << score << endl;
64+
}
65+
66+
void Input() {
67+
if (_kbhit()) {
68+
switch (_getch()) {
69+
case 'a':
70+
dir = LEFT;
71+
break;
72+
case 'd':
73+
dir = RIGHT;
74+
break;
75+
case 'w':
76+
dir = UP;
77+
break;
78+
case 's':
79+
dir = DOWN;
80+
break;
81+
case 'x':
82+
gameover = true;
83+
break;
84+
}
85+
}
86+
}
87+
88+
void Logic() {
89+
int prevX = tailX[0];
90+
int prevY = tailY[0];
91+
int prev2X, prev2Y;
92+
tailX[0] = x;
93+
tailY[0] = y;
94+
for (int i = 1; i < nTail; i++) {
95+
prev2X = tailX[i];
96+
prev2Y = tailY[i];
97+
tailX[i] = prevX;
98+
tailY[i] = prevY;
99+
prevX = prev2X;
100+
prevY = prev2Y;
101+
}
102+
103+
switch (dir) {
104+
case LEFT:
105+
x--;
106+
break;
107+
case RIGHT:
108+
x++;
109+
break;
110+
case UP:
111+
y--;
112+
break;
113+
case DOWN:
114+
y++;
115+
break;
116+
default:
117+
break;
118+
}
119+
120+
if (x >= width)
121+
x = 0;
122+
else if (x < 0)
123+
x = width - 1;
124+
125+
if (y >= height)
126+
y = 0;
127+
else if (y < 0)
128+
y = height - 1;
129+
130+
for (int i = 0; i < nTail; i++)
131+
if (tailX[i] == x && tailY[i] == y)
132+
gameover = true;
133+
134+
if (x == fruitX && y == fruitY) {
135+
score += 10;
136+
fruitX = rand() % width;
137+
fruitY = rand() % height;
138+
nTail++;
139+
}
140+
}
141+
142+
int main() {
143+
Setup();
144+
while (!gameover) {
145+
Draw();
146+
Input();
147+
Logic();
148+
Sleep(10); // Add a slight delay to control the speed of the game
149+
}
150+
return 0;
151+
}

Hungry_Serpent/readme.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Hungry Serpent Game
2+
3+
Welcome to the Snake Game implemented in C++! This classic arcade game allows you to control a snake as it moves around the screen, eating food and growing longer with each meal. Be careful not to collide with the walls or yourself!
4+
5+
## How to Play
6+
7+
### Controls
8+
9+
- **'a'**: Move the snake left
10+
- **'d'**: Move the snake right
11+
- **'w'**: Move the snake up
12+
- **'s'**: Move the snake down
13+
- **'x'**: Quit the game
14+
15+
### Objective
16+
17+
- Eat the 'F' (fruit) to grow your snake.
18+
- Avoid running into the walls or the snake's own tail, as it will result in game over.
19+
20+
### Scoring
21+
22+
- Each 'F' eaten increases your score by 10 points.
23+
- The game speed may increase as your score grows, making it more challenging.
24+
25+
## Compiling and Running
26+
27+
To compile and run the game, follow these steps:
28+
29+
1. Compile the code using a C++ compiler like g++:
30+
```bash
31+
g++ -o snake Hungry_Serpent.cpp
32+
33+
2. Run the compiled program:
34+
```bash
35+
./snake
36+
```
37+
38+
## Requirements
39+
40+
- C++ compiler (e.g., g++)
41+
- Windows OS (for 'conio.h' library)

0 commit comments

Comments
 (0)