#include "project.h"
//测试
unsigned int *mem_p;
void game2(P_PDI p_pdi){
int i, j, ret, win, first, count1, count2, countkill;
int data[N][N] = {0}; //游戏数组
int olddata[N][N] = {0}; //备份数组
int array[10] = {0}; //猜测数组, 用来表示难度
initarray(array, array_len); //(2)初始化猜测数组
initGame(data, array, array_len); //(3)初始化游戏·界面
if(initbmp() == -1) //(4)打开图片
{
printf("initbmp fail\n");
uninitbmp();
return;
}
ret = checkstart(p_pdi);
if(ret == 1)
{
printf("获取坐标成功");
}
if(ret == -1)
{
sleep(3);
return;
}
clearscreen(p_pdi);
printf("游戏开始\n");
first = 1;
count1 = 0;
count2 = 0;
countkill = 0;
while(1)
{
show(data); //14显示游戏界面
if(ret != 0) //单纯点击时,不改变控制台的游戏界面
{
showGame(data,p_pdi);
}
win = check(data); //16.检查能否继续游戏
if(win == 0 || ret == 16) //游戏结束
{
printf("游戏结束\n");
showlose(p_pdi);
sleep(1);
break;
}
if(win == 2)
{
printf("恭喜你,胜利了\n");
showwin();
sleep(1);
break;
}
ret = paddle_ts(p_pdi); //获取触摸屏情况
switch (ret)
{
case 1: //左划
copyData(data, olddata);
mergeLeft(data, &first, &count1, &count2, &countkill);
produceXY(data, array, array_len);
break;
case 2: //右划
copyData(data, olddata);
mergeRight(data, &first, &count1, &count2, &countkill);
produceXY(data, array, array_len);
break;
case 3: //上划
copyData(data, olddata);
mergeUp(data, &first, &count1, &count2, &countkill);
produceXY(data, array, array_len);
break;
case 4: //下划
copyData(data, olddata);
mergeDown(data, &first, &count1, &count2, &countkill);
produceXY(data, array, array_len);
break;
case 13: //重新开始游戏
copyData(data, olddata);
for(i = 0; i<N; i++)
{
for(j = 0; j<N; j++)
{
data[i][j] = 0;
}
}
produceXY(data, array, array_len);
produceXY(data, array, array_len);
break;
case 14: //清空最大的数字
copyData(data, olddata); //备份数据来方便撤销动作
produceXY(data, array, array_len);
break;
}
system("clear"); //清空控制台屏幕
}
return;
}
//28.显示游戏结束
void showlose(P_PDI p_pdi)
{
int lose = open(bmp_lose, O_RDONLY);
if(lose == -1)
{
printf("open bmp_lose fail\n");
return ;
}
close(lose);
sleep(2);
int defeat = open(bmp_defeat, O_RDONLY);
if(defeat == -1)
{
printf("open bmp_feat fail\n");
return ;
}
close(defeat);
sleep(2);
int end = open(bmp_end, O_RDONLY);
if(end == -1)
{
printf("open bmp_end fail\n");
return ;
}
close(end);
}
//27.显示游戏胜利
void showwin()
{
int victory = open(bmp_victory, O_RDONLY);
if(victory == -1)
{
printf("open bmp_victory fail\n");
return;
}
close(victory);
sleep(2);
int win = open(bmp_win, O_RDONLY);
if(win == -1)
{
printf("open bmp_win fail\n");
return;
}
close(win);
}
//26. 0点击 1左划 2右划 3上划 4下划 12-16特效
int paddle_ts(P_PDI p_pdi)
{
int x = 0, y = 0, xx = 0, yy = 0;
struct input_event ts;
memset(&ts, 0, sizeof(struct input_event));
while (1)
{
//读触摸屏
read(p_pdi->ts,&(p_pdi->touch),sizeof(p_pdi->touch));
//决断是否为相对坐标类型
if(p_pdi->touch.type == EV_ABS && p_pdi->touch.code == ABS_X)
{
x = p_pdi->touch.value;;
}
//决断是否为相对坐标类型
if(p_pdi->touch.type == EV_ABS && p_pdi->touch.code == ABS_Y)
{
y = p_pdi->touch.value;
}
//表示手指按下
if(p_pdi->touch.type == EV_KEY && p_pdi->touch.code == BTN_TOUCH && p_pdi->touch.value == 1)
{
//记录起点坐标
xx = x;
yy = y;
}
//表示手指松开
if(p_pdi->touch.type == EV_KEY && p_pdi->touch.code == BTN_TOUCH && p_pdi->touch.value == 0)
{
//坐标换算 起点坐标
xx = xx;
yy = yy;
//坐标换算 结束坐标
x = x;
y = y;
//如果点击功能菜单区域
if(x>520 && x<760)
{
if(y>0 && y<80)
{
return 11;
}
if(y>80 && y<160)
{
return 12;
}
if(y>160 && y<240)
{
return 13;
}
if(y>240 && y<320)
{
return 14;
}
if(y>320 && y<400)
{
return 15;
}
if(y>400 && y< 480)
{
return 16;
}
}
//点击游戏界面
if(x>0 && x<480)
{
if(abs(xx-x) > abs(yy - y))
{
if((xx - x) > 100)
{
return 1; //左划
}
if((x - xx) >= 100)
{
return 2; //右划
}
return 0;
}
else
{
if((yy - y) > 50)
{
return 3; //上划
}
if((y - yy) >= 50)
{
return 4; //下划
}
return 0;
}
return 0; //点击
}
}
}
return 0;
}
//17.判断是否还有空位
int checkSpace(int data[N][N])
{
int i, j, n;
n = 0;
for(i = 0; i<N; i++)
{
for(j = 0; j<N; j++)
{
if(!data[i][j]) //还有空位
{
n++;
}
}
}
return n;
}
//18.选择一个空坐标,生成数字
void produceXY(int data[N][N], int *array, int n)
{
int i, j, flag, a[2];
flag = checkSpace(data);
if(!flag) //如果没有空位就不用产生数字
{
return;
}
while(1) //一定可以找到空位并产生数字
{
flag = 0;
Random(a, 2); //随机产生一个坐标, (a[0], a[1])
for(i = 0; i<N; i++)
{
for(j = 0; j<N;