-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredLight.cpp
More file actions
143 lines (126 loc) · 4.28 KB
/
Copy pathredLight.cpp
File metadata and controls
143 lines (126 loc) · 4.28 KB
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
/* Version: 06.1746 */
#include "redLight.h"
using namespace std;
// set terminal to non blocking mode
void RedLightGame::setNonBlockingInput(bool enable)
{
struct termios tty;
tcgetattr(STDIN_FILENO, &tty);
if (enable)
{
tty.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK); // 設置非阻塞模式
}
else
{
tty.c_lflag |= (ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) & ~O_NONBLOCK); // 關閉非阻塞模式
}
}
void RedLightGame::startGame(Player& player)
{
bool isSucceed = false;
// initialize game
int playerPos = PLAYER_START_POS;
bool isRedLight = false;
float redLightSwitchTime = 0; // record light switching time
int timeLeft = TIME_LIMIT;
std::srand(static_cast<unsigned>(std::time(0)));
auto startTime = std::chrono::steady_clock::now();
auto redLightTime = startTime;
setNonBlockingInput(true); // enable terminal non blocking mode
while (timeLeft > 0)
{
// calculate time left
auto now = std::chrono::steady_clock::now();
timeLeft = TIME_LIMIT - static_cast<int>(
std::chrono::duration_cast<std::chrono::seconds>(now - startTime).count());
// switch light randomly
if (std::rand() % 100 < 5)
{ // 5% 機率每迴圈切換燈號
isRedLight = !isRedLight;
redLightTime = now;
}
// 清除螢幕
system("clear");
// 顯示遊戲狀態
std::cout << "Time Left: " << timeLeft << "s\n";
std::cout << (isRedLight ? "RED LIGHT" : "GREEN LIGHT") << "\n";
// 顯示遊戲場景
for (int i = 0; i < WINDOW_WIDTH; ++i)
{
if (i == playerPos)
{
std::cout << "P"; // 玩家
}
else if (i == FINISH_LINE_X)
{
std::cout << "|"; // 終點線
}
else
{
std::cout << "-";
}
}
std::cout << "\n";
// 檢查玩家是否到達終點
if (playerPos >= FINISH_LINE_X)
{
std::cout << "Phew! You made it!\n";
isSucceed = true;
break;
}
// 處理玩家輸入
char input;
if (read(STDIN_FILENO, &input, 1) > 0)
{ // 非阻塞讀取輸入
if (input == 'a' || input == 'A')
{
playerPos = std::max(playerPos - 1, 0); // 向左移動
}
else if (input == 'd' || input == 'D')
{
// 檢查是否違反紅燈規則
float timeSinceRed = std::chrono::duration_cast<std::chrono::milliseconds>(now - redLightTime).count() / 1000.0;
if (isRedLight && timeSinceRed > BUFFER_TIME)
{
std::cout << "You moved during RED LIGHT! Game Over!\n";
break;
}
playerPos = std::min(playerPos + 1, FINISH_LINE_X); // 向右移動
}
}
// 暫停片刻模擬遊戲節奏
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (timeLeft <= 0)
{
std::cout << "Times up! you lost!";
}
if (isSucceed)
{
std::cout << "You've gained " << gainedEXP << " EXP!" << '\n';
std::cout << "++ Coin: " << gainedCoin << endl;
player.setExp(player.getExp() + gainedEXP);
player.addCoin(gainedCoin);
}
else
{
std::cout << "You've lost " << lostHP << " pts of HP!" << '\n';
player.sethp(player.gethp() - lostHP);
}
std::cout << "Press \"Enter\" to continue\r" << endl;
std::cout.flush();
setNonBlockingInput(false); // 恢復終端默認模式
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
for (int sec = 1; sec > 0; sec--)
{
std::cout << "Returning to maze in " << sec << " seconds...\r";
std::cout.flush();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << " \r";
std::cout.flush();
}
}