-
Notifications
You must be signed in to change notification settings - Fork 47
/
Snake.cpp
162 lines (154 loc) · 3.41 KB
/
Snake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <bits/stdc++.h>
#include <conio.h>
using namespace std;
bool gameOver;
unsigned long speed_snake;
const int height = 20, width = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int ntail;
enum eDirection{STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir, prevdir;
void Setup()
{
speed_snake = 60;
srand(time(0));
ntail = 0;
gameOver = false;
dir = STOP;
prevdir = STOP;
x = width / 2;
y = height / 2;
fruitX = rand() % (width - 2);
fruitY = rand() % (height - 2);
score = 0;
}
void Draw()
{
system("cls");
for(int i = 0; i<width+1; i++)
cout << '#';
cout << endl;
for(int i = 0; i < height ; i++)
{
for(int j = 0; j < width ; j++)
{
if(j == 0 || j == width-1)
cout << '#';
if(x == j && y == i)
cout << 'O';
else if(fruitX == j && fruitY == i)
cout << 'f';
else
{
bool print = false;
for(int k = 0; k < ntail; k++)
{
if(tailX[k] == j && tailY[k] == i)
{
print = true;
cout << "o";
}
}
if(!print)
cout << ' ';
}
}
cout << endl;
}
for(int i = 0 ; i < width+1 ; i++)
cout << '#';
cout << "\nScore: " << score << endl;
}
void Input()
{
if(_kbhit())
{
switch(_getch())
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 's':
dir = DOWN;
break;
case 'w':
dir = UP;
break;
case 'x':
gameOver = true;
break;
}
}
}
void Logic()
{
for(int i = ntail ; i > 0; i--)
{
tailX[i]=tailX[i-1];
tailY[i]=tailY[i-1];
}
tailX[0] = x;
tailY[0] = y;
switch(dir)
{
case LEFT:
if(prevdir == RIGHT && ntail > 0)
{
dir = prevdir; x++; break;
}
x--; break;
case RIGHT:
if(prevdir == LEFT && ntail > 0)
{
dir = prevdir; x--; break;
}
x++; break;
case UP:
if(prevdir == DOWN && ntail > 0)
{
dir = prevdir; y++; break;
}
y--; break;
case DOWN:
if(prevdir == UP && ntail > 0)
{
dir = prevdir; y--; break;
}
y++; break;
}
if(x > width-3) x = 0;
else if(x < 0) x = width-2;
else if(y < 0) y = height;
else if(y > height - 1) y = 0;
for(int j = 0;j<ntail;j++)
if(x == tailX[j] && y == tailY[j])
gameOver = true;
if(x == fruitX && y == fruitY)
{
score+=10;
srand(time(0));
fruitX = rand() % (width - 2);
fruitY = rand() % (height - 2);
ntail++;
if(speed_snake > 15 && score%50 == 0)
speed_snake -= 10;
}
prevdir = dir;
}
int main()
{
Setup();
while(!gameOver)
{
Draw();
Input();
Logic();
_sleep(speed_snake);
}
cout << "\nGOOD GAME";
return 0;
}