-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player
69 lines (65 loc) · 1.27 KB
/
Player
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
class Player {
public:
Player() {}
CMD GetCmd(); // 获取控制命令
void DispatchCmd(CMD _cmd); // 分发控制命令
};
//以下为实现
// 获取控制命令
DWORD m_oldtime;
CMD Player::GetCmd()
{
// 获取控制值
while (true)
{
// 如果超时,自动下落一格
DWORD newtime = GetTickCount();
if (newtime - m_oldtime >= 500)
{
m_oldtime = newtime;
return CMD_DOWN;
}
// 如果有按键,返回按键对应的功能
if (_kbhit())
{
switch (_getch())
{
case 'w':
case 'W': return CMD_ROTATE;
case 'a':
case 'A': return CMD_LEFT;
case 'd':
case 'D': return CMD_RIGHT;
case 's':
case 'S': return CMD_DOWN;
case 27: return CMD_QUIT;
case ' ': return CMD_SINK;
case 0:
case 0xE0:
switch (_getch())
{
case 72: return CMD_ROTATE;
case 75: return CMD_LEFT;
case 77: return CMD_RIGHT;
case 80: return CMD_DOWN;
}
}
}
// 延时 (降低 CPU 占用率)
Sleep(20);
}
}
// 分发控制命令
void Player::DispatchCmd(CMD _cmd)
{
Square B;
switch (_cmd)
{
case CMD_ROTATE: B.OnRotate(); break;
case CMD_LEFT: B.OnLeft(); break;
case CMD_RIGHT: B.OnRight(); break;
case CMD_DOWN: B.OnDown(); break;
case CMD_SINK: B.OnSink(); break;
case CMD_QUIT: break;
}
}