#include
#include
#include
#include
#include
#include
#include
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
int *plcd = NULL;
char *bmp_name[] =
{
"color_x80_2.bmp",
"color_x80_4.bmp",
"color_x80_8.bmp",
"color_x80_16.bmp",
"color_x80_32.bmp",
"color_x80_64.bmp",
"color_x80_128.bmp",
"color_x80_256.bmp",
"color_x80_512.bmp",
"color_x80_1024.bmp",
"color_x80_2048.bmp",
"color_x80_4096.bmp",
"color_x80_8192.bmp",
"color_x80_16384.bmp",
"color_x80_32768.bmp",
"color_x80_65536.bmp",
"color_x80_131072.bmp",
"color_x80_262144.bmp",
"color_x80_524288.bmp",
};
int matrix[4][4] =
{
0,0,0,0,
0,2,0,0,
0,0,0,0,
0,0,0,4,
};
//
/*
@dis 在坐标为(x,y)画一个颜色为color的点
@x 横坐标
@y 纵坐标
@return void
*/
void lcd_draw_point(int x, int y, int color)
{
*(plcd + x + 800*y) = color;
}
//实现一个函数,在坐标为x0,y0的位置显示一个矩形,宽度为w,高度为h,颜色为color
void lcd_draw_rect(int x0, int y0, int w, int h, int color)
{
int x, y;
for (y = y0; y < y0+h; y++)
{
for (x = x0; x < x0+w; x++)
lcd_draw_point(x, y, color);
}
}
//在顶点为x0,y0画一个n*n的颜色矩阵,每个矩形的大小是w*w,//矩形和矩形之间的间隔为dis
void lcd_draw_drect(int x0, int y0, int n, int w, int dis, int color)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
lcd_draw_rect(x0+(w+dis)*j, y0+(w+dis)*i, w,w, color);
}
}
//在坐标为(x0,y0)的位置显示一张100*100的bmp图片
void lcd_draw_bmp(char *file, int x0, int y0)
{
//打开bmp图片
int fd;
fd = open(file, O_RDWR);
if (-1 == fd)
{
printf("open %s error !\n", file);
return ;
}
//读取bmp图片
char pixel[100*100*3 + 54];
read(fd, pixel, 100*100*3 + 54);
int j = 0,x, y;
int color;
char r, g, b;
for (y = 0; y < 100; y++)
{
for (x = 0; x < 100; x++)
{
b = pixel[54 + j];
j++;
g = pixel[54 + j];
j++;
r = pixel[54 + j];
j++;
color = (r << 16) | (g << 8) | b;
lcd_draw_point(x+x0, 99-y+y0, color);
}
}
close(fd);
}
char *get_filename_by_digit(int digit)
{
int index;
switch (digit)
{
case 2:
index = 0;
break;
case 4:
index = 1;
break;
case 8:
index = 2;
break;
case 16:
index = 3;
break;
case 32:
index = 4;
break;
case 64:
index = 5;
break;
case 128:
index = 6;
break;
case 256:
index = 7;
break;
case 512:
index = 8;
break;
case 1024:
index = 9;
break;
case 2048:
index = 10;
break;
case 4096:
index = 11;
break;
case 8192:
index = 12;
break;
case 16384:
index = 13;
break;
case 32768:
index = 14;
break;
case 65536:
index = 15;
break;
case 131072:
index = 16;
break;
case 262144:
index = 17;
break;
case 524288:
index = 18;
break;
default:
index = 0;
break;
}
return bmp_name[index];
}
void lcd_draw_matrix()
{
int i, j;
for (i = 0 ; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (matrix[i][j] == 0)
{
//画一个颜色矩形
// void lcd_draw_rect(int x0, int y0, int //w, int h, int color)
lcd_draw_rect(20+ 120*j, 20+120*i,100, 100, 0x0000ff00);
}
else
{
//画一张对应数字的bmp图片
// void lcd_draw_bmp(char *file, int x0, //int y0)
lcd_draw_bmp(get_filename_by_digit(matrix[i][j]),20+ 120*j, 20+120*i);
}
}
}
}
//获得手指的滑动方向
int get_finger_move_direct()
{
int fd;
int direct;
fd = open("/dev/event0", O_RDWR);
//if (fd == -1)
if (-1 == fd)
{
printf("open errror!\n");
return -1;
}
struct input_event ev;
int x0 = -1, y0 = -1;
int x1, y1;
while (1)
{
read(fd, &ev, sizeof(ev));
//识别当前的事件是不是触摸事件
if ((ev.type == EV_ABS) && (ev.code == ABS_X))
{
if (x0 == -1)
x0 = ev.value;
else
x1 = ev.value;
printf("x: %d\n", x0);
}
else if ((ev.type == EV_ABS) && (ev.code == ABS_Y))
{
if (y0 == -1)
y0 = ev.value;
else
y1 = ev.value;
printf("y: %d\n", y0);
}
if ((ev.type == EV_ABS) && (ev.code == ABS_PRESSURE) && (ev.value == 0))
{
break;
}
}
//判断手指滑动的方向了
int delta_x,delta_y;
delta_x = abs(x1-x0);
delta_y = abs(y1-y0);
if (delta_y > delta_x)
{
if (y1 > y0)
{
printf("up....\n");
direct = UP;
}
else
{
printf("down ...\n");
direct = DOWN;
}
}
else
{
if (x1 > x0)
{
printf("right....\n");
direct = RIGHT;
}
else
{
printf("left ...\n");
direct = LEFT;
}
}
close(fd);
return direct;
}
void handle_up()
{
int i,j,k;
for(j=0;j<4;j++)
{
int flag=1;
for(i=1;i<4;i++)
{//移动点i,j
for(k=i;k>=1;k--)//移动到的现在的位置
if(matrix[k-1][j]||k==1)//此时停住
{
if(k==1&&matrix[k-1][j]==0)
{
matrix[k-1][j]=matrix[i][j];
matrix[i][j]=0;
continue;
}
if(matrix[i][j]==matrix[k-1][j]&&flag)
{
matrix[k-1][j]=2*matrix[i][j];
matrix[i][j]=0;
flag=0;
}
else
if(k!=i)//如果移动了
{
matrix[k][j]=matrix[i][j];
matrix[i][j]=0;
}
break;
}
}
}
}
void handle_down()
{
int i,j,k;
for(j=0;j<4;++j)
{
int flag=1;
for(i=2;i>=0;--i)
{//移动点i,j
for(k=i;k<=2;++k)//移动到的现在的位置
if(matrix[k+1][j]||k==2)//此时停住
{
if(k==2&&matrix[k+1][j]==0)
{
matrix[k+1][j]=matrix[i][j];
matrix[i][j]=0;
continue;
}
if(matrix[i][j]==matrix[k+1][j]&&flag)
{
matrix[k+1][j]=2*matrix[i][j];
matrix[i][j]=0;
flag=0;
}
else
if(k!=i)//如果移动了
{
matrix[k][j]=matrix[i][j];
matrix[i][j]=0;
}
break;
}
}
}
}
void handle_left()
{
int i,j,k;
for(i=0;i<4;++i)
{
int flag=1;
for(j=1;j<4;++j)
if(matrix[i][j])
{//移动点i,j
for(k=j;k>=1;--k)//移动到的现在的位置
if(matrix[i][k-1]||k==1)//此时停住
{
if(k==1&&matrix[i][k-1]==0)
{
matrix[i][k-1]=matrix[i][j];
matrix[i][j]=0;
continue;
}
if(matrix[i][j]==matrix[i][k-1]&&flag)
{
matrix[i][k-1]=2*matrix[i][j];
matrix[i][j]=0;
flag=0;
}
else
if(k!=j)//如果移动了
{
matrix[i][k]=matrix[i][j];
matrix[i][j]=0;
}
break;
}
}
}
}
void handle_right()
{
int i,j,k;
for(j=2;j>=0;--j)
{
for(i=0;i<4;++i)
{//移动点i,j
int flag=1;
for(k=j;k<=2;++k)//移动到的现在的位置
if(matrix[i][k+1]||k==2)//此时停住
{
if(k==2&&matrix[i][k+1]==0)
{
matrix[i][k+1]=matrix[i][j];
matrix[i][j]=0;
continue;
}
if(matrix[i][j]==matrix[i][k+1]&&flag)
{
matrix[i][k+1]=2*matrix[i][j];
matrix[i][j]=0;
flag=0;
}
else
if(k!=j)//如果移动了
{
matrix[i][k]=matrix[i][j];
matrix[i][j]=0;
}
break;
}
}
}
}
void rand_digital()
{
int n=0,i,j,num;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(matrix[i][j]==0) n++;
if(!n) return;
num=rand()%n+1;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(matrix[i][j]==0)
{
n--;
if(!n){
int t=rand()%2;
if(t==0)
matrix[i][j]=2;
else
matrix[i][j]=4;
}
}
}
int False()
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<3;j++)
if(matrix[i][j]==0||matrix[i][j]==matrix[i][j+1]) return 0;
for(j=0;j<4;j++)
for(i=0;i<3;i++)
if(matrix[i][j]==0||matrix[i+1][j]==matrix[i][j]) return 0;
if(matrix[3][3]==0) return 0;
return 1;
}
void pfalse()
{
lcd_draw_rect(0, 0, 800, 480, 0x0);
lcd_draw_bmp("false.bmp",200,200);
}
void handle(int direct)
{
switch (direct)
{
case UP:
handle_up();
break;
case DOWN:
handle_down();
break;
case LEFT:
handle_left();
break;
case RIGHT:
handle_right();
break;
}
}
void My_2048()
{
int direct;
while(1)
{
lcd_draw_matrix();
direct = get_finger_move_direct();
switch (direct)
{
case UP:
handle_up();
break;
case DOWN:
handle_down();
break;
case LEFT:
handle_left();
break;
case RIGHT:
handle_right();
break;
}
handle(direct);
if(False())
{
pfalse();
return;
}
rand_digital();
}
}
int main(int argc, char *argv[])
{
//打开lcd设备 /dev/fb0
//变量的命名规则:
//只能包含数字、字母和下划线,不能以数字开头
//见其名知其意,尽量别用拼音
int fd;
fd = open("/dev/fb0", O_RDWR);
//if (fd == -1)
if (-1 == fd)
{
printf("open errror!\n");
return 0;
}
plcd = mmap(NULL, 800*480*4,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0);
if (MAP_FAILED == plcd)
{
printf("mmap error !\n");
return 0;
}
#if 0
int i;
for (i = 0; i < 800*480;i++)
*(plcd + i) = 0x0000ff00;
#endif
#if 0
int x, y, color;
color = 0x000000ff;
for (y = 0; y < 480; y++)
{
for (x = 0; x < 800; x++)
lcd_draw_point(x, y, color);
}
#endif
//在坐标为(200,200)的位置,显示一个100*100的颜色矩形,颜色为红色
#if 0
for (y = 200; y < 200+100; y++)
{
for (x = 200; x < 200 + 100; x++)
lcd_draw_point(x, y, color);
}
#endif
lcd_draw_rect(0, 0, 800, 480, 0x0);
//void lcd_draw_drect(int x0, int y0, int n, int //w, int dis, int color)
//lcd_draw_drect(20, 20, 4, 100, 20, 0x0000ff00);
// srand();
// rand();
// lcd_draw_bmp("digit_2.bmp", 50, 50);
My_2048();
return 0;
}
相关文件下载