-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
117 lines (100 loc) · 3.1 KB
/
Map.cpp
File metadata and controls
117 lines (100 loc) · 3.1 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
#include <iostream>
#include <Windows.h>
#include "Random.h"
#include "Map.h"
#include<graphics.h>
extern int score;
using namespace std;
// 定义全局数组
int map[4][4];
int score = 0;
// 初始化地图的函数定义
void Map() {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
map[i][j] = 0;
int x = Random();
Sleep(5);
int y = Random();
map[x][y] = 2;
while (true) {
Sleep(5);
int a = Random();
Sleep(5);
int b = Random();
int a2 = a % 2 * 3; // 获得0或3的随机数
int b2 = b % 2 * 3;
if (map[a2][b2] == 0) {
map[a2][b2] = 2;
break;
}
}
}
void drawGrid()
{
int Position = 10;
setlinecolor(BROWN);
setlinestyle(PS_SOLID, 2); // 设置线型为实线,线宽为2像素
// 绘制横向网格线
for (int i = 0; i <= 4; i++) {
line(Position + i * 100, Position + 0, Position + i * 100, Position + 400);
}
// 绘制纵向网格线
for (int i = 0; i <= 4; i++) {
line(Position, Position + i * 100, Position + 400, Position + i * 100);
}
}
void drawTiles(int map[][4])
{
int Position = 10;
settextcolor(BLACK); // 设置文字颜色为黑色
settextstyle(40, 0, _T("微软雅黑", ANTIALIASED_QUALITY)); // 设置文字样式,具体参数可根据需要调整
int transform[4][4];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
transform[i][j] = map[j][i];
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) {
// 绘制方块
rectangle(Position + i * 100, Position + j * 100, Position + (i + 1) * 100, Position + (j + 1) * 100);
// 绘制数字,假设数字不为0
if (transform[i][j] != 0) {
TCHAR num[5];
_stprintf_s(num, _T("%d"), transform[i][j]);
// 计算数字在方块中央的位置
int textX = i * 100 + 50 - textwidth(num) / 2;
int textY = j * 100 + 50 - textheight(num) / 2;
outtextxy(Position + textX, Position + textY, num);
}
}
}
void output_map(int map[][4])
{
drawGrid();
drawTiles(map);
}
void DisplayScore()
{
settextcolor(BLACK);
settextstyle(30, 0, _T("微软雅黑"));
TCHAR s[] = _T("SCORE:");
outtextxy(20,425,s);
settextcolor(RED);
settextstyle(40, 0, _T("微软雅黑"));
TCHAR str_score[5];
_stprintf_s(str_score, _T("%d"), score);
outtextxy(120, 425, str_score);
settextcolor(BROWN);
settextstyle(20, 0, _T("宋体"));
TCHAR help[] = _T("请在黑色控制台内按WASD或方向键操作");
outtextxy(10, 500, help);
settextcolor(BROWN);
settextstyle(20, 0, _T("宋体"));
TCHAR skill[] = _T("按B可使用技能\"炸弹\"");
outtextxy(10, 520, skill);
settextcolor(BLACK);
settextstyle(20, 0, _T("宋体"));
TCHAR infor[] = _T("游戏帮助:\n1、按方向键以移动\n2、相同数字的两个格子, 相撞时数字会相加.\n3、每次滑动时, 空白处会随机刷新出一个数字的格子.\n4、当界面不可运动时(当界面全部被数字填满时), 游戏结束; 当界面中最大数字是2048时,游戏胜利.");
RECT r = { 450,300,890,590 };
drawtext(infor, &r, DT_WORDBREAK| DT_NOFULLWIDTHCHARBREAK);
}