forked from powerslam/KMU_SNAKEGAME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
142 lines (111 loc) · 4.84 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
#include <iostream>
#include <chrono>
#include "kmu_curses.h"
#include "SnakeGame.h"
using namespace std;
using namespace std::chrono;
auto getTime() {
return steady_clock::now();
}
auto milliInterval(const time_point<steady_clock>& start) {
return duration_cast<milliseconds>(getTime() - start).count();
}
int main() {
SnakeGame* game = new SnakeGame();
game->init();
char c;
auto updateTime = getTime(), gateTime = getTime(), itemTime = getTime();
auto startTime = getTime(), changeTime = getTime();
bool missionFlag = false, gateFlag = true;
while(getch() == ERR){
string title = "\toooooo oo oo oooo oo oo oooooo oooooooo oooo oo oo oooooo \n";
title += "\too oooo oo oo oo oo oo oo oo oo oo oooo oooo oo \n";
title += "\toooooo oo oo oo oooooooo ooooo oooooo oo oooo oooooooo oo oo oo oo oooooo \n";
title += "\t oo oo oooo oo oo oo oo oo oo oo oo oo oo oo oo oo \n";
title += "\toooooo oo ooo oo oo oo oo oooooo oooooooo oo oo oo oo oooooo \n";
game->draw(title);
}
game->createItems();
game->draw("", true);
while(game->isGaming()){
if(!game->wall.isUsed() && game->isMissionClear()){
if(game->currStage == 5){
game->setGameStatus(GAME_STATUS::WIN);
break;
}
if(!missionFlag) {
missionFlag = true;
changeTime = getTime();
game->changeNoticeMessage("");
game->setDirection(SNAKE_HEAD_DIRECTION::LEFT);
}
getch();
auto calcTime = milliInterval(changeTime);
if(calcTime < 3000LL){
char msg[50];
snprintf(msg, sizeof(msg), "change %d stage to %d stage after %lld sec...", game->currStage, game->currStage + 1, 3LL - (calcTime / 1000));
game->draw(msg);
} else {
missionFlag = false;
updateTime = gateTime = itemTime = getTime();
game->setNextGateShowFlag(false);
game->changeMap();
game->removeExpiredItems();
game->createItems();
game->changeNoticeMessage("start new stage!");
game->update(milliInterval(startTime) / 1000LL);
}
continue;
}
switch (getch()){
case 'w':
game->setDirection(SNAKE_HEAD_DIRECTION::UP);
break;
case 'a':
game->setDirection(SNAKE_HEAD_DIRECTION::LEFT);
break;
case 's':
game->setDirection(SNAKE_HEAD_DIRECTION::DOWN);
break;
case 'd':
game->setDirection(SNAKE_HEAD_DIRECTION::RIGHT);
break;
default: break;
}
if(milliInterval(gateTime) >= GATE_DURATION - NEXT_GATE_DURATION && !game->getNextGateShowFlag()){
game->setNextGateShowFlag(true);
}
if(!game->wall.isUsed() && milliInterval(gateTime) >= GATE_DURATION){
game->changeGate();
gateTime = getTime();
game->setNextGateShowFlag(false);
}
if(milliInterval(itemTime) >= ITEM_DURATION){
game->removeExpiredItems();
game->createItems();
itemTime = getTime();
}
if(milliInterval(updateTime) >= UPDATE_DURATION){
game->update(milliInterval(startTime) / 1000LL);
updateTime = getTime();
}
}
while(game->isLose() && getch() == ERR){
string title = "\too oo oooooooo oo oo oo oooooooo oooooo oooooo \n";
title += "\t oo oo oo oo oo oo oo oo oo oo oo \n";
title += "\t oooooo oo oo oo oo oo oo oo oooooo oooooo \n";
title += "\t oo oo 0o oo oo oo oo oo oo oo \n";
title += "\t oo oooooooo oooooooo oooooooo oooooooo oooooo oooooo \n";
game->draw(title, true);
}
while(game->isWin() && getch() == ERR){
string title = "\too oo oooooooo oo oo oo oo oo oooooo oo oo \n";
title += "\t oo oo oo oo oo oo oo oooo oo oo oooo oo \n";
title += "\t oooooo oo oo oo oo oo oo oo oo oo oo oo oo \n";
title += "\t oo oo 0o oo oo oo oo oo oo oo oo oooo \n";
title += "\t oo oooooooo oooooooo oooo oooo oooooo oo ooo \n";
game->draw(title, true);
}
endwin();
return 0;
}