GEC6818开发板制作五子棋小游戏

利用GEC6818开发板制作五子棋小游戏

软件及版本:Ubuntu18.04.4,SecureCRT,Source Insight4.0,VMware Workstation Pro。

主要代码:

main.c

#include <stdio.h>
#include "light.h"
#include "ev.h"
#include "bmp.h"
#include "game.h"

int x,y;
int main()
{
	Lcd_Init();
	Dis_pic("xiaqi.bmp");//封面图片
    Get_ev(&x, & y);//开始操作
	return 0;
}

bmp.h

#ifndef __BMP_H__
#define __BMP_H__

#include "light.h"
#include <stdio.h>
#include "ev.h"

void Dis_pan();

int Dis_pic(char *pic);


//void Draw_Ck(int x0,int y0,int color);

#endif

bmp.c

#include "bmp.h"
#include "ev.h"

int Dis_pic(char *pic)
{
	int fd = open(pic,O_RDONLY);
	if(-1 == fd)
	{
		perror("open error");
		return -1;
	}
	int width,height;
	short depth;
	lseek(fd,0x12,SEEK_SET);
	read(fd,&width,4);
	read(fd,&height,4);
	
	lseek(fd,0x1c,SEEK_SET);
	read(fd,&depth,2);
	printf("%d %d %d\n",width,height,depth);
	int laizi =( 4-(width * depth / 8) % 4) % 4;
	unsigned char color_buf[height*(width*depth/8+laizi)];//32 24
	char color_a = 0,color_r,color_g,color_b;//颜色分量
	unsigned int color;//像素点的颜色
	unsigned char *p = color_buf;
	lseek(fd,0x36,SEEK_SET);

	int r;
	r=read(fd,color_buf,height*(width*depth/8+laizi));
	printf("%d %d\n",r,height*(width*depth/8+laizi));
	for(int i=height-1;i>=0;i--)
	{
		for(int j=0;j<width;j++)
			{
				color_b= *p++;//b颜色
				color_g= *p++;
				color_r= *p++;
				if(32 == depth)
				{
					color_a= *p++;
				}
				color = color_a << 24 | color_r << 16 |color_g << 8 | color_b;//屏幕需要的颜色a r g b
				Display(color, j, i);
			}
			p+= laizi;
	}
}

void Dis_pan()//棋盘
{
	for(int i=0;i<480;i++)
	{
		for(int j = 0;j<800;j++)
		{
			if(i%40 == 0||j%40 == 0)
			Display(0x00, j, i);
			else
			{
			Display(0xffff, j, i);
			}
		}
	}
}

/*void Draw_Ck(int x0,int y0,int color)
{
	int flag;
	for(int i= 0;i<480;i++)
		{
			for(int j=0;j<800;j++)
			{
			    if((j-x0)*(j-x0)+(i-y0)*(i-y0)<=10*10)
			    {
			    	if(flag)
					Display(0x0000ff,j,i);
					else
					Display(0xffffff, j, i);
				}
			}
		}		
}*/

ev.h

#ifndef __EV_H__
#define __EV_H__

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <math.h>
#include <linux/input.h>


int Get_ev(int *x,int *y);


#endif

ev.c

#include "ev.h"
#include "bmp.h"
#include "game.h"


int Get_ev(int *x,int *y)
{	
    int flag=0;
    int flag1=3;
	int fd = open("/dev/input/event0",O_RDONLY);
	if(-1 == fd)
	{
		perror("open error");
		return -1;
	}
	struct input_event ev;
	int x1,y1;
	
	while(1)
		{
			read(fd,&ev,sizeof(ev));
			printf("ev_type = %d code = %d value = %d\n",ev.type,ev.code,ev.value);
            if(ev.type == EV_ABS)
            {
            	if(ev.code == 0)//x
            	{
            		x1 = ev.value * 800/1024;
            		
            	}
            	else
            	{
            		y1 = ev.value * 480/600;
            	}
            }
            if(ev.type == EV_KEY && ev.code == 330 && ev.value == 1)//保存初始左边
            {
            	*x = x1;
            	*y = y1;
            }
            if(ev.type == EV_KEY && ev.code == 330 && ev.value == 0)
            {
            	if(*x == x1 && *y == y1)//点击
            	{
            		printf("dianji\n");
            		if(flag1==1)
            		Game_Change(x1,y1);
            		if(flag1!=1)
            		{
            		Dis_pan();
            		flag1=1;
            		}
            	}
            	if(x1 > *x)//右滑
            	{
            		printf("youhua\n");

            		flag1=2;
            	}
            	if(x1 < *x)//左滑
            	{
            		printf("zuohua\n");
            	
            		flag1=3;
            	}
            	Game_Over();
            }
		}
	}

game.h

#ifndef __GAME_H__
#define __GAME_H__

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <math.h>
#include <linux/input.h>


int Game_Change();
int Dis_zi(int x,int y,int flag);
int Game_Over();
int clear();





#endif

game.c

#include "ev.h"
#include "bmp.h"
#include "game.h"

int flag= 0;
unsigned int Game_buf[12][20] = {0};

