背景
本人于粤嵌学习嵌入式技术的时候,做的一个小项目见到同班同学都做了2048游戏的实现,本人也想做,但上网搜发现没有一个良好的教程教导人从头开始学起,本人花了两天时间才搞懂,但当时要是网上有一篇好的教程或许很快就能搞好,基于此,本人决定写下这篇博客。
2048游戏规则
玩家可以在一个4x4的格子上,上下左右滑动,若当前4x4的界面中相邻格子上有相同的数字,则往滑动方向合并,若无,则往滑动方向位移填充空白区域,若出现2048则游戏成功,若4x4的格子内无空白格子且单独格子内无上下左右方位相同数字的邻格可以合并,则游戏失败。
核心思想
如上16个格子,我们要实现上下左右的滑动之前,我们就必须先构建一个画板,就是构建一个背景板,若各位用的是使用arm架构lcd显示屏大小为800x480的6818单片机,下面有一个在指定位置下显示指定大小图片的方法供大家参考。
int showbmp_pos_size(const char*fileName,int posX,int posY,int width,int height)
{
char bmpbuf[width*height*3];
int tempbuf[width*height];
int lcdfd = open("/dev/fb0",O_RDWR);
if(lcdfd == -1){
perror("open lcd error");
return -1;
}
int* lcd_p=mmap(NULL,480*800*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcdfd,0);
if(lcd_p== MAP_FAILED)
{
perror("lcd mmap error");
return -1;
}
int bmpfd=open(fileName,O_RDWR);
if(bmpfd==-1){
perror("指定位置显示指定大小图片bmpfd is error ");
return -1;
}
lseek(bmpfd,54,SEEK_SET);
read(bmpfd,bmpbuf,width*height*3);
for(int i=0,j=0;i<width*height;i++,j+=3){
tempbuf[i]=0x00<<24|bmpbuf[j+2]<<16|bmpbuf[j+1]<<8|bmpbuf[j];
}
for(int j=0; j<height; j++)
{
for(int i=0; i<width ;i++)
{
lcd_p[(j+posY)*800+i+posX] = tempbuf[(height-1-j)*width+i];
}
}
close(bmpfd);
close(lcdfd);
munmap(lcd_p,480*800*4);
}
接下来就是使用上诉的showbmp_pos_size()方法先加载背景图片,分数面板,再加载4x4的游戏格子。
下面思考一个问题:如何滑动屏幕使得4x4的格子移动,并且实现相等数字合并,不相等数字占据滑动方向的空白格子?
思考倒数 1… 2… 3…
不知道大家想到了什么方法,要是没有思路,不妨参照一下我所使用的方法。
第一步,首先先创建一个二维数组存储4x4游戏方格里面的值,在定义一个存储char型指针类型的一维数组存储 0,2,4,8,16,…,2048字体图片的地址,如下定义。
int Two_Zero_Four_Eight[4][4]={0};
char* Fontaddress[50]={"图片地址0","图片地址1","图片地址2","图片地址3",...};
定义好之后,再把图片和数组关联起来,填充到lcd显示屏的左侧(0,0)到(400,480)的区域,如下代码。
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][0]],0,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][1]],TZFE_NUMBER_SIZE_X,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][2]],TZFE_NUMBER_SIZE_X*2,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][3]],TZFE_NUMBER_SIZE_X*3,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][0]],0,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][1]],TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][2]],TZFE_NUMBER_SIZE_X*2,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][3]],TZFE_NUMBER_SIZE_X*3,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][0]],0,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][1]],TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][2]],TZFE_NUMBER_SIZE_X*2,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][3]],TZFE_NUMBER_SIZE_X*3,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][0]],0,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][1]],TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][2]],TZFE_NUMBER_SIZE_X*2,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y)
上面的宏定义参考如下
#define TZFE_NUMBER_SIZE_X 100
#define TZFE_NUMBER_SIZE_Y 120
当游戏区域填充好之后,我们接下来就需要去判断用户进行了什么操作,下方函数可以获取用户操作
enum{
TOUCH,
SLIDE_RIGHT,
SLIDE_LEFT,
SLIDE_TOP,
SLIDE_DOWN,
};
int ts_func_slide(int *x,int *y)
{
int tsfd=open("/dev/input/event0",O_RDWR);
if(tsfd==-1){
perror("slide open event0 error");
return -1;
}
while(1){
struct input_event info;
int x1=0,x2=0,y1=0,y2=0;
while(1)
{
read(tsfd,&info,sizeof(struct input_event));
if(info.type==EV_ABS&&info.code==ABS_X){
x1=info.value;
x1=(x1)*800*1.0/1024;
}
if(info.type==EV_ABS&&info.code==ABS_Y){
y1=info.value;
y1=(y1)*480*1.0/600;
}
if(x1!=0&&y1!=0){
break;
}
}
while(1)
{
read(tsfd,&info,sizeof(struct input_event));
if(info.type==EV_ABS&&info.code==ABS_X){
x2=info.value;
x2=(x2)*800*1.0/1024;
}
if(info.type==EV_ABS&&info.code==ABS_Y){
y2=info.value;
y2=(y2)*480*1.0/600;
}
if(info.type==EV_KEY&&info.code==BTN_TOUCH&&info.value==0)
{
break;
}
}
if(x2-x1>50&&abs(y1-y2)<30){
printf("右滑动\n");
return SLIDE_RIGHT;
}
else if(x1-x2>50&&abs(y1-y2)<30){
printf("左滑动\n");
return SLIDE_LEFT;
}
else if(abs(x1-x2)<30&&y1-y2>50){
printf("上滑动\n");
return SLIDE_TOP;
}
else if(abs(x2-x1)<30&&y2-y1>50){
printf("下滑动\n");
return SLIDE_DOWN;
}
else{
*x=x1;
*y=y1;
printf("(%d,%d)\n",x1,y1);
printf("点击\n");
return TOUCH;
}
}
}
接下来便是该游戏最核心的部分-游戏算法的理解
当发生左滑 右滑 上滑 下滑的时候,Two-Zero-Four-Eight这个数组该如何移动。 1.我们先以左滑事件为例,当发生左滑的时候,我们可以先从数组的(0,0)开始往下遍历,遍历到(3,3),然后以每一行为单位,逐行进行操作。 操作一:当遇到0的时候,遍历顺序方向上的前一个数便和后一个数进行交换,直至交换到行的边际坐标(0,3),(1,3)等等这些坐标就停止交换,然后在尾部(0,0),(1,0)等等这些坐标下的数置为0。 操作二:当遇到遍历顺序方向上的当前坐标下的数等于下一坐标的数的情况,就把下一坐标的数加上1,(注:这里为什么是不加二而是加一,因为当前我们的Two_Zero_Four_Eight数组下的存放的是0-11这些数字,只是显示出来的时候我们把0显示为0,1显示为2,2显示为4)然后再把当前坐标的值清零。 然后再把操作一,操作二重复一次。(注:因为会存在行中数字排列为2 2 2 0 这种情况)。 2.而当发生右滑事件的时候,我们就必须从(0,3)开始遍历,也是以每一行为单位进行操作,就是(0,3)(0,2) (0,1) (0,0)然后切换到下一行(1,3)…这样进行操作。 操作一:当遇到0的时候,遍历顺序方向上的前一个数便和后一个数进行交换,直至交换至行的边际坐标(0,0),(1,0)等等这些坐标就停止交换,然后在尾部(0,3) (1,3)等等这些坐标下的数置为0。 操作二:当遇到遍历顺序方向上的当前坐标下的数等于下一坐标的数的情况,就把下一坐标的数加一,然后再把当前坐标的值清零。 然后再把操作一,操作二重复一次。 3.当发生上滑事件的时候,我们就从(0,0)开始遍历,此时我们以每一列为单位进行操作,就是(0,0)(1,0)(2,0)(3,0)然后切换到下一列(0,1)…这样进行操作。 操作一:当遇到0的时候,遍历顺序方向上的前一个数便和后一个数进行交换,直至交换至列的边际坐标(0,0),(0,1)等等这些坐标就停止交换,然后在尾部(0,3) (1,3)等等这些坐标下的数置为0。 操作二:当遇到遍历顺序方向上的当前坐标下的数等于下一坐标的数的情况,就把下一坐标的数加一,然后再把当前坐标的值清零。 然后再把操作一,操作二重复一次。 4.当发生下滑事件的时候,我们就从(0,3)开始遍历,此时我们以每一列为单位进行操作,就是(3,0)(2,0)(1,0)(0,0)然后切换到下一列(0,3)…这样进行操作。 操作一:当遇到0的时候,遍历顺序方向上的前一个数便和后一个数进行交换,直至交换至列的边际坐标(3,0),(3,1)等等这些坐标就停止交换,然后在尾部(0,0) (0,1)等等这些坐标下的数置为0。 操作二:当遇到遍历顺序方向上的当前坐标下的数等于下一坐标的数的情况,就把下一坐标的数加一,然后再把当前坐标的值清零。 然后再把操作一,操作二重复一次。
代码如下:
if(action==SLIDE_TOP)
{
for (int i = 0; i <4; i++)
{
for (int j = 0; j < 4; j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=i;
while (1)
{
if(m==3){
Two_Zero_Four_Eight[3][j]=0;
break;
}
Two_Zero_Four_Eight[m][j]=Two_Zero_Four_Eight[m+1][j];
m++;
}
}
else
{
if(i!=0)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i-1][j])
{
Two_Zero_Four_Eight[i-1][j]=Two_Zero_Four_Eight[i-1][j]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for (int i = 0; i <4; i++)
{
for (int j = 0; j < 4; j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=i;
while (1)
{
if(m==3){
Two_Zero_Four_Eight[3][j]=0;
break;
}
Two_Zero_Four_Eight[m][j]=Two_Zero_Four_Eight[m+1][j];
m++;
}
}
else
{
if(i!=0)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i-1][j])
{
Two_Zero_Four_Eight[i-1][j]=Two_Zero_Four_Eight[i-1][j]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
}
if(action==SLIDE_DOWN)
{
for (int i = 3; i >=0; i--)
{
for (int j = 0; j < 4; j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=i;
while (1)
{
if(m==0)
{
Two_Zero_Four_Eight[0][j]=0;
break;
}
Two_Zero_Four_Eight[m][j]=Two_Zero_Four_Eight[m-1][j];
m--;
}
}
else
{
if(i!=3)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i+1][j])
{
Two_Zero_Four_Eight[i+1][j]=Two_Zero_Four_Eight[i+1][j]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for (int i = 3; i >=0; i--)
{
for (int j = 0; j < 4; j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=i;
while (1)
{
if(m==0)
{
Two_Zero_Four_Eight[0][j]=0;
break;
}
Two_Zero_Four_Eight[m][j]=Two_Zero_Four_Eight[m-1][j];
m--;
}
}
else
{
if(i!=3)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i+1][j])
{
Two_Zero_Four_Eight[i+1][j]=Two_Zero_Four_Eight[i+1][j]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
}
if(action==SLIDE_LEFT)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=j;
while (1)
{
if(m==3)
{
Two_Zero_Four_Eight[i][3]=0;
break;
}
Two_Zero_Four_Eight[i][m]=Two_Zero_Four_Eight[i][m+1];
m++;
}
}
else
{
if(j!=0)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i][j-1])
{
Two_Zero_Four_Eight[i][j-1]=Two_Zero_Four_Eight[i][j-1]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=j;
while (1)
{
if(m==3)
{
Two_Zero_Four_Eight[i][3]=0;
break;
}
Two_Zero_Four_Eight[i][m]=Two_Zero_Four_Eight[i][m+1];
m++;
}
}
else
{
if(j!=0)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i][j-1])
{
Two_Zero_Four_Eight[i][j-1]=Two_Zero_Four_Eight[i][j-1]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
}
if(action==SLIDE_RIGHT)
{
for (int i = 0; i <4; i++)
{
for (int j = 3; j >=0 ; j--)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=j;
while (1)
{
if(m==0){
Two_Zero_Four_Eight[i][0]=0;
break;
}
Two_Zero_Four_Eight[i][m]=Two_Zero_Four_Eight[i][m-1];
m--;
}
}
else
{
if(j!=3)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i][j+1])
{
Two_Zero_Four_Eight[i][j+1]=Two_Zero_Four_Eight[i][j+1]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for (int i = 0; i <4; i++)
{
for (int j = 3; j >=0 ; j--)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=j;
while (1)
{
if(m==0){
Two_Zero_Four_Eight[i][0]=0;
break;
}
Two_Zero_Four_Eight[i][m]=Two_Zero_Four_Eight[i][m-1];
m--;
}
}
else
{
if(j!=3)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i][j+1])
{
Two_Zero_Four_Eight[i][j+1]=Two_Zero_Four_Eight[i][j+1]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
}
看到这个,想必大家已经对游戏如何实现有了一定的想法,剩下的功能不过是记录当前获得分数,在Two_Zero_Four_Eight数组中值为0的位置上的数替换为1的方法,相信这些功能难不倒聪明的你。
PS:如果难到了聪明的你,请继续往下看。
如何在2048数组中随机0的位置上拿1去替换
for (int i = 0; i < 4; i++)
{
for(int j=0;j<4;j++){
if(Two_Zero_Four_Eight[i][j]==0)
{
while (1)
{
int number=rand()%4;
int number1=rand()%4;
if(Two_Zero_Four_Eight[number][number1]==0)
{
Two_Zero_Four_Eight[number][number1]=1;
break;
}
}
}
}
}
记录当前分数方法
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(Two_Zero_Four_Eight[i][j]>score)
{
score=Two_Zero_Four_Eight[i][j];
}
}
}
showbmp_pos_size(Font_Address[score],700,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
总体实现过程
int show_2048_game(int x,int y){
char *Font_Address[50]={TZFE_ZERO_ADDRESS,TZFE_TWO_ADDRESS,TZFE_FOUR_ADDRESS,TZFE_EIGHT_ADDRESS,TZFE_SIXTEEN_ADDRESS,TZFE_THIRTY_TWO_ADDRESS,TZFE_SIXTY_FOUR_ADDRESS,TZFE_ONE_HUNDRED_AND_TWENTY_EIGHT_ADDRESS,TZFE_TWO_HUNDRED_AND_FIFTY_SIX_ADDRESS,TZFE_FIVE_HUNDRED_AND_TWELVE_ADDRESS,TZFE_ONE_THOUSAND_AND_TWENTY_FOUR_ADDRESS,TZFE_TWO_THOUSAND_FOURTY_EIGHT_ADDRESS};
int Two_Zero_Four_Eight[4][4]={0};
int score=0;
int flag=2;
srand((unsigned int)time(NULL));
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
int random=rand()%2;
Two_Zero_Four_Eight[i][j]=random;
}
}
showbmp_pos_size(TZFE_INTERFACE_ADDRESS,0,0,BMP_MAX_WIDTH,BMP_MAX_HEIGHT);
showbmp_pos_size(TZFE_BACK_ADDRESS,400,180,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(TZFE_START_ADDRESS,200,180,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
while (1)
{
ts_func(&x,&y);
if(BMP_MENU_2048_EXIT(x,y))
{
printf("退出游戏\n");
break;
}
if(BMP_MENU_2048_START(x,y))
{
srand((unsigned int)time(NULL));
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
int random=rand()%2;
Two_Zero_Four_Eight[i][j]=random;
}
}
printf("进入游戏\n");
showbmp_pos_size(TZFE_INTERFACE_ADDRESS,0,0,BMP_MAX_WIDTH,BMP_MAX_HEIGHT);
showbmp_pos_size(TZFE_BACK_ADDRESS,700,360,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(TZFE_INTERFACE_SCORE_ADDRESS,600,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(TZFE_VISABLE_ZERO_ADDRESS,700,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
int debug=0;
while(1)
{
debug++;
printf("第%d次循环\n",debug);
if(BMP_MENU_2048_GAME_EXIT(x,y))
{
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
Two_Zero_Four_Eight[i][j]=0;
}
}
printf("退出游戏\n");
showbmp_pos_size(TZFE_INTERFACE_ADDRESS,0,0,BMP_MAX_WIDTH,BMP_MAX_HEIGHT);
showbmp_pos_size(TZFE_BACK_ADDRESS,400,180,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(TZFE_START_ADDRESS,200,180,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
break;
}
flag=2;
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][0]],0,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][1]],TZFE_NUMBER_SIZE_X,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][2]],TZFE_NUMBER_SIZE_X*2,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][3]],TZFE_NUMBER_SIZE_X*3,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][0]],0,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][1]],TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][2]],TZFE_NUMBER_SIZE_X*2,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][3]],TZFE_NUMBER_SIZE_X*3,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][0]],0,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][1]],TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][2]],TZFE_NUMBER_SIZE_X*2,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][3]],TZFE_NUMBER_SIZE_X*3,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][0]],0,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][1]],TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][2]],TZFE_NUMBER_SIZE_X*2,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][3]],TZFE_NUMBER_SIZE_X*3,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
int action=ts_func_slide(&x,&y);
printf("%d(%d,%d)\n",__LINE__,x,y);
if(action==SLIDE_TOP)
{
for (int i = 0; i <4; i++)
{
for (int j = 0; j < 4; j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=i;
while (1)
{
if(m==3){
Two_Zero_Four_Eight[3][j]=0;
break;
}
Two_Zero_Four_Eight[m][j]=Two_Zero_Four_Eight[m+1][j];
m++;
}
}
else
{
if(i!=0)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i-1][j])
{
Two_Zero_Four_Eight[i-1][j]=Two_Zero_Four_Eight[i-1][j]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for (int i = 0; i <4; i++)
{
for (int j = 0; j < 4; j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=i;
while (1)
{
if(m==3){
Two_Zero_Four_Eight[3][j]=0;
break;
}
Two_Zero_Four_Eight[m][j]=Two_Zero_Four_Eight[m+1][j];
m++;
}
}
else
{
if(i!=0)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i-1][j])
{
Two_Zero_Four_Eight[i-1][j]=Two_Zero_Four_Eight[i-1][j]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for (int i = 0; i < 4; i++)
{
for(int j=0;j<4;j++){
if(Two_Zero_Four_Eight[i][j]==0)
{
while (1)
{
int number=rand()%4;
int number1=rand()%4;
if(Two_Zero_Four_Eight[number][number1]==0)
{
Two_Zero_Four_Eight[number][number1]=1;
break;
}
}
}
}
}
}
if(action==SLIDE_DOWN)
{
for (int i = 3; i >=0; i--)
{
for (int j = 0; j < 4; j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=i;
while (1)
{
if(m==0)
{
Two_Zero_Four_Eight[0][j]=0;
break;
}
Two_Zero_Four_Eight[m][j]=Two_Zero_Four_Eight[m-1][j];
m--;
}
}
else
{
if(i!=3)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i+1][j])
{
Two_Zero_Four_Eight[i+1][j]=Two_Zero_Four_Eight[i+1][j]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for (int i = 3; i >=0; i--)
{
for (int j = 0; j < 4; j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=i;
while (1)
{
if(m==0)
{
Two_Zero_Four_Eight[0][j]=0;
break;
}
Two_Zero_Four_Eight[m][j]=Two_Zero_Four_Eight[m-1][j];
m--;
}
}
else
{
if(i!=3)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i+1][j])
{
Two_Zero_Four_Eight[i+1][j]=Two_Zero_Four_Eight[i+1][j]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for (int i = 0; i < 4; i++)
{
for(int j=0;j<4;j++){
if(Two_Zero_Four_Eight[i][j]==0)
{
while (1)
{
int number=rand()%4;
int number1=rand()%4;
if(Two_Zero_Four_Eight[number][number1]==0)
{
Two_Zero_Four_Eight[number][number1]=1;
break;
}
}
}
}
}
}
if(action==SLIDE_LEFT)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=j;
while (1)
{
if(m==3)
{
Two_Zero_Four_Eight[i][3]=0;
break;
}
Two_Zero_Four_Eight[i][m]=Two_Zero_Four_Eight[i][m+1];
m++;
}
}
else
{
if(j!=0)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i][j-1])
{
Two_Zero_Four_Eight[i][j-1]=Two_Zero_Four_Eight[i][j-1]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=j;
while (1)
{
if(m==3)
{
Two_Zero_Four_Eight[i][3]=0;
break;
}
Two_Zero_Four_Eight[i][m]=Two_Zero_Four_Eight[i][m+1];
m++;
}
}
else
{
if(j!=0)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i][j-1])
{
Two_Zero_Four_Eight[i][j-1]=Two_Zero_Four_Eight[i][j-1]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for (int i = 0; i < 4; i++)
{
for(int j=0;j<4;j++){
if(Two_Zero_Four_Eight[i][j]==0)
{
while (1)
{
int number=rand()%4;
int number1=rand()%4;
if(Two_Zero_Four_Eight[number][number1]==0)
{
Two_Zero_Four_Eight[number][number1]=1;
break;
}
}
}
}
}
}
if(action==SLIDE_RIGHT)
{
for (int i = 0; i <4; i++)
{
for (int j = 3; j >=0 ; j--)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=j;
while (1)
{
if(m==0){
Two_Zero_Four_Eight[i][0]=0;
break;
}
Two_Zero_Four_Eight[i][m]=Two_Zero_Four_Eight[i][m-1];
m--;
}
}
else
{
if(j!=3)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i][j+1])
{
Two_Zero_Four_Eight[i][j+1]=Two_Zero_Four_Eight[i][j+1]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for (int i = 0; i <4; i++)
{
for (int j = 3; j >=0 ; j--)
{
if(Two_Zero_Four_Eight[i][j]==0)
{
int m=j;
while (1)
{
if(m==0){
Two_Zero_Four_Eight[i][0]=0;
break;
}
Two_Zero_Four_Eight[i][m]=Two_Zero_Four_Eight[i][m-1];
m--;
}
}
else
{
if(j!=3)
{
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i][j+1])
{
Two_Zero_Four_Eight[i][j+1]=Two_Zero_Four_Eight[i][j+1]+1;
Two_Zero_Four_Eight[i][j]=0;
}
}
}
}
}
for (int i = 0; i < 4; i++)
{
for(int j=0;j<4;j++){
if(Two_Zero_Four_Eight[i][j]==0)
{
while (1)
{
int number=rand()%4;
int number1=rand()%4;
if(Two_Zero_Four_Eight[number][number1]==0)
{
Two_Zero_Four_Eight[number][number1]=1;
break;
}
}
}
}
}
}
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][0]],0,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][1]],TZFE_NUMBER_SIZE_X,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][2]],TZFE_NUMBER_SIZE_X*2,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[0][3]],TZFE_NUMBER_SIZE_X*3,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][0]],0,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][1]],TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][2]],TZFE_NUMBER_SIZE_X*2,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[1][3]],TZFE_NUMBER_SIZE_X*3,TZFE_NUMBER_SIZE_Y,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][0]],0,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][1]],TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][2]],TZFE_NUMBER_SIZE_X*2,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[2][3]],TZFE_NUMBER_SIZE_X*3,TZFE_NUMBER_SIZE_Y*2,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][0]],0,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][1]],TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][2]],TZFE_NUMBER_SIZE_X*2,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
showbmp_pos_size(Font_Address[Two_Zero_Four_Eight[3][3]],TZFE_NUMBER_SIZE_X*3,TZFE_NUMBER_SIZE_Y*3,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(Two_Zero_Four_Eight[i][j]>score)
{
score=Two_Zero_Four_Eight[i][j];
}
}
}
showbmp_pos_size(Font_Address[score],700,0,TZFE_NUMBER_SIZE_X,TZFE_NUMBER_SIZE_Y);
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if( Two_Zero_Four_Eight[i][j]==11){
flag=1;
}
}
}
if (flag==1)
{
printf("游戏胜利\n");
sleep(1);
showbmp_pos_size(TZFE_GAME_SUCCESSFUL_ADDRESS,0,0,BMP_MAX_WIDTH,BMP_MAX_HEIGHT);
break;
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++){
if( Two_Zero_Four_Eight[i][j]==0){
flag=0;
}
}
}
if (flag==2)
{
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
if(j<3){
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i][j+1])
{
flag=0;
}
}
if(j>0){
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i][j-1])
{
flag=0;
}
}
if(i<3){
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i+1][j])
{
flag=0;
}
}
if(i>0){
if(Two_Zero_Four_Eight[i][j]==Two_Zero_Four_Eight[i-1][j])
{
flag=0;
}
}
}
}
if(flag==2){
printf("游戏失败!\n");
sleep(1);
showbmp_pos_size(TZFE_GAME_FAIL_ADDRESS,0,0,BMP_MAX_WIDTH,BMP_MAX_HEIGHT);
break;
}
}
}
printf("%d来到了外层循环\n",__LINE__);
sleep(5);
break;
}
}
}
附上的我所导入的库
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<linux/input.h>
#include<string.h>
#include<strings.h>
#include<linux/input.h>
#include<time.h>
实现效果
视频传不了,效果只能看图片了,老铁们
|