头文件:
#ifndef _HEAD_H__
#define _HEAD_H__
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/mman.h>
#include<linux/input.h>
#endif
在学习了文件io后自制的触摸屏小游戏,技术不足,不喜勿喷
主代码
int a;//全局变量,记录步数,达到步数即为失败,显示失败图片
int main()
{
Init_Game:
//1.打开屏幕
Lcd_Init();
//显示游戏初始界面
Lcd_DisplayBmp(0,0,"huarongland.bmp");
int i;
for(i = 0;i < 4;i++)
{
Lcd_DrawWord(280 + 70 * i,400,64,64,WordSch[i],0xFFFF00);
}
//判断是否按下开始按键
while(1)
{
Point p=Get_Touch();
if(p.x>=0 && p.x<=800 && p.y>400 && p.y<=600)
{
break;
}
}
#if 1
/*背景初始化*/
background();
lineation();
/*第一关初始化*/
Init_first();
for(i = 0;i < 3;i++)
{
Lcd_DrawWord(590+ 70*i,110,96,97,WordSch1[i],0xFFFF00);
}
int arr[3][3]={1,2,3,4,8,5,7,0,6};//初始化第一关数组
//开始游戏
int step=6;//第一关规定步数
Play_game(step,arr,&a);
if(a==0)//输了返回开始游戏
{
sleep(3);
goto Init_Game;
}
#if 0//测试滑动
int t=get_slip_way();
if(t==0)
printf("up");
else if(t==1)
printf("down");
else if(t==2)
printf("left");
else if(t==3)
printf("right");
#endif
//延迟测试
#if 0
sleep(5);
if(IS_Win(arr)==1)
{
Lcd_DisplayBmp(150,80,"win.bmp");
for(i = 0;i < 5;i++)
{
Lcd_DrawWord(280 + 70 * i,400,32,64,WordSch0[i],0xFFFF00);
}
}
else
{
Lcd_DisplayBmp(180,0,"lose.bmp");
for(i = 0;i < 5;i++)
{
Lcd_DrawWord(280 + 70 * i,400,32,64,WordSch0[i],0xFFFF00);
}
}
#endif
//继续游戏
while(1)
{
Point p=Get_Touch();
if(p.x>=0 && p.x<=800 && p.y>390 && p.y<=600)
{
break;
}
}
/*背景初始化*/
background();
lineation();
/*第二关初始化*/
Init_second();
for(i = 0;i < 3;i++)
{
Lcd_DrawWord(590+ 70*i,110,96,97,WordSch3[i],0x00FF00);
}
//第二关数组初始化
int arr1[3][3]={1,3,6,4,2,0,7,5,8};
//第二关步数为10
step=10;
//开始第二关
Play_game(step,arr1,&a);
if(a==0)//输了返回开始游戏
{
sleep(1);
goto Init_Game;
}
//继续游戏
while(1)
{
Point p=Get_Touch();
if(p.x>=0 && p.x<=800 && p.y>390 && p.y<=600)
{
break;
}
}
/*背景初始化*/
background();
lineation();
/*第三关初始化*/
Init_third();
for(i = 0;i < 3;i++)
{
Lcd_DrawWord(590+ 70*i,110,96,97,WordSch4[i],0x250250);
}
//第三关数组初始化
int arr2[3][3]={2,3,5,1,4,6,0,7,8};
//第三关可走15步
step=15;
//开始第三关
Play_game(step,arr2,&a);
if(a==0)//输了返回开始游戏
{
sleep(3);
goto Init_Game;
}
//继续游戏
while(1)
{
Point p=Get_Touch();
if(p.x>=0 && p.x<=800 && p.y>390 && p.y<=600)
{
break;
}
}
/*背景初始化*/
background();
lineation();
/*第四关初始化*/
Init_forth();
for(i = 0;i < 3;i++)
{
Lcd_DrawWord(590+ 70*i,110,96,97,WordSch5[i],0x360360);
}
//第四关数组初始化
int arr3[3][3]={1,2,0,5,6,3,4,7,8};
//第四关可走20步
step=20;
//开始第四关
Play_game(step,arr3,&a);
if(a==0)//输了返回开始游戏
{
sleep(3);
goto Init_Game;
}
while(1)
{
Point p=Get_Touch();
if(p.x>=0 && p.x<=800 && p.y>390 && p.y<=600)
{
break;
}
}
/*背景初始化*/
background();
lineation();
/*第五关初始化*/
Init_fifth();
for(i = 0;i < 3;i++)
{
Lcd_DrawWord(590+ 70*i,110,96,97,WordSch6[i],0xFF1231);
}
//第五关数组初始化,
int arr4[3][3]={4,3,5,7,2,1,8,6,0};
//第五关可走20步
step=20;
//开始第五关
Play_game(step,arr4,&a);
if(a==0)//输了返回开始游戏
{
sleep(3);
goto Init_Game;
}
sleep(5);
//显示进阶界面
Lcd_DisplayBmp(0,0,"blue.bmp");
while(1)
{
Point p=Get_Touch();
if(p.x>=0 && p.x<=800 && p.y>390 && p.y<=600)
{
break;
}
}
#endif
//初始化新的地图
background();
lineation1();
/*最后一关初始化*/
Init_last();
//最后数组初始化
int arr5[4][4]={97,99,103,100,101,98,107,104,122,105,102,108,109,106,110,111};
//最后一关20步
step=20;
//开始最后一关
Play_game1(step,arr5,&a);
if(a==0)//输了返回开始游戏
{
sleep(3);
goto Init_Game;
}
}
背景代码:
#include"head.h"
#include"screen.h"
#define ROW 160
#define COLUMN 200
/*
画格子(后五关)
*/
void lineation1(void)
{
for(int y=0;y<480;y++)
{
if(y%120==0)
{
for(int x=0;x<=600;x++)
{
Lcd_DrawPoint(x,y,0x000000);
}
}
}
for(int x=0;x<=600;x++)
{
if(x%150==0)
{
for(int y=0;y<480;y++)
{
Lcd_DrawPoint(x,y,0x000000);
}
}
}
}
/*
搭建华容道背景(后五关)
*/
void background(void)
{
for(int y=0;y<480;y++)
{
for(int x=0;x<800;x++)
{
Lcd_DrawPoint(x,y,0xFFFFFF);
}
}
}
/*
画格子(前五关)
*/
void lineation(void)
{
for(int y=0;y<480;y++)
{
if(y%ROW==0)
{
for(int x=0;x<=600;x++)
{
Lcd_DrawPoint(x,y,0x000000);
}
}
}
for(int x=0;x<=600;x++)
{
if(x%COLUMN==0)
{
for(int y=0;y<480;y++)
{
Lcd_DrawPoint(x,y,0x000000);
}
}
}
}
/*第一关初始化*/
void Init_first()
{
int arr[3][3]={1,2,3,4,8,5,7,0,6};
int i,j;
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
if(arr[j][i]==1)
{
Lcd_DisplayBmp(20+i*200,10+j*100,"1.bmp");
}
if(arr[j][i]==2)
{
Lcd_DisplayBmp(20+i*200,10+j*100,"2.bmp");
}
if(arr[j][i]==3)
{
Lcd_DisplayBmp(20+i*200,10+j*100,"3.bmp");
}
if(arr[j][i]==4)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"4.bmp");
}
if(arr[j][i]==8)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"8.bmp");
}
if(arr[j][i]==5)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"5.bmp");
}
if(arr[j][i]==7)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"7.bmp");
}
if(arr[j][i]==0)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"0.bmp");
}
if(arr[j][i]==6)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"6.bmp");
}
}
}
}
/*第二关初始化*/
void Init_second()
{
int arr[3][3]={1,3,6,4,2,0,7,5,8};
int i,j;
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
if(arr[j][i]==1)
{
Lcd_DisplayBmp(20+i*200,10+j*100,"1.bmp");
}
if(arr[j][i]==3)
{
Lcd_DisplayBmp(20+i*200,10+j*100,"3.bmp");
}
if(arr[j][i]==6)
{
Lcd_DisplayBmp(20+i*200,10+j*100,"6.bmp");
}
if(arr[j][i]==4)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"4.bmp");
}
if(arr[j][i]==2)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"2.bmp");
}
if(arr[j][i]==0)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"0.bmp");
}
if(arr[j][i]==7)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"7.bmp");
}
if(arr[j][i]==5)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"5.bmp");
}
if(arr[j][i]==8)
{
Lcd_DisplayBmp(20+i*200,10+j*170,"8.bmp");
}
}
}
}
/*第三关初始化*/
void Init_third()
{
int arr[3][3]={2,3,5,1,4,6,0,7,8};
char bmp_name[64]={0};
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
if(arr[j][i])
{
memset(bmp_name,0,64);
sprintf(bmp_name,"%d.bmp",arr[j][i]);
Lcd_DisplayBmp(20+i*200,10+j*170,bmp_name);
}
else
{
Lcd_DisplayBmp(20+i*200,10+j*170,"0.bmp");
}
}
}
}
/*第四关初始化*/
void Init_forth()
{
int arr[3][3]={1,2,0,5,6,3,4,7,8};
char bmp_name[64]={0};
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
if(arr[j][i])
{
memset(bmp_name,0,64);
sprintf(bmp_name,"%d.bmp",arr[j][i]);
Lcd_DisplayBmp(20+i*200,10+j*170,bmp_name);
}
else
{
Lcd_DisplayBmp(20+i*200,10+j*170,"0.bmp");
}
}
}
}
/*第五关初始化*/
void Init_fifth()
{
int arr[3][3]={4,3,5,7,2,1,8,6,0};
char bmp_name[64]={0};
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
if(arr[j][i])
{
memset(bmp_name,0,64);
sprintf(bmp_name,"%d.bmp",arr[j][i]);
Lcd_DisplayBmp(20+i*200,10+j*170,bmp_name);
}
else
{
Lcd_DisplayBmp(20+i*200,10+j*170,"0.bmp");
}
}
}
}
/*最后一关初始化*/
void Init_last()
{
int arr[4][4]={97,99,103,100,101,98,107,104,122,105,102,108,109,106,110,111};
char bmp_name[1024]={0};
for(int j=0;j<4;j++)
{
for(int i=0;i<4;i++)
{
if(arr[j][i])
{
memset(bmp_name,0,64);
sprintf(bmp_name,"%d.bmp",arr[j][i]);
Lcd_DisplayBmp(25+i*150,10+j*120,bmp_name);
}
}
}
}
屏幕显示代码:
#include "head.h"
//屏幕文件的文件描述符
int Fd_Lcd = -1;
//映射区域的地址
int *Plcd = NULL;
//屏幕文件的路径
#define LCD_PATH "/dev/fb0"
//屏幕文件的大小
#define LCD_WIDTH 800
#define LCD_HEIGHT 480
/*
Lcd_Init:对LCD屏进行初始化
@return : 初始化成功返回0,初始化失败返回-1
*/
int Lcd_Init(void)
{
//打开屏幕文件
Fd_Lcd = open(LCD_PATH,O_RDWR);
if(Fd_Lcd == -1)
{
perror("打开屏幕文件失败");
return -1;
}
//将屏幕文件映射到内存中去
Plcd = mmap(NULL,LCD_WIDTH*LCD_HEIGHT*4,PROT_READ | PROT_WRITE,MAP_SHARED,Fd_Lcd,0);
if(Plcd == MAP_FAILED)
{
perror("映射屏幕文件失败");
return -1;
}
return 0;
}
/*
Lcd_DrawPoint:将屏幕(x,y)处的像素点显示成color颜色
*/
void Lcd_DrawPoint(int x,int y,int color)
{
if(x >= 0 && x < 800 && y >= 0 && y < 480)
{
*(Plcd + 800 * y + x) = color;
}
}
/*
Lcd_DisplayBmp:在(x0,y0)处显示bmpname指定的BMP文件
@return : 显示成功返回0,显示失败返回-1
*/
int Lcd_DisplayBmp(int x0,int y0,const char *bmpname)
{
//1.打开BMP文件
int fd_bmp = open(bmpname,O_RDONLY);
if(fd_bmp == -1)
{
perror("打开BMP文件失败");
return -1;
}
//2.判断bmpname是不是BMP文件
unsigned char buf[2] = {0};
int ret = read(fd_bmp,buf,2);
if(ret == -1)
{
perror("读取BMP文件魔数失败");
return -1;
}
if(buf[0] == 0x42 && buf[1] == 0x4D)
{
//printf();
}
else
{
printf("%s is not bmp file,bye bye!\n",bmpname);
close(fd_bmp);
return -1;
}
//3.读取当前bmp文件的宽度
//将光标偏移至宽度数据之前
lseek(fd_bmp,0x12,SEEK_SET);
int width;
ret = read(fd_bmp,&width,4);
if(ret == -1)
{
perror("读取BMP文件宽度失败");
return -1;
}
//4.读取当前bmp文件的高度
//将光标偏移至高度数据之前
lseek(fd_bmp,0x16,SEEK_SET);
int height;
ret = read(fd_bmp,&height,4);
if(ret == -1)
{
perror("读取BMP文件高度失败");
return -1;
}
//5.读取当前bmp文件的色深
//将光标偏移至色深数据之前
lseek(fd_bmp,0x1C,SEEK_SET);
unsigned short depth;
ret = read(fd_bmp,&depth,2);
if(ret == -1)
{
perror("读取BMP文件色深失败");
return -1;
}
printf("%s is bmp file : width %d height %d depth %d\n",bmpname,width,height,depth);
//求出BMP文件的像素数组一行的大小
int bytesbmpline = abs(width) * (depth / 8);
unsigned int laizi = 0;
//求出赖子的个数
if(bytesbmpline % 4)
{
laizi = 4 - bytesbmpline % 4;
}
printf("laizi = %d\n",laizi);
//求出用来保存像素数组的空间一行的字节数
int bytespiexlline = bytesbmpline + laizi;
//求出用来保存像素数组的空间大小
int size = abs(height) * bytespiexlline;
//把空间开辟出来
unsigned char *piexl = malloc(size);
//将像素数组读取到开辟出来的空间中去
lseek(fd_bmp,54,SEEK_SET);
ret = read(fd_bmp,piexl,size);
if(ret == -1)
{
perror("读取像素数组失败");
return -1;
}
//对数据进行处理
//if depth == 24 每次从空间中取三个字节整合形成一个像素点的颜色值
//将其显示到屏幕的对应位置上去
int x,y;
unsigned char a,r,g,b;
unsigned int color;
int i = 0;
for(y = 0;y < abs(height);y++)
{
for(x = 0;x < abs(width);x++)
{
//获取BMP文件中坐标为(x,y)处的像素点的颜色分量
b = piexl[i++];
g = piexl[i++];
r = piexl[i++];
if(depth == 24)
{
a = 0x00;
}
else if(depth == 32)
{
a = piexl[i++];
}
else
{
printf("%s文件色深不支持!\n",bmpname);
return -1;
}
//将像素点的三个颜色分量合成一个像素点的值
color = (a << 24 | r << 16 | g << 8 | b);
//将BMP文件中坐标为(x,y)处的像素点显示到屏幕的对应位置上去
Lcd_DrawPoint(x0 + x,y0 + abs(height) - y - 1,color);
}
i = i + laizi;
}
close(fd_bmp);
}
/*
Recover_Source:回收资源
*/
void Recover_Source(void)
{
close(Fd_Lcd);
munmap(Plcd,LCD_WIDTH*LCD_HEIGHT*4);
}
/*
Lcd_DrawWord:在位置为(x0,y0)处显示宽为w,高为h,颜色color的汉字
*/
void Lcd_DrawWord(int x0,int y0,int w,int h,char *data,int color)
{
int i,k;
for(i = 0;i < w*h/8;i++)
{
for(k = 0;k < 8;k++)
{
if((data[i] << k) & 0x80)
{
Lcd_DrawPoint(x0 + (i*8+k) % w,y0 + i/ (w/8),color);
}
}
}
}
触摸与游戏规则代码:
#include"screen.h"
#include"head.h"
#include"ev.h"
#define ROW1 3
#define COLUMN1 3
unsigned char WordSch0[5][32*64/8]={
/*-- 文字: g --*/
/*-- 楷体48; 此字体下对应的点阵为:宽x高=32x64 --*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0xE0,0x7E,0x00,0x7F,0xFB,0xFE,0x01,0xFF,0xFF,0xFE,
0x03,0xFF,0xFF,0xFE,0x03,0xFF,0xFF,0xFE,0x07,0xF8,0x7F,0x00,0x07,0xE0,0x3F,0x80,
0x0F,0xE0,0x1F,0x80,0x0F,0xE0,0x1F,0x80,0x0F,0xE0,0x1F,0x80,0x0F,0xE0,0x1F,0x80,
0x0F,0xE0,0x1F,0x80,0x0F,0xE0,0x1F,0x80,0x07,0xE0,0x3F,0x80,0x07,0xF8,0x7F,0x00,
0x03,0xFF,0xFF,0x00,0x03,0xFF,0xFE,0x00,0x07,0xFF,0xFC,0x00,0x0F,0xFF,0xF8,0x00,
0x1F,0x9F,0xE0,0x00,0x1F,0x80,0x00,0x00,0x1F,0x80,0x00,0x00,0x1F,0xE0,0x00,0x00,
0x1F,0xFF,0xFE,0x00,0x0F,0xFF,0xFF,0xC0,0x07,0xFF,0xFF,0xE0,0x07,0xFF,0xFF,0xF0,
0x0F,0xFF,0xFF,0xF8,0x1F,0x80,0x07,0xF8,0x3F,0x00,0x01,0xF8,0x3F,0x00,0x01,0xF8,
0x3F,0x00,0x01,0xF8,0x3F,0x00,0x03,0xF8,0x3F,0xC0,0x07,0xF8,0x1F,0xFF,0xFF,0xF0,
0x0F,0xFF,0xFF,0xE0,0x07,0xFF,0xFF,0x80,0x00,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00},
/*-- 文字: o --*/
/*-- 楷体48; 此字体下对应的点阵为:宽x高=32x64 --*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0xF0,0x00,0x00,0x7F,0xFE,0x00,0x01,0xFF,0xFF,0x00,
0x03,0xFF,0xFF,0x80,0x07,0xFF,0xFF,0xC0,0x0F,0xFC,0x3F,0xE0,0x0F,0xF0,0x1F,0xF0,
0x1F,0xE0,0x07,0xF0,0x1F,0xC0,0x07,0xF8,0x3F,0x80,0x03,0xF8,0x3F,0x80,0x03,0xFC,
0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,
0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,
0x3F,0x80,0x03,0xF8,0x3F,0xC0,0x03,0xF8,0x1F,0xC0,0x07,0xF8,0x1F,0xE0,0x0F,0xF0,
0x0F,0xF8,0x1F,0xE0,0x07,0xFF,0xFF,0xE0,0x03,0xFF,0xFF,0xC0,0x01,0xFF,0xFF,0x80,
0x00,0xFF,0xFE,0x00,0x00,0x3F,0xFC,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
/*-- 文字: o --*/
/*-- 楷体48; 此字体下对应的点阵为:宽x高=32x64 --*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0xF0,0x00,0x00,0x7F,0xFE,0x00,0x01,0xFF,0xFF,0x00,
0x03,0xFF,0xFF,0x80,0x07,0xFF,0xFF,0xC0,0x0F,0xFC,0x3F,0xE0,0x0F,0xF0,0x1F,0xF0,
0x1F,0xE0,0x07,0xF0,0x1F,0xC0,0x07,0xF8,0x3F,0x80,0x03,0xF8,0x3F,0x80,0x03,0xFC,
0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,
0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,0x3F,0x80,0x01,0xFC,
0x3F,0x80,0x03,0xF8,0x3F,0xC0,0x03,0xF8,0x1F,0xC0,0x07,0xF8,0x1F,0xE0,0x0F,0xF0,
0x0F,0xF8,0x1F,0xE0,0x07,0xFF,0xFF,0xE0,0x03,0xFF,0xFF,0xC0,0x01,0xFF,0xFF,0x80,
0x00,0xFF,0xFE,0x00,0x00,0x3F,0xFC,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
/*-- 文字: n --*/
/*-- 楷体48; 此字体下对应的点阵为:宽x高=32x64 --*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1F,0x81,0xFE,0x00,0x1F,0x8F,0xFF,0x80,0x1F,0x9F,0xFF,0xC0,
0x1F,0xBF,0xFF,0xE0,0x1F,0xFF,0xFF,0xF0,0x1F,0xFE,0x0F,0xF0,0x1F,0xF8,0x07,0xF0,
0x1F,0xF0,0x03,0xF8,0x1F,0xE0,0x03,0xF8,0x1F,0xE0,0x03,0xF8,0x1F,0xC0,0x03,0xF8,
0x1F,0xC0,0x03,0xF8,0x1F,0xC0,0x03,0xF8,0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,
0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,
0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,
0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,
0x1F,0x80,0x03,0xF8,0x1F,0x80,0x03,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
/*-- 文字: ! --*/
/*-- 楷体48; 此字体下对应的点阵为:宽x高=32x64 --*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0x00,0x00,0x01,0xFF,0x00,0x00,0x01,0xFF,0x00,
0x00,0x01,0xFF,0x00,0x00,0x01,0xFF,0x00,0x00,0x01,0xFF,0x00,0x00,0x01,0xFF,0x00,
0x00,0x01,0xFF,0x00,0x00,0x01,0xFF,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,
0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,
0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,
0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,
0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x7C,0x00,
0x00,0x00,0x7C,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x7C,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0xFE,0x00,
0x00,0x01,0xFF,0x00,0x00,0x01,0xFF,0x00,0x00,0x03,0xFF,0x00,0x00,0x01,0xFF,0x00,
0x00,0x01,0xFF,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
};
void Play_game(int step,int arr[3][3],int*a)
{
while(1)
{
//获取滑动方向
int t=get_slip_way();
if(step==0)
{
Lcd_DisplayBmp(180,0,"lose.bmp");
for(int i = 0;i < 5;i++)
{
Lcd_DrawWord(280 + 70 * i,400,32,64,WordSch0[i],0xFFFF00);
}
*a=0;
break;
}
int x=Judge_change0(arr,t);
if(x)//交换成功
{
//测试
char bmp_name[64]={0};
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
if(arr[j][i])
{
memset(bmp_name,0,64);
sprintf(bmp_name,"%d.bmp",arr[j][i]);
Lcd_DisplayBmp(20+i*200,10+j*170,bmp_name);
}
else
{
Lcd_DisplayBmp(20+i*200,10+j*170,"0.bmp");
}
}
}
int status=Get_states(arr);
if(IS_Win(status))
{
Lcd_DisplayBmp(150,80,"win.bmp");
for(int i= 0;i < 5;i++)
{
Lcd_DrawWord(280 + 70 * i,400,32,64,WordSch0[i],0xFFFF00);
}
*a=1;
break;
}
else
{
step--;
continue;
}
}
else//交换失败
{
continue;
}
}
}
/*
最后一关
*/
void Play_game1(int step,int arr[4][4],int*a)
{
while(1)
{
//获取滑动方向
int t=get_slip_way();
if(step==0)
{
Lcd_DisplayBmp(180,0,"lose.bmp");
for(int i = 0;i < 5;i++)
{
Lcd_DrawWord(280 + 70 * i,400,32,64,WordSch0[i],0xFFFF00);
}
*a=0;
break;
}
int x=Judge_change01(arr,t);
if(x)//交换成功
{
//测试
char bmp_name[64]={0};
for(int j=0;j<4;j++)
{
for(int i=0;i<4;i++)
{
if(arr[j][i])
{
memset(bmp_name,0,64);
sprintf(bmp_name,"%d.bmp",arr[j][i]);
Lcd_DisplayBmp(25+i*150,10+j*120,bmp_name);
}
}
}
//int status=Get_states1(arr);
if(IS_win1(arr))
{
Lcd_DisplayBmp(150,80,"win.bmp");
for(int i= 0;i < 5;i++)
{
Lcd_DrawWord(280 + 70 * i,400,32,64,WordSch0[i],0xFFFF00);
}
*a=1;
break;
}
else
{
step--;
continue;
}
}
else//交换失败
{
continue;
}
}
}
//转化
int Get_states(int arr[3][3])
{
int status=0;
int i,j;
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
status*=10;
status+=arr[j][i];
}
}
return status;
}
//最后一关转化
int Get_states1(int arr[4][4])
{
int status=0;
int i,j;
for(j=0;j<4;j++)
{
for(i=0;i<4;i++)
{
status*=10;
status+=arr[j][i]%10;
}
}
return status;
}
/*
最后一关胜利
*/
int IS_win1(int arr[4][4])
{
#if 0
if(status==7890123456789012)
return 1;
else
return 0;
#endif
for(int j=0;j<4;j++)
{
for(int i=0;i<4;i++)
{
if(arr[j][i]>arr[j][i+1]&&i+1<4)
{
return 0;
}
}
}
return 1;
}
/*
判断是否胜利
*/
int IS_Win(int status)
{
#if 0
int win=0;
int flag=1;
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
if(arr[j][i+1]<arr[j][i]&&i+1<3)
{
if(arr[j][i+1]==0)
{
if(j==2&&i==1)//即前面已经排好序且最后一个是0,符合胜利条件
{
flag=1;
}
else
{
flag=0;
break;
}
}
else
{
flag=0;
break;
}
}
}
}
if(flag)
{
win=1;
}
if(win)
{
return 1;
}
else
return 0;
#endif
#if 1
if(status==123456780)
return 1;
else
return 0;
#endif
}
/*
最后一关交换
*/
int Judge_change01(int arr[4][4],int t)
{
if(t==0)//上滑,找到上面是122的点交换,没找到则返回0
{
for(int j=0;j<4;j++)
{
for(int i=0;i<4;i++)
{
if(arr[j][i]==122&&j+1<4)
{
arr[j][i]=arr[j+1][i];
arr[j+1][i]=122;
return 1;
}
}
}
}
else if(t==1)//下滑,找到下面是122的点交换,没找到则返回0
{
for(int j=0;j<4;j++)
{
for(int i=0;i<4;i++)
{
if(arr[j][i]==122&&j-1>=0)
{
arr[j][i]=arr[j-1][i];
arr[j-1][i]=122;
return 1;
}
}
}
}
else if(t==2)//左滑,找到左边是122的点交换,没找到则返回0
{
for(int j=0;j<4;j++)
{
for(int i=0;i<4;i++)
{
if(arr[j][i]==122&&i+1<4)
{
arr[j][i]=arr[j][i+1];
arr[j][i+1]=122;
return 1;
}
}
}
}
else if(t==3)//右滑,找到右边是122的点交换,没找到则返回0
{
for(int j=0;j<4;j++)
{
for(int i=0;i<4;i++)
{
if(arr[j][i]==122&&i-1>=0)
{
arr[j][i]=arr[j][i-1];
arr[j][i-1]=122;
return 1;
}
}
}
}
else
return 0;
}
/*
判断是否与0发生交换
@return:交换成功返回1,交换失败1返回0
*/
int Judge_change0(int arr[3][3],int t)
{
if(t==0)//上滑,找到上面是0的点交换,没找到则返回0
{
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
if(arr[j][i]==0&&j+1<3)
{
arr[j][i]=arr[j+1][i];
arr[j+1][i]=0;
return 1;
}
}
}
}
else if(t==1)//下滑,找到下面是0的点交换,没找到则返回0
{
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
if(arr[j][i]==0&&j-1>=0)
{
arr[j][i]=arr[j-1][i];
arr[j-1][i]=0;
return 1;
}
}
}
}
else if(t==2)//左滑,找到左边是0的点交换,没找到则返回0
{
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
if(arr[j][i]==0&&i+1<3)
{
arr[j][i]=arr[j][i+1];
arr[j][i+1]=0;
return 1;
}
}
}
}
else if(t==3)//右滑,找到右边是0的点交换,没找到则返回0
{
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
if(arr[j][i]==0&&i-1>=0)
{
arr[j][i]=arr[j][i-1];
arr[j][i-1]=0;
return 1;
}
}
}
}
else
return 0;
}
#if 0
/*
Judge_origent:判断滑动方向
@return:"'u'向上滑动(0)'d' :向下滑动(1)'l':向左滑动(2) 'r':向右滑动(3)
*/
int Judge_origent(void)
{
int x,y;
int px=-1;
int px1=-1;
int py=-1;
int py1=-1;
int flag=0;
//打开触碰屏
int fd_touch=open(TOUCH_PATH,O_RDONLY);
if(fd_touch==-1)
{
perror("打开触碰文件失败");
return -1;
}
struct input_event ev;
while(1)
{
read(fd_touch,&ev,sizeof(ev));
if(ev.type==EV_ABS)
{
if(ev.code==0)
{
x=ev.value*800/1024;
}
else
{
y=ev.value*400/600;
}
}
if(ev.type == EV_KEY && ev.code == 330 && ev.value == 1)
{
px=x;
py=y;
flag==1;
}
if(flag)
{
if(px1==-1&&py1==-1)
{
Point p1=Get_Touch();
px1=p1.x;
py1=p1.y;
flag==0;
}
if(py<py1&&px>px1-70&&px<70-px1)
{
return 0;
}
if(py>py1&&px>px1-70&&px<70-px1)
{
return 1;
}
if(px>px1&&py>py1-70&&py<70-py1)
{
return 2;
}
if(px<px1&&py>py1-70&&py<70-py1)
{
return 3;
}
}
}
}
#endif
/*
get_slip_way:获取滑动方向
*/
int get_slip_way()
{
Point p1;
p1.x = -1;
p1.y = -1;
Point p2;
p2.x = -1;
p2.y = -1;
int fd = open("/dev/input/event0",O_RDWR);
if(fd == 0)
{
perror("open error\n");
return -1;
}
struct input_event ev;
int ret;
int flag = 0;
int count = 0;
while(1)
{
ret = read(fd,&ev,sizeof(ev));
if(ret != sizeof(ev))
{
close(fd);
perror("read error");
return -1;
}
//判断触摸事件,并获取X值
if(ev.type == EV_ABS && ev.code == ABS_X)
{
if(p1.x < 0)
{
p1.x = ev.value;
}
else
{
p2.x = ev.value;
}
}
if(ev.type == EV_ABS && ev.code == ABS_Y)
{
if(p1.y < 0)
{
p1.y = ev.value;
}
else
{
p2.y = ev.value;
}
}
if(ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value==0)
{
int dt_x = abs(p1.x - p2.x);
int dt_y = abs(p1.y - p2.y);
if(dt_x > 2*dt_y)
{
if(p1.x > p2.x)
{
return 2;
}
if(p1.x < p2.x)
{
return 3;
}
}
else if (2*dt_x < dt_y)
{
if(p1.y > p2.y)
{
return 0;
}
if(p1.y < p2.y)
{
return 1;
}
}
else
{
p1.x = -1;
p1.y = -1;
p2.x = -1;
p2.y = -1;
}
}
}
}
#if 0
typedef struct point
{
int x;
int y;
}Point;
#define TOUCH_PATH "/dev/input/event0"
#endif
//获得触碰的信息
Point Get_Touch(void)
{
Point p;
p.x=p.y=-1;
//打开触碰屏
int fd_touch=open(TOUCH_PATH,O_RDONLY);
if(fd_touch==-1)
{
perror("打开触碰文件失败");
return p;
}
struct input_event ev;
//读取触摸屏的内容
while(1)
{
int ret=read(fd_touch,&ev,sizeof(ev));
if(ret!=sizeof(ev))
{
continue;
}
if(ev.type == EV_ABS && ev.code==ABS_X)
{
//意味着这是一个x轴的坐标
p.x=ev.value;
}
else if(ev.type==EV_ABS && ev.code==ABS_Y)
{
//意味这是一个y轴坐标
p.y=ev.value;
}
else if(ev.type==EV_KEY && ev.code==BTN_TOUCH && ev.value==0)
{
//意味着手指离开屏幕
}
if(p.x>=0 && p.y>=0)
{
close(fd_touch);
return p;
}
}
}