Skip to content

Commit 10361b3

Browse files
Added Hungry_Serpent source file
1 parent 4cfbb8a commit 10361b3

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

Hungry_Serpent/Hungry_Serpent.cpp

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

0 commit comments

Comments
 (0)