c语言2048小游戏

  1. 1. cpp环境搭建
  2. 2. vsc配置需要
  3. 3. 源码

cpp环境搭建

正常安装c环境后 增加easyx库

vsc配置需要

Visual Studio 2019 Developer Command Prompt v16.6.3

源码

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//2048游戏
//FOR YOU
//作者白筱
#include<stdio.h>
#include<conio.h>
#include<graphics.h>//文件库
#define MAX_SIZE 4 //格子数量
#define GRID_WIDTH 100 //格子宽度
#define INTERVAL 15 //格子间隔
#define WIN_SIZE MAX_SIZE*GRID_WIDTH+5*INTERVAL
enum Color//枚举颜色 2048 2的11次方
{
zero = RGB(202,241,248),
twoto1 = RGB(145,224,239),
twoto2 = RGB(0,180,215),
twoto3 = RGB(0,120,183),
twoto4 = RGB(3,4,96),
twoto5 = RGB(101,125,129),
twoto6 = RGB(160,201,195),
twoto7 = RGB(226,180,183),
twoto8 = RGB(226,180,183),
twoto9 = RGB(189,99,125),
twoto10 = RGB(205,198,229),
twoto11 = RGB(119,118,188),
bk = RGB(3,76,95),
};
Color arr[13] = {zero,twoto1,twoto2,twoto3,twoto4,twoto5,twoto6,twoto7,twoto8,twoto9,twoto10,twoto11,bk};
int num[12] = { 0,2,4,8,16,32,64,128,256,512,1024,2048 };//颜色数据建立联系
int map[MAX_SIZE][MAX_SIZE];//数据存储
POINT pos[MAX_SIZE][MAX_SIZE];
char usrkey=0;
int RandTwoFour()//随即生成一个数字2or4
{
if(rand()%10==0)
{
return 4;
}
else
{
return 2;
}
}
void CreateNumber()
{
while(1)
{
int x = rand() % MAX_SIZE;
int y = rand() % MAX_SIZE;
if(map[x][y] == 0)
{
map[x][y]=RandTwoFour();
break;
}
}
}
void GameInit()//初始化坐标
{
srand(GetTickCount());
for(int i=0;i<MAX_SIZE;i++)
{
for(int k=0;k<MAX_SIZE;k++)
{
map[i][k] = 0;

}
}



for(int i=0;i< MAX_SIZE ;i++)
{
for(int k=0;k< MAX_SIZE ;k++)
{
pos[i][k].x=(k*GRID_WIDTH)+((k+1)*INTERVAL);
pos[i][k].y=(i*GRID_WIDTH)+((i+1)*INTERVAL);
}
}
//产生两个数据
CreateNumber();
CreateNumber();
}
void GameDrw()
{
setbkcolor(Color::bk);
cleardevice();
for(int i=0;i< MAX_SIZE ;i++)
{
for(int k=0;k< MAX_SIZE ;k++)
{
for(int q=0;q<12;q++)
{
if(map[i][k] == num[q])//给予方块对应颜色
{
DWORD tempcolor = arr[q];
setfillcolor(tempcolor);
solidrectangle(pos[i][k].x, pos[i][k].y, pos[i][k].x + GRID_WIDTH , pos[i][k].y + GRID_WIDTH);
if(num[q]!=0)
{
char number[5]="";
sprintf(number,"%d",num[q]);
setbkmode(TRANSPARENT);//透明
settextstyle(50,0,"黑体");//字体属性
settextcolor(RGB(0,0,0));//颜色
int temp =textwidth(number);
temp = (GRID_WIDTH-temp)/2;
outtextxy(pos[i][k].x+temp,pos[i][k].y+25,number);
}
}
}
}
}
}
void moveup()//向上
{
for(int i=0;i<MAX_SIZE;i++)
{
int temp = 0;
for(int begin=1;begin<MAX_SIZE;begin++)
{
if(map[begin][i]!=0)
{
if(map[temp][i]==0)
{
map[temp][i]= map[begin][i];
map[begin][i]=0;
}
else
if(map[temp][i]==map[begin][i])
{
map[temp][i]+=map[begin][i];
map[begin][i]=0;
temp++;
}
else
{
map[temp + 1][i]=map[begin][i];
if(temp+1!=begin)
{
map[begin][i]=0;
}
temp++;
}

}
}
}
}
void movedown()//下移
{
for (int i = 0; i < MAX_SIZE; i++)
{
int temp = MAX_SIZE-1;
for (int begin = MAX_SIZE-2; begin>=0; begin--)
{
if (map[begin][i] != 0)
{
if (map[temp][i] == 0)
{
map[temp][i] = map[begin][i];
map[begin][i] = 0;
}
else if (map[temp][i] == map[begin][i])
{
map[temp][i] += map[begin][i];
map[begin][i] = 0;
}
else
{
map[temp - 1][i] = map[begin][i];
if (temp - 1 != begin)
{
map[begin][i] = 0;
}
}
temp--;

}
}
}

}
void moveleft()//左移
{
for (int i = 0; i < MAX_SIZE; i++)
{
int temp = 0;
for (int begin = 1; begin < MAX_SIZE; begin++)
{
if (map[i][begin] != 0)
{
if (map[i][temp] == 0)
{
map[i][temp] = map[i][begin];
map[i][begin] = 0;
}
else if (map[i][temp] == map[i][begin])
{
map[i][temp] += map[i][begin];
map[i][begin] = 0;
}
else
{
map[i][temp+1] = map[i][begin];
if (temp + 1 != begin)
{
map[i][begin] = 0;
}
}
temp++;

}
}
}

}
void moveright()//右移
{
for (int i = 0; i < MAX_SIZE; i++)
{
int temp = MAX_SIZE-1;
for (int begin = MAX_SIZE-2; begin>=0; begin--)
{
if (map[i][begin] != 0)
{
if (map[i][temp] == 0)
{
map[i][temp] = map[i][begin];
map[i][begin] = 0;
}
else if (map[i][temp] == map[i][begin])
{
map[i][temp] += map[i][begin];
map[i][begin] = 0;
}
else
{
map[i][temp-1] = map[i][begin];
if (temp - 1 != begin)
{
map[i][begin] = 0;
}
}
temp--;

}
}
}
}
void keyControl()
{
usrkey = _getch();
switch (usrkey)
{
case 75:
case 'A':
case 'a':
moveleft();CreateNumber();
break;
case 77:
case 'd':
case 'D':
moveright();CreateNumber();
break;
case 80:
case 's':
case 'S':
movedown();CreateNumber();
break;
case 72:
case 'w':
case 'W':
moveup();CreateNumber();
break;
default:
break;
}
}
int main()
{
initgraph(WIN_SIZE,WIN_SIZE,SHOWCONSOLE);//创建一个窗口
GameInit();//初始化坐标

while (1)
{
GameDrw();
keyControl();
}

return 0;
}