C++编写2048小游戏
工具Virtual Studio 2019
思路:
1.画出一个界面(因为不会mfc)
2.用图片来显示数字,用键盘WASD表示方向
实现:
0.头文件
#include<bits/stdc++.h>
#include<graphics.h>
#include<conio.h>
1.main函数:
这里画出的界面大小是通过数字图片素材的大小计算的,例如,我这里图片的大小是125*125,而2048游戏需要一个 4 * 4 的界面来操作,同时我又加了一行来显示我的标注。
之后进入循环,读入键盘操作,持续画图。
int main()
{
loadResource();
initgraph(126 * 4, 126 * 4 + 125);
drawMap();
while (1)
{
keyDown();
drawMap();
}
getchar();//防止闪屏
closegraph();
return 0;
}
2.图片处理
需要图片库插件
下载后运行安装,安装到需要的路径即可
图片素材:(不懂怎么批量上传)
加载这12张图片,用数组给图片做标记。
再用一个二维数组来实现4*4的布局
IMAGE img[12];
int imgIndex[12] = { 0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 };
int window[4][4] = { 0 };
void loadResource() //批量加载
{
for (int i = 0; i < 12; i++)
{
char fileName[20] = "";
sprintf_s(fileName, "%d.png", imgIndex[i]);//根据格式调整文件名后缀
loadimage(img + i, fileName);
}
}
void drawMap() //界面布局
{
setbkcolor(RGB(30, 41, 61));
cleardevice();
settextcolor(WHITE);
settextstyle(35, 0, "黑体");
outtextxy(50, 43, " 2048 -- by Scream");
int x, y, k ;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
x = 125 * j;
y = 125 * (i + 1);
for (k = 0; k < 12; k++)
{
if (imgIndex[k] == window[i][j])
{
break;
}
}
putimage(x, y, img + k);
}
}
}
3.键盘操作响应
void keyDown() //按键响应
{
char key=_getch();
switch (key)
{
case 'w':
case 'W':
randNumber();
moveUp();
break;
case 's':
case 'S':
randNumber();
moveDown();
break;
case 'a':
case 'A':
randNumber();
moveLeft();
break;
case 'd':
case 'D':
randNumber();
moveRight();
break;
}
}
4.随机位置生成图片
int randNumber()
{
srand((unsigned int)time(NULL));
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (window[i][j] == 0)
{
window[i][j] = (rand() % 3) * 2;
if (window[i][j] == 0)
{
continue;
}
return 0;
}
}
}
return 0;
}
5.上下左右各方向操作
逻辑都一样,建议先写一个,剩下的对照着改
int moveRight()
{
int flag = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 3; j >= 0; j--)
{
int nowKey = window[i][j];//当前元素
if (nowKey != 0)
{
int k = j - 1;
while (k >= 0)
{
int nowKeyNext = window[i][k];//当前元素的下一个
if (nowKeyNext != 0)
{
//如果想邻相同则合并
if (nowKey == nowKeyNext)
{
flag = 1;
window[i][j] += window[i][k];
window[i][k] = 0;
}
k = -1;
}
k--;//判断其他位置
}
}
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 3; j >= 0; j--)
{
int nowKey = window[i][j];
if (nowKey == 0)
{
int k = j - 1;
while (k >= 0)
{
int nowKeyNext = window[i][k];
if (nowKeyNext != 0)
{
flag = 1;
window[i][j] = nowKeyNext;
window[i][k] = 0;
k = -1;
}
k--;
}
}
}
}
if (flag)
return 0;
else
return 1;
}
int moveLeft()
{
int flag = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
int nowKey = window[i][j];//当前元素
if (nowKey != 0)
{
int k = j + 1;
while (k <4 )
{
int nowKeyNext = window[i][k];//当前元素的下一个
if (nowKeyNext != 0)
{
//如果想邻相同则合并
if (nowKey == nowKeyNext)
{
flag = 1;
window[i][j] += window[i][k];
window[i][k] = 0;
}
k = 4;
break;
}
k++;//判断其他位置
}
}
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j <4 ; j++)
{
int nowKey = window[i][j];
if (nowKey == 0)
{
int k = j + 1;
while (k <4 )
{
int nowKeyNext = window[i][k];
if (nowKeyNext != 0)
{
flag = 1;
window[i][j] = nowKeyNext;
window[i][k] = 0;
k = 4;
}
k++;
}
}
}
}
if (flag)
return 0;
else
return 1;
}
int moveUp()
{
int flag = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
int nowKey = window[i][j];//当前元素
if (nowKey != 0)
{
int k = i + 1;
while (k < 4)
{
int nowKeyNext = window[k][j];//当前元素的下一个
if (nowKeyNext != 0)
{
//如果想邻相同则合并
if (nowKey == nowKeyNext)
{
flag = 1;
window[i][j] += window[k][j];
window[k][j] = 0;
}
k = 4;
}
k++;//判断其他位置
}
}
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
int nowKey = window[i][j];
if (nowKey == 0)
{
int k = i + 1;
while (k < 4 )
{
int nowKeyNext = window[k][j];
if (nowKeyNext != 0)
{
flag = 1;
window[i][j] = nowKeyNext;
window[k][j] = 0;
k = 4;
}
k++;
}
}
}
}
if (flag)
return 0;
else
return 1;
}
int moveDown()
{
int flag = 0;
for (int i = 3; i >=0; i--)
{
for (int j = 0; j <4 ; j++)
{
int nowKey = window[i][j];//当前元素
if (nowKey != 0)
{
int k = i - 1;
while (k >= 0)
{
int nowKeyNext = window[k][j];//当前元素的下一个
if (nowKeyNext != 0)
{
//如果相邻相同则合并
if (nowKey == nowKeyNext)
{
flag = 1;
window[i][j] += window[k][j];
window[k][j] = 0;
}
k = -1;
}
k--;//判断其他位置
}
}
}
}
for (int i = 3; i >= 0; i--)
{
for (int j = 0; j < 4; j++)
{
int nowKey = window[i][j];
if (nowKey == 0)
{
int k = i - 1;
while (k >= 0)
{
int nowKeyNext = window[k][j];
if (nowKeyNext != 0)
{
flag = 1;
window[i][j] = nowKeyNext;
window[k][j] = 0;
k = -1;
}
k--;
}
}
}
}
if (flag)
return 0;
else
return 1;
}
其它问题:
1.VS中调试没问题,但是它生成的exe文件不能正常运行
图片和exe的路径不一致,可以把图片们放到exe所在的文件夹里
2.exe文件不能在没有VS的环境中运行
缺少.dll文件,可以把调试方法由Debug改成Release
之后,在项目设置中设置:
配置属性—>C/C++—>代码生成—>运行库—>多线程Dll(/MD);
配置属性—>高级—>高级属性—>字符集—>未设置;