-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
219 lines (200 loc) · 4.77 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <ctime>
#include <vector>
#include <windows.h>
#define HEIGHT 20
#define WIDTH 40
#define FRAMERATE 10
class bodyPart
{
public:
char shape;
int x, y;
bodyPart(char shape, int x, int y)
{
this -> shape = shape;
this -> x = x;
this -> y = y;
}
bodyPart(char shape)
{
this -> shape = shape;
}
};
class coordinates
{
public:
int x,y;
coordinates(int x, int y)
{
this -> x = x;
this -> y = y;
}
};
class fClock
{
private:
time_t initTime;
public:
fClock()
{
initTime = clock();
}
void restart()
{
initTime = clock();
}
clock_t getElapsedTime()
{
return (clock() - initTime);
}
};
char initChar, ScreenGrid[HEIGHT + 2][WIDTH + 2];
int direction = VK_RIGHT, fruitPosNum;
std::vector<bodyPart> Body;
std::vector<coordinates> spaces;
bodyPart Fruit('*');
fClock FRControl;
void clearGrid();
void addBody();
void addItem(bodyPart part);
void updatePartsLocation();
void draw();
void generateFruit();
void getKey();
void updateHeadLocation();
void gameOver();
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
srand((unsigned) time(0));
Body.push_back(bodyPart('0', WIDTH/2, HEIGHT/2));
for(int i = 0; i < 3; i++) Body.push_back(bodyPart('o', Body[0].x - (i+1), Body[0].y));
clearGrid();
addBody();
generateFruit();
while(true)
{
if(FRControl.getElapsedTime() >= (1000 / FRAMERATE))
{
FRControl.restart();
if(Body[0].x == Fruit.x && Body[0].y == Fruit.y)
{
Body.push_back(bodyPart('o'));
generateFruit();
}
getKey();
updatePartsLocation();
updateHeadLocation();
if(Body[0].x == 0 || Body[0].x >= WIDTH + 1 || Body[0].y == 0 || Body[0].y >= HEIGHT + 1) gameOver();
for(unsigned int i = 1; i < Body.size(); i++)
{
if(Body[0].x == Body[i].x && Body[0].y == Body[i].y) gameOver();
}
clearGrid();
addItem(Fruit);
addBody();
system("cls");
draw();
}
}
return 0;
}
void clearGrid()
{
for(int i = 0; i < HEIGHT + 2; i++)
{
if(i == 0 || i == HEIGHT + 1) initChar = '#';
else initChar = ' ';
for(int j = 0; j < WIDTH + 2; j++)
{
if(j == 0 || j == WIDTH + 1) ScreenGrid[i][j] = '#';
else ScreenGrid[i][j] = initChar;
}
}
}
void draw()
{
for(int i = 0; i < HEIGHT + 2; i++)
{
for(int j = 0; j < WIDTH + 2; j++)
std::cout << ScreenGrid[i][j];
std::cout << std::endl;
}
}
void addBody()
{
for(unsigned int i = 0; i < Body.size(); i++) ScreenGrid[Body[i].y][Body[i].x] = Body[i].shape;
}
void addItem(bodyPart part)
{
ScreenGrid[part.y][part.x] = part.shape;
}
void updatePartsLocation()
{
for(int i = Body.size() - 1; i > 0; i--)
{
Body[i].x = Body[i-1].x;
Body[i].y = Body[i-1].y;
}
}
void generateFruit()
{
spaces.clear();
for(int i = 1; i <= HEIGHT; i++)
{
for(int j = 1; j <= WIDTH; j++)
{
if(ScreenGrid[i][j] == ' ')
spaces.push_back(coordinates(j, i));
}
}
fruitPosNum = rand() % spaces.size();
Fruit.x = spaces[fruitPosNum].x;
Fruit.y = spaces[fruitPosNum].y;
}
void getKey()
{
if(GetAsyncKeyState(VK_UP) && direction != VK_DOWN) direction = VK_UP;
if(GetAsyncKeyState(VK_DOWN) && direction != VK_UP) direction = VK_DOWN;
if(GetAsyncKeyState(VK_RIGHT) && direction != VK_LEFT) direction = VK_RIGHT;
if(GetAsyncKeyState(VK_LEFT) && direction != VK_RIGHT) direction = VK_LEFT;
}
void updateHeadLocation()
{
switch(direction)
{
case VK_UP:
Body[0].y--;
break;
case VK_DOWN:
Body[0].y++;
break;
case VK_RIGHT:
Body[0].x++;
break;
case VK_LEFT:
Body[0].x--;
break;
}
}
void gameOver()
{
int Dots = 0;
while(true)
{
if(FRControl.getElapsedTime() >= (1000 / FRAMERATE))
{
FRControl.restart();
system("cls");
std::cout << "\n\n\n\n\n\n\n\n\n\n ";
std::cout << "*** GAME OVER ***\n\n\n\n\n\n\n\n press ENTER to exit\n ";
for(int i=0; i<Dots; i++) std::cout << '.';
std::cout << std::endl;
if(Dots>=19) Dots=0;
else Dots++;
}
if(GetAsyncKeyState(VK_RETURN)) exit(0);
}
}