-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
51 lines (40 loc) · 1.25 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
#include "common.h"
#include "io.h"
// Gameplay update loop for handling ticks/updates
void gameUpdateLoop(field& f, inputHandle& ih) {
f.tick();
print(f);
if (!ih.inputAvailable()) { return; }
switch (ih.getInput()) {
case CTRL_HARD_DROP: f.drop(); break;
case CTRL_SOFT_DROP: f.fall(); sleep(0.1); break;
case CTRL_MOV_L: f.mov(false); break;
case CTRL_MOV_R: f.mov(true); break;
case CTRL_ROT_L: f.rot(false); break;
case CTRL_ROT_R: f.rot(true); break;
case CTRL_HOLD: f.swap(); break;
default: break;
}
}
// Gameplay loop creating the field and using it until the game ends
void masterLoop(inputHandle& ih) {
// Create a new field to play on
field f;
while(f.isValid()) gameUpdateLoop(f, ih);
printGameOver(f.getScore()/10);
sleep(8);
}
int main() {
init_io();
initRand();
inputHandle ih;
// The game runs indefinitely
while(true) {
printMainScreen();
while (!ih.inputAvailable()) { sleep(0.2); }
// Consuming input is required for some systems
ih.getInput();
// Enter the master gameplay loop
masterLoop(ih);
}
}