Skip to content

Commit fc5b6dc

Browse files
Update Hungry_Serpent.cpp
1 parent 7b4e2f0 commit fc5b6dc

File tree

1 file changed

+123
-139
lines changed

1 file changed

+123
-139
lines changed

Hungry_Serpent/Hungry_Serpent.cpp

Lines changed: 123 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <iostream>
21
#include <conio.h>
2+
#include <iostream>
33
#include <windows.h>
44

55
using namespace std;
@@ -13,155 +13,139 @@ int nTail;
1313
enum eDirecton { STOP = 0, LEFT, RIGHT, UP, DOWN };
1414
eDirecton dir;
1515

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;
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;
2524
}
2625

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
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+
}
6248
}
63-
cout << endl;
64-
}
49+
if (!print)
50+
cout << " ";
51+
}
6552

66-
for (int i = 0; i < width + 2; i++)
67-
cout << "#";
53+
if (j == width - 1)
54+
cout << "#"; // Right wall
55+
}
6856
cout << endl;
57+
}
6958

70-
cout << "Score:" << score << endl;
71-
}
59+
for (int i = 0; i < width + 2; i++)
60+
cout << "#";
61+
cout << endl;
7262

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-
}
63+
cout << "Score:" << score << endl;
9664
}
9765

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;
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;
13184
}
85+
}
86+
}
13287

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-
}
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+
}
154140
}
155141

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;
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;
167151
}

0 commit comments

Comments
 (0)