int Game_Change(int x,int y)
{
	int i,j;
	i = x % 40;
	j = y % 40;
	if(i > 20)
	{
		i = x / 40 + 1;
	}
	else
	{
		i = x / 40;
	}
	if(j > 20)
	{
		j = y / 40 + 1;
	}
	else 
	{
		j = y / 40;
	}	
	x = i * 40;
	y = j * 40;
	if(Game_buf[j][i]==0)
	{
		if(flag)
		{
			Game_buf[j][i]=1;	
			Dis_zi(y,x,flag);
			
		}
		else
		{
			Game_buf[j][i]=2;
			Dis_zi(y,x,flag);
		}	
		flag = ~flag;
	}


}
int clear()
{
	for(int a=0;a<12;a++)
	{
		for(int b = 0;b < 20;b++)
		{
			Game_buf[a][b]=0;

		}
	}
}

int Dis_zi(int x,int y,int flag)
{
	int i,j;
	if(flag)
	{
		for(i=0;i<480;i++)
		{
			for(j=0;j<800;j++)
			{
				if((i-x)*(i-x)+(j-y)*(j-y)<=330)
				{
					Display(0x00,j,i);
				}
			}
		}
	}
	else
	{
		for(i=0;i<480;i++)
		{
			for(j=0;j<800;j++)
			{
				if((i-x)*(i-x)+(j-y)*(j-y)<=330)
				{
					Display(0x00ffffff,j,i);
				}
			}
		}
	}
}

int Game_Over()
{
	int a,b,h=1,s=1,x=1,j=1;
	for(a = 0;a < 12;a++)
	{
		for(b = 0;b<20;b++)
		{
			if(Game_buf[a][b]!=0)
			{
			
				while(h!=5)
				{
					if(Game_buf[a][b]==Game_buf[a+h][b])
					h++;
					else
					break;
				}
				while(s!=5)
				{
					if(Game_buf[a][b]==Game_buf[a][b+s])
					s++;
					else
					break;
				}
				while(x!=5)
				{
					if(Game_buf[a][b]==Game_buf[a+x][b+x])
					x++; 
					else
					break;
				}
				while(j!=5)
				{
					if(Game_buf[a][b]==Game_buf[a+j][b-j])
					j++; 
					else
					break;
				}
				if(h==5||s==5||x==5||j==5)
				{
					Dis_pic("bucuoo.bmp");
					clear();
				}
			}
		}
	}	
}

light.h

#ifndef __LIGHT_H__
#define __LIGHT_H__


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <math.h>

int Display(int color,int x,int y);
int Lcd_Init();
void Dis_wh();
void Dis_qu();
void Dis_cir();
void Dis_tri();



#endif

light.c

#include "light.h"
unsigned int *plcd =NULL;
#include "ev.h"


int Display(int color,int x,int y)
{
	if(x >= 0 && x <= 800 && y >= 0 && y <= 480)
	{
		*(plcd + y*800+ x) = color;
	}
}


int Lcd_Init()
{
	int fd = open("/dev/fb0",O_RDWR);
	if(-1==fd)
	{
		perror("open error");
		return -1;
	}
	plcd =mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
}

void Dis_wh()
{
	for(int i = 0;i < 480;i++)
	{
		for(int j = 0;j < 800;j++)
		{
			Display(0xffffff,j,i);
		}
	}
	
}
//画矩形
void Dis_qu()
{
	for(int i= 100;i<300;i++)
		{
			for(int j=100;j<600;j++)
			{
				Display(0xffffff,j,i);
			}
		}
}

//圆形
void Dis_cir()
{
    for(int i= 100;i<480;i++)
		{
			for(int j=100;j<800;j++)
			{ 
			    if((j-350)*(j-350)+(i-200)*(i-200)<=100*100)
				Display(0x00ffff,j,i);
				if((j-400)*(j-400)+(i-275)*(i-275)<=100*100)
				Display(0xff00ff,j,i);
				if((j-450)*(j-450)+(i-200)*(i-200)<=100*100)
				Display(0xffff00,j,i);
				if(((j-350)*(j-350)+(i-200)*(i-200)<=100*100)&&((j-400)*(j-400)+(i-275)*(i-275)<=100*100)&&((j-450)*(j-450)+(i-200)*(i-200)<=100*100))
				Display(0xff0000,j,i);
			}
		}
}
void Dis_tri()
{
	for(int i = 0;i<480;i++)
		   {
			   for(int j = 0;j<800;j++)
			   {
			   		if(i+j<=400)
			   		Display(0xff00ff,j,i);
			   }
		   }
}

/*
void Dis_tri()
{
	for(int y = 0;y<480;y++)
		   {
			   for(int x = 0;x<800;x++)
			   {
			   		if(x>=100 && y<=300 && y>=100 && y<=-x+400&& y>=x)
			   		Display(0xff00ff,x,y);
			   }
		   }
}
*/

/*int main()
{
	Lcd_Init();
	 Dis_wh();
	//Dis_wh();
	//Dis_qu();
	//Dis_cir();
	Dis_tri();
	 
	return 0;
	
}*/

代码完成后在Ubuntu控制台终端进行验证 。

代码验证无误后,通过SecureCRT将其烧录到GEC6818开发板当中。

最后即可在GEC6818开发板上实现五子棋小游戏。 

  • 16
    点赞
  • 166
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值