-
Notifications
You must be signed in to change notification settings - Fork 0
/
Console_Runner_buffer.cpp
759 lines (686 loc) · 19.7 KB
/
Console_Runner_buffer.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <thread>
#include <chrono>
#include <fstream>
#define FPS 24 //set game FPS (speed of gamplay too =\ )
using namespace std;
typedef unsigned long long ull;
typedef chrono::system_clock::time_point time_p;
typedef chrono::duration<chrono::system_clock::rep, chrono::system_clock::period> duration;
const ull mod_rng = 0x10000000000; //2^40 modulus for RNG engine!
const char quit_key = 'q';
const char jump_key = ' ';
const char pl_avatar = '@';
const char ground = '='; //maybe procedural generation of ground etc. via loop??? :MeFromFuture: "OH, yea!!"
const char wall = '|';
const char ramp_up = '/';
const short int gravity = 1;
struct resolution { const short x = 90; const short y = 30; const short sqr = y*x; } res; //resolution
const string original_map = {
" "//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
};
string map; // screen buffer
ull getSeed()
{
chrono::system_clock::time_point cur_time = chrono::system_clock::now();
chrono::system_clock::duration seed = cur_time.time_since_epoch();
return seed.count()%100000000000000/100; //max seed 1*10^12 - 1
}
//Linear Congruential Pseudo Random Number Generator
ull rng(int seed) //seed must be 0 <= seed < mod!
{ //multiplyer also must be multi%8 = 5 !!! For best uniformal distribution!
const ull multi = 0x4C8F53A795; //multiplier 2.|multi - 1 must be % = 0 for all mod's prime factors|
const ull inc = 1; //increment 1.|inc and mod must be co-prime|
const ull mod = mod_rng; //2^40 modulus 3.|multi must be %4 = 0 if mod%4 = 0|
return (multi * seed + inc) % mod;
}
const ull seed = getSeed();
ull rnd_num = seed;
//uniform random number distribution of range
int rngRange(int min, int max) //min,max must be non negative!
{
const ull rngrg = mod_rng;//RNG range
int range = max - min + 1;
ull scale = rngrg/range; //big range part to 1 part of range
rnd_num = rng(rnd_num);
return rnd_num/scale + min; //downscale
}
class Player
{
const int x;
int y;
int v_spd; //vertical speed
int d_jumps; //double jumps
int dist_run; //distanced runned
bool can_d_jump;
char icon;
public:
Player() : x(24)
{
y = 26; //resolution sensetive
v_spd = dist_run = 0;
d_jumps = 3;
can_d_jump = true;
icon = pl_avatar;
}
void Put_in_map()
{
checkForMoney();
applyPhysics();
int cur_pos = getPos(0); //current position
if(!v_spd) //insert in map
{
if(map[cur_pos+1] == ramp_up) //go up ramp
map[cur_pos] = icon, --y;
else
map[cur_pos] = icon;
}
else
map[getPos(1)] = icon;
++dist_run; //log distance
}
void Jump()
{
v_spd = 3; //jumping FORCE!
if(flying()) ++v_spd;
}
bool Is_Dead()
{
bool wall_hit, pit_fall; //death causes
wall_hit = map[getPos(0)+1] == wall || map[getPos(0)+1] == ground ? true : false;
pit_fall = getPos(0) > res.sqr ? true : false;
return wall_hit || pit_fall;
}
bool Can_Jump()
{
bool can_j = !flying() ? true : false;
if(!can_d_jump && !flying()) can_d_jump = true;
if(flying() && d_jumps > 0 && can_d_jump)
{
can_j = true;
can_d_jump = false;
--d_jumps;
}
return can_j;
}
void DisplayStats()
{
cout << "Money: $" << d_jumps << " ";
cout << "Distance traversed: ";
if(dist_run < 1000) //convert to km
cout << dist_run << " m" << endl;
else if(dist_run%1000 == 0)
cout << dist_run/1000 << " km " << endl; //8 spaces to erase trash meters sighn
else
cout << dist_run/1000 << " km " << dist_run%1000 << " m" << endl;
}
int GetDistRun()
{
return dist_run;
}
private:
bool flying(int dst = 1, int x_off = 0) //is player in air? par: distance down, xoffset
{
int u_pos = getPos(0, y+dst)+x_off; //position under player
return map[u_pos] == ' ' || map[u_pos] == '$' || u_pos > res.sqr ? true : false;
}
void applyPhysics() //PHYSICS ENGINE!
{
if(flying() && flying(1, -1)) //check old position too for edges!
v_spd -= gravity; //apply gravity
if(!flying() && v_spd < 0) //not flying and negative speed
v_spd = 0;
else if(v_spd < 0)
{
for(int i = -2; i >= v_spd-1; --i)
{
if (!flying(abs(i)))
{
y -= i + 1;
v_spd = 0;
}
}
}
}
void checkForMoney() //check for money i.e. double jump power up
{
if(v_spd > 0)
{
for(int i = y-1; i >= y-v_spd; --i)
{
int tmp_pos = getPos(0, i);
if(map[tmp_pos] == '$')
{
++d_jumps;
map[tmp_pos] = ' ';
}
}
}
else if(v_spd < 0)
{
for(int i = y+1; i <= y+v_spd; ++i)
{
int tmp_pos = getPos(0, i);
if(map[tmp_pos] == '$')
{
++d_jumps;
map[tmp_pos] = ' ';
}
}
}
else
{
int tmp_pos = getPos(0) + 1;
if(map[tmp_pos] == '$') ++d_jumps;
if(tmp_pos == ramp_up && map[getPos(0, y-1)+1] == '$') ++d_jumps;
}
}
int getPos(bool new_or_cur, int Y)
{
if(new_or_cur)
return x + (Y -= v_spd) * res.x;
return x + Y * res.x;
}
int getPos(bool new_or_cur) //get position
{
if(new_or_cur)
return x + (y -= v_spd) * res.x;
return x + y * res.x;
}
};
class Obstacle
{
const int y_max;
const int y_min;
int y;
int y_end;
int length;
int old_length; //tmp var for some obstacles
int height;
short int obs_index; //obstacle index, 0 = none
int getPos()
{
return res.x - 1 + y * res.x;
}
int getPos(int Y) //custom y
{
return res.x - 1 + Y * res.x;
}
void genGround() //generate ground
{
if(length < 0) length = rngRange(5, 16); //ground min max length
map[getPos()] = ground;
if(!length--) obs_index = 0;
}
void genHole() //generate pit
{
if(length < 0) length = rngRange(2, 6), old_length = length; //pit min max size
if(length == old_length || !length)
{
map[getPos()] = ground;
for(int i = y+1; i < res.y; ++i)
map[getPos(i)] = wall;
}
else
map[getPos()] = ' ';
if(!length--) obs_index = 0;
}
void genColumn() //generate column(pillar)
{
if(length < 0) length = rngRange(1, 10), old_length = length; //column min max length
if(height < 0) height = rngRange(2, 6); //column min max height
if(length == old_length || !length)
{
map[getPos()] = ground;
if(!length) y += height;
for(int i = 1; i < height; ++i)
map[getPos(y-i)] = wall;
if(length == old_length) y -= height;
map[getPos()] = ground;
}
else
map[getPos()] = ground;
if(!length--) obs_index = 0, height = -1;
}
void genUpHill()
{
if(length < 0 && y == y_max) //if y is at max, get another obstacle!
{
obs_index = 0;
map[getPos()] = ground;
return;
}
if(length < 0)
{
length = rngRange(4, 7);
old_length = rngRange(4, 7);
}
if(height < 0)
{
int tmp = y-y_max+1;
if(tmp >= 3) height = rngRange(3, tmp);
else height = 2;
}
if(old_length)
{
map[getPos()] = ground;
--old_length;
}
else if(height)
{
map[getPos()] = ramp_up;
--height;
--y;
if(!height) ++y;
}
else
{
map[getPos()] = ground;
--length;
}
if(!length) obs_index = 0, length = height = -1;
}
void genDownHill()
{
if(length < 0 && y == y_min) //if y is at min, get another obstacle!
{
obs_index = 0;
map[getPos()] = ground;
return;
}
if(length < 0)
{
length = rngRange(4, 7);
old_length = rngRange(4, 7);
}
if(height < 0)
{
int tmp = y_min-y+1;
if(tmp >= 3) height = rngRange(3, tmp);
else height = 2;
}
if(old_length)
{
map[getPos()] = ground;
--old_length;
}
else if(height)
{
map[getPos()] = '\\';
--height;
++y;
if(!height) --y;
}
else
{
map[getPos()] = ground;
--length;
}
if(!length) obs_index = 0, length = height = -1;
}
void genCliff()
{
if(y >= (y_min-y_max)/2 + y_max)
{
height = rngRange(2, 6); //cliff min max height left
map[getPos()] = ground;
for(int i = y-1; i > y-height; --i)
map[getPos(i)] = wall;
y -= height;
map[getPos()] = ground;
obs_index = 0;
height = -1;
}
else
{
int tmp = y_min-y;
height = rngRange(2, tmp); //cliff min max height right
map[getPos()] = ground;
for(int i = y+1; i < y+height; ++i)
map[getPos(i)] = wall;
y += height;
map[getPos()] = ground;
obs_index = 0;
height = -1;
}
}
void genDoubleJump()
{
int y_min_tmp = y - 1;
if(y > 12)
{
int y_max_tmp = y - 12;
map[getPos(rngRange(y_max_tmp, y_min_tmp))] = '$';
}
else
map[getPos(rngRange(0, y_min_tmp))] = '$';
}
public:
Obstacle() : y_max(17), y_min(27) //MIN/MAX height of obstacles resolution sensetive
{
y = y_min;
length = y_end = height = -1;
obs_index = 0;
}
void Put_in_map()
{
if(!obs_index) obs_index = rngRange(1, 6); //RNG
int dice60 = rngRange(1, 60); //roll 60 sided dice
if(dice60 == 31) genDoubleJump(); //spawning double jump bonus
switch(obs_index)
{
case 1: genGround();
break;
case 2: genHole();
break;
case 3: genColumn();
break;
case 4: genUpHill();
break;
case 5: genDownHill();
break;
case 6: genCliff();
break;
}
}
};
void showCursor(bool showFlag) //delete the cursor, muahha!
{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(out, &cursorInfo);
cursorInfo.bVisible = showFlag;
SetConsoleCursorInfo(out, &cursorInfo);
}
void resetCursor(short y = 0) //reset console cursor to (0,0)
{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
COORD home_coord = {0,y};
SetConsoleCursorPosition(out, home_coord);
}
void drawMap()
{
HANDLE hOutput = (HANDLE)GetStdHandle(STD_OUTPUT_HANDLE);
COORD dwBufferSize = { res.x, res.y };
COORD dwBufferCoord = { 0, 1 };
SMALL_RECT rcRegion = { 0, 2, (SHORT)(res.x), (SHORT)(res.y) };
CHAR_INFO buffer[res.sqr];
for(int i = 0; i < res.sqr; ++i)
{
buffer[i].Char.AsciiChar = map[i];
buffer[i].Attributes = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
}
WriteConsoleOutput(hOutput, buffer, dwBufferSize,
dwBufferCoord, &rcRegion);
}
// redundant with the use of WIN API console buffer
/*void drawMap()
{
for(int i = 0; i < res.sqr; ++i)
{
if(i > 0 && i % res.x == 0)
cout << endl;
cout << map[i];
}
}*/
void moveMap()
{
for(int i = 0; i < res.sqr; ++i)
{
if(map[i] != ' ' && i % res.x) //NOT space AND NOT first element of row
{
if(map[i] == pl_avatar) map[i] = ' '; //clear player's "tail"
map[i-1] = map[i];
map[i] = ' ';
}
else if(i%res.x == 0 && map[i+1] == ' ') //IS first AND nothing ahead
map[i] = ' ';
}
}
void insertText(string& m_map, string& text, int y) //insert text in menu dummy map
{
int mid_pos = res.x * y + res.x/2 - text.size()/2 + 1; //calculate middle position
for(unsigned int i = mid_pos, j = 0; j < text.size(); ++j, ++i)
m_map[i] = text[j];
}
void drawMenu()
{
string menu_map = original_map;
string msg1 = "Wellcome to Console Runner!";
string msg2 = "You escaped from Prison Castle, try to run as far as you can.";
string msg3 = "Space to jump. Double jumps will cost you money.";
string msg4 = "Further you go, faster you run.";
string msg5 = "Press any key to start, press Q to exit.";
insertText(menu_map, msg1, 8);
insertText(menu_map, msg2, 10);
insertText(menu_map, msg3, 11);
insertText(menu_map, msg4, 12);
insertText(menu_map, msg5, 16);
menu_map[2364] = pl_avatar;
cout << endl;
for(int i = 0; i < res.sqr; ++i)
{
if(i > 0 && i % res.x == 0)
cout << endl;
cout << menu_map[i];
}
}
void insertGameOverBox()
{
string text[10];
text[0] = "______________________________";
text[1] = "| |";
text[9] = "|____________________________|";
text[2] = text[3] = text[4] = text[5] = text[6] = text[7] = text[8] = text[1];
for(int i = 0; i < 10; ++i)
insertText(map, text[i], 6+i);
}
string convertToString(int& score) //convert to km and string
{
if(score < 1000)
return to_string(score) + " m";
else if(score%1000 == 0)
return to_string(score/1000) + " km";
else
return to_string(score/1000) + " km, " + to_string(score%1000) + " m";
}
void gameOver(int new_score)
{
fstream score("high_scores.txt"); //open file
int high_score = 0;
string date;
chrono::system_clock::time_point today = chrono::system_clock::now();
long tt = chrono::system_clock::to_time_t(today);
string cur_date = ctime(&tt);
cur_date.pop_back(); //delete newline
if(score.is_open()) //file exist
{
score.seekg(0, ios::end);
if(score.tellg()) //file is not empty
{
score.seekg(0, ios::end);
char tmp = '~';
while(tmp != '\n')
{
score.seekg(-3, ios::cur); //-3 offset anomaly! 0_0
if((int)score.tellg() <= 0) //start of file is start of line
{
score.seekg(0);
break;
}
tmp = score.get();
}
score >> high_score;
char date_buff[30];
score.getline(date_buff, 50);
date = date_buff;
date.erase(0, 1); //erase space in the beginning
}
}
else //create file, if there is no file
{
score.open("high_scores.txt", ios::out);
score.close();
score.open("high_scores.txt");
}
resetCursor(1);
if(new_score > high_score)
{
string msg1 = "Congratulations!";
string msg2 = "New Record: ";
msg2 += convertToString(new_score) + '!';
string msg3 = "Press P to reset scores,";
string msg4 = "Q to quit, R to restart...";
insertGameOverBox();
insertText(map, msg1, 8);
insertText(map, msg2, 9);
insertText(map, cur_date, 11);
insertText(map, msg3, 13);
insertText(map, msg4, 14);
drawMap();
score << new_score << ' ' << cur_date << endl;
}
else if(new_score < high_score)
{
string msg1 = "Game over, man, GAME OVER!";
string msg2 = "Last Record: ";
msg2 += convertToString(high_score) + '!';
string msg3 = "Press P to reset scores,";
string msg4 = "Q to quit, R to restart...";
insertGameOverBox();
insertText(map, msg1, 8);
insertText(map, msg2, 9);
insertText(map, date, 11);
insertText(map, msg3, 13);
insertText(map, msg4, 14);
drawMap();
}
else
{
string msg1 = "Good, you got last record!";
string msg2 = "Last Record: ";
msg2 += convertToString(high_score) + '!';
string msg3 = "Press P to reset scores,";
string msg4 = "Q to quit, R to restart...";
insertGameOverBox();
insertText(map, msg1, 8);
insertText(map, msg2, 9);
insertText(map, date, 11);
insertText(map, msg3, 13);
insertText(map, msg4, 14);
drawMap();
}
score.close();
}
char handleInput(Player& player)
{
char key_prs = '~';
if(_kbhit())
key_prs = _getch();
if(key_prs == jump_key)
{
if(player.Can_Jump()) player.Jump();
key_prs = '~';
}
return key_prs;
}
void gameLoop(char& key_prs)
{
Player dog; // @ - is a dog!
Obstacle obst;
map = original_map;
duration f_time = chrono::milliseconds(1000/FPS); // set time per frame (FPS)
while(key_prs != 'r' && key_prs != quit_key)
{
time_p start = chrono::system_clock::now();
key_prs = handleInput(dog); // process user input
/* UPDATE */
moveMap(); // shift map left by 1 char
obst.Put_in_map(); //procedual map generation
dog.Put_in_map();
/* END UPDATE */
/* RENDER */
resetCursor();
dog.DisplayStats();
drawMap();
/* END RENDER */
if(dog.Is_Dead())
{
gameOver(dog.GetDistRun()-1);
break;
}
time_p end = chrono::system_clock::now();
auto sleep_time = start + f_time - end;
this_thread::sleep_for(sleep_time); //framerate!
}
}
int main()
{
showCursor(false); //hide console cursor
system("mode 91, 31"); //set console window size
char key_prs; //key pressed
drawMenu();
if(_getch() == quit_key) return 0;
resetCursor();
while(key_prs != quit_key)
{
key_prs = '~';
gameLoop(key_prs);
if(key_prs == '~')
{
while(true)
{
key_prs = _getch();
if(key_prs == 'p') //reset highscores
{
fstream scores("high_scores.txt", ios::trunc | ios::out);
scores.close();
resetCursor(1);
string txt1 = "| |";
string txt2 = "| High scores reset! |";
string txt3 = "|____________________________|";
insertText(map, txt1, 16);
insertText(map, txt2, 17);
insertText(map, txt3, 18);
drawMap();
}
if(key_prs == 'r' || key_prs == quit_key) break; //MAKE RESET HIGHSCORES KEY VIA TRUNCATE!
}
}
system("CLS");
}
showCursor(true); //show console cursor
return 0;
}