-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDraw.cpp
More file actions
77 lines (70 loc) · 2.52 KB
/
Draw.cpp
File metadata and controls
77 lines (70 loc) · 2.52 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
#include "Draw.h"
#include <iostream>
#include <iomanip>
#ifdef _WIN32
#include <afxres.h>
#endif
void Draw::ClearScreen() {
#ifdef _WIN32
/* 重设光标输出位置清屏可以减少闪烁,system("cls")为备用清屏命令,均为Windows平台相关*/
COORD pos = {0, 0};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
CONSOLE_CURSOR_INFO info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
#else
printf("\033c"); /* linux下的清屏命令 */
printf("\033[?25l"); /* linux下的隐藏输入光标 */
#endif
}
void Draw::PrintNumAlignCenter(int num, int w, bool Zero) {
if (num == 0 && !Zero) {
for (int i = 0; i < w; i++)
std::cout << ' ';
return;
}
int len = 0, tnum = num;
while (tnum) {
tnum /= 10;
len++;
}
if (num == 0)
len = 1;
if (w < len) {
std::cout << "ERROR" << std::endl;
return;
}
w -= len;
for (int i = 0; i < w - w / 2; i++)
std::cout << ' ';
std::cout << num;
for (int i = 0; i < w / 2; i++)
std::cout << ' ';
}
void Draw::PrintUi(GameData Data, bool flagQuit, bool flagGameOver) {
std::cout << " HAPPY! SCORE: ";
PrintNumAlignCenter(Data.GetScore(), 6, true);
std::cout << " BEST: ";
PrintNumAlignCenter(Data.GetBestScore(), 6, true);
std::cout << " !HAPPY" << std::endl;
std::cout << " ----------------------------------------------------" << std::endl;
std::cout << std::endl << std::endl;
std::cout << " |------|------|------|------|" << std::endl;
for (int i = 0; i < 4; i++) {
std::cout << " |";
for (int j = 0; j < 4; j++) {
PrintNumAlignCenter(Data.GetPositionNumber(i, j), 6, false);
std::cout << ("|");
}
std::cout << std::endl;
std::cout << " |------|------|------|------|" << std::endl;
}
std::cout << std::endl << std::endl;
std::cout << " ----------------------------------------------------" << std::endl;
if (flagGameOver)
std::cout << " Do you want to play the game again?([Y]/[N]) " << std::endl;
else if (flagQuit)
std::cout << " Do you really want to quite the game?([Y]/[N]) " << std::endl;
else
std::cout << " [W]:UP [S]:DOWN [A]:LEFT [D]:RIGHT [Q]:EXIT" << std::endl;
}
Draw::Draw() = default;