/
FlappySpot.cpp
339 lines (322 loc) · 6.66 KB
/
FlappySpot.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "stdafx.h"
#include <iostream>
#include <cstdlib> // чтобы библиотеки тоже могли юзать std
#include <windows.h> // много разных макросов и штук, чтобы все работало
#include <conio.h> // для гетча и кбхита
#include <ctime> // очевидно
#include <fstream> // для работы с файлами
#include <iomanip> // это в общем тоже для времени
using namespace std;
ifstream inp;
ofstream outp;
char world[30][21];
int check[30][21];
int highscore;
int key = 0, score = 0, t = 0, ft = 0, x = 0, y = 0, godmode=0;
bool err;
void game();
void show_world();
void make_world();
void changepos();
bool gameover();
void checkscore();
void menu();
void endgame();
int main()
{
srand(time(0));
inp.open("C:/Users/user/Documents/Visual Studio 2015/Projects/ConsoleApplication16/ConsoleApplication16/highscore.txt");
if (inp.is_open())
{
inp >> highscore;
inp.close(); // открываем файл с хайскором, а если он не открылся (а потому что не захотел, кто его знает), то хайскор равен 0
err = false;
}
else
{
highscore = 0;
err = true;
}
int a = 0;
char sl;
while (1)
{
if (a == 0) goto play;
if (a>0)
{
score = 0;
cout << "Do you want to play again? [y/n] ";
cin >> sl;
if (sl == 'n') return 0;
else goto play;
}
play:
menu();
cin >> sl; // меню, все дела
switch (sl)
{
case '1':
{
game();
break;
}
case '2':
{
return 0;
break;
}
default:
{
goto play;
break;
}
}
a++;
}
}
void game() // скажем так "движок" игры
{
int i, j;
char s;
for (j = 0; j<21; j++)
{
for (i = 0; i<30; i++)
{
if (j<20)
{
world[i][j] = ' ';
check[i][j] = 0;
}
if (j == 20)
{
world[i][j] = '#';
check[i][j] = 2;
}
}
}
world[10][10] = '@';
show_world();
while (1)
{
s = '~';
Sleep(0.2 * 1000); // тут можно поменять скорость игры
t++;
if (_kbhit())
{
s = _getch();
if (s != '~') key = 1; // можем не париться с управлением и играть любой кнопкой кроме тильды
if (s == 'g') godmode = 1;
}
for (i = 0; i<30; i++)
{
world[i][20] = '#';
check[i][20] = 2;
}
changepos();
checkscore();
if (gameover() == true)
{
if (score>highscore) highscore = score;
if (err == false)
{
outp.open("C:/Users/user/Documents/Visual Studio 2015/Projects/ConsoleApplication16/ConsoleApplication16/highscore.txt");
outp << highscore;
outp.close();
}
show_world();
endgame();
return;
}
make_world();
if (score>highscore) highscore = score;
show_world();
if (key>0) key++;
if (key == 3) key = 0;
}
}
void show_world() //рисуем мир и кол-во очков
{
int i, j;
system("cls");
for (j = 0; j<21; j++)
{
for (i = 0; i<30; i++)
{
if (i<29) cout << world[i][j];
if (i == 29) cout << world[i][j] << endl;
}
}
cout << "" << endl;
cout << "Your Score: " << score;
if (godmode == 1) cout << "\n HAHA NOW YOU CANNOT DIE CAUSE YOU'RE GOD MWAHAHHAH \n (it means that you have to shut down the game to quit or play forever)";
}
void make_world() // делаем стены и двигаем их
{
int i, j, centre;
if (t == 10)
{
centre = (rand() % 11) + 5;
for (j = 0; j<20; j++)
{
world[29][j] = '#';
check[29][j] = 2;
}
world[29][centre - 1] = ' ';
world[29][centre] = ' ';
world[29][centre + 1] = ' ';
check[29][centre - 1] = 0;
check[29][centre] = 0;
check[29][centre + 1] = 0;
t = 0;
}
{
for (j = 0; j<20; j++)
{
for (i = 0; i<30; i++)
{
if (world[i][j] == '#')
{
if ((i>0) && (godmode==0 || world[i-1][j] != '@'))
{
world[i - 1][j] = '#';
check[i - 1][j] = 2;
world[i][j] = ' ';
check[i][j] = 0;
}
if (i == 0)
{
world[i][j] = ' ';
check[i][j] = 0;
}
}
}
}
}
}
void changepos() // птица летает вверх/вниз
{
int i, j;
if (key>0)
{
ft = 0;
for (j = 0; j<20; j++)
{
for (i = 0; i<30; i++)
{
if (world[i][j] == '@')
{
if (j>0)
{
world[i][j - 1] = '@';
world[i][j] = ' ';
x = i;
y = j - 1;
return;
}
}
}
}
}
else
{
ft++;
for (j = 0; j<20; j++)
{
for (i = 0; i<30; i++)
{
if (world[i][j] == '@')
{
if (j<20)
{
if (ft<3)
{
world[i][j + 1] = '@';
world[i][j] = ' ';
x = i;
y = j + 1;
return;
}
else if (ft>2 && ft<5)
{
world[i][j + 2] = '@';
world[i][j] = ' ';
x = i;
y = j + 2;
return;
}
else if (ft>4)
{
world[i][j + 3] = '@';
world[i][j] = ' ';
x = i;
y = j + 3;
return;
}
}
else
{
return;
}
}
}
}
}
}
void checkscore() // тут понятно
{
int j;
for (j = 0; j<20; j++)
{
if (world[x][j] == '#')
{
score++;
return;
}
}
}
bool gameover() //проверка на целостность птичьей головы
{
int f = 0;
if (godmode == 1) return false; // хех
if (y>19)
{
world[x][19] = '@';
world[x][20] = '#';
f = 1;
if (f == 1) return true;
else return false;
}
else
{
if (check[x][y]>0 && (world[x][y] == '#' || world[x][y] == '@'))
{
world[x][y] = '#';
world[x - 1][19] = '@';
f = 1;
if (f == 1) return true;
else return false;
}
}
}
void endgame() // очевидно
{
show_world();
cout << "" << endl << endl;
cout << "GAME OVER ";
cout << "" << endl << endl;
cout << "YOUR SCORE: " << score << endl << endl;
cout << "HIGH SCORE: " << highscore << endl;
cout << "" << endl << endl;
}
void menu() // тоже очевидно
{
system("cls");
cout << "" << endl;
cout << "FLAPPY SPOT";
cout << "" << endl << endl;
cout << "High Score: " << highscore << endl << endl;
cout << "" << endl << endl;
cout << "MENU: " << endl << endl;
cout << "1: Start Game " << endl << endl;
cout << "2: Exit " << endl << endl;
}