资讯详情

基于c++EGE图形库编写的五子棋小游戏

基于c EGE图形库编写的五子棋游戏

EGE图形库

新手推荐使用

EGE(Easy Graphics Engine),是windows下面的简易绘图库类似于BGI(graphics.h)的面向C/C 语言新手图形库的目标是取代它TC的BGI库而存在。

使用方法及TC中的graphics.h相当接近,对于新手来说,简单、友好、易于使用、免费开源,界面意义直观,即使没有接触过图形编程,也能快速学习基本绘图。

目前,EGE图形库得到了完美的支持VC6, VC2008, VC2010, VC2012, VC2013, VC2015, VC2017, VC2019, C-Free, DevCpp, Code::Blocks, wxDev, Eclipse for C/C 等IDE,即支持使用MSVC和MinGW编译环境IDE。假如你需要在那里VC下使用graphics.h,那么ege它将是一个很好的替代品。

引用自EGE图形库官网

EGE官网:Easy Graphics Engine – EGE图形库主站 (xege.org)


结果展示

在这里插入图片描述

五子棋简单


实现原理

步骤:

1.打印棋盘;

2.打印棋子;

3.判断胜利方;

在整个棋盘图纸上添加黑白子并不容易写,所以我们可以改变我们的想法。将棋盘分为n个格子,分别为:

1)空白格子

2)下黑子格子

3)下了白子格子

然后我们需要拼接三种图片

编写过程

使用EGE图形库:

在EGE在官网下载小熊猫dev-c ,单击菜单中的文件→新建→新建项目→多媒体→Graphics.h

这就是EGE的图形库了

头文件加载:
#include<graphics.h> //EGE图形库 #include<cstdio> 
所需函数如下:
int chessBord[15][15];  //棋盘 int player=2;    ///当前玩家 PIMAGE picture[3];   //棋盘图片 mouse_msg msg;    //鼠标 int loadPicture();   //加载图片函数 int printBord();   //打印棋盘 int judgeWiner(int x,int y); ///判断输赢函数 

为了将图片导入程序,需要提前加载

设置棋盘括号中的数字为棋盘大小,可自行设置。例如,我的例子是15*15

加载图片函数:
int loadPicture() {  char path[100];   //加载图片路径  for(int i=0;i<3;i  )  {   picture[i]=newimage(); ///向计算机申请图片空间   sprintf(path,"image/%d.jpg",i);///   getimage(picture[i],path);////存储系统中的图片数据picture  }  return 0; } 

有了图片,我们可以打印棋盘。

打印棋盘函数:
int printBord() {  for(int i=0;i<15;i  )   for(int j=0;j<15;j  )    putimage(i*25,j*25,picture[chessBord[i][j]]);  return 0; } 

先写主函数

我们已经有了加载图片函数和打印棋盘函数。然后我们可以设置棋盘的基本信息并打印它们

棋盘设置与打印:
 initgraph(375,375);  ///设置屏幕大小并显示  setcaption("五子棋"); //设置程序标题  loadPicture();   ///调用加载图片函数 将素材存入picture图片数组中  printfBord();   //打印棋盘 

要知道,下棋的时候是鼠标点的地方,棋子方,双方的黑白子轮流下棋

点击直到点击:
while(mousemsg()) {  msg=getmouse(); } 

检测到鼠标点击,然后用相应的棋子替换点击地点的图片

注意
鼠标点击处是否能落子需要满足以下两个条件:

1.鼠标点击在棋盘范围内;

2.点击处没有下棋;

因此,有必要写一个判断:
int x=msg.x/25,y=msg.y/25;  if(chessBord[x][y]==0)  ///判断当前格子能否落子     {         chessBord[x][y]=player; ////改变当前棋盘格子状态   player=player==1?2:1; //换手     } 

下棋部分做好了,下一步就是判断输赢,每次落后都要判断输赢。

if(judgeWiner(x,y)) {  printBord();  if(player==1) outtextxy(150,150,"白方获胜");  else outtextxy(150,150,"黑方获胜");  getch();   return 0; } 
为了不让胜利信息显示直接消失,可以添加一个getch()在后面,效果相当于点击任何键后退出
下一步判断输赢函数:
int judgeWiner(int x,int y) {  int nx,ny;  int lr=1,ud=1,ld=1,lu=1;  nx=x,ny=y-1; //左右方向  while(chessBord[nx][ny]==chessBord[x][y]) lr  ,ny--;  nx=x,ny=y 1;  while(chessBord[nx][ny]==chessBord[x][y]) lr  ,ny  ;  nx=x-1,ny=y; //上下方向  while(chessBord[nx][ny]==chessBord[x][y]) ud  ,nx--;  nx=x 1,ny=y;   while(chessBord[nx][ny]==chessBord[x][y]) ud  ,nx  ;  nx=x-1,ny=y-1; //左上右下  while(chessBord[nx][ny]==chessBord[x][y]) ld  ,nx--,ny--;  nx=x 1,ny=y 1;   while(chessBord[nx][ny]==chessBord[x][y]) ld  ,nx  ,ny  ;  nx=x 1,ny=y-1; 左下到右上  while(chessBord[nx][ny]==chessBord[x][y]) lu  ,nx  ,ny--;  nx=x-1,ny=y 1;  while(chessBord[nx][ny]==chessBord[x][y]) lu  ,nx--,ny  ;  if(lr>=5||ud>=5||ld>=5||lu>=5) return 1;  else return 0;  } 

其实并没有看起来那么复杂,类似于广度优先搜索。

这里的大部分程序都完成了,剩下的就是组合起来,然后进行小的优化。


完整代码:

#include<graphics.h>
#include<cstdio>
int chessBord[15][15];		//棋盘
int player=2;				//当前玩家
PIMAGE picture[3];			//棋盘图片
mouse_msg msg;				//鼠标
int loadPicture();			//加载图片函数
int printBord();			//打印棋盘
int judgeWiner(int x,int y);	//判断输赢函数
int main()
{
	initgraph(375,375);		//设置幕布大小并显示
	setcaption("五子棋");	//设置程序标题
	loadPicture();			//调用加载图片函数 将素材存入picture图片数组中
	for(;is_run();delay_fps(180))
	{
		while(mousemsg())
		{
			msg=getmouse();
		}
		if(msg.is_down())
		{
			int x=msg.x/25,y=msg.y/25;
			if(chessBord[x][y]==0)		//判断当前格子是否可以落子
			{
				chessBord[x][y]=player;	//改变当前棋盘格子状态
				if(judgeWiner(x,y))
				{
					printBord();
					if(player==1)	outtextxy(150,150,"白方获胜");
					else	outtextxy(150,150,"黑方获胜");
					getch();
					return 0;
				}
				player=player==1?2:1;	//换手
			}
		}
		printBord();					//更新棋盘
	}
	return 0;
}
int loadPicture()
{
	char path[100];			//加载图片路径
	for(int i=0;i<3;i++)
	{
		picture[i]=newimage();	//向计算机申请一个图片空间
		sprintf(path,"image/%d.jpg",i);//拼接图片相对路径
		getimage(picture[i],path);//将系统中的图片数据存入picture
	}
	return 0;
}
int printBord()
{
	for(int i=0;i<15;i++)
		for(int j=0;j<15;j++)
			putimage(i*25,j*25,picture[chessBord[i][j]]);
	return 0;
}
int judgeWiner(int x,int y)
{
	int nx,ny;
	int lr=1,ud=1,ld=1,lu=1;
	nx=x,ny=y-1;	//左右方向
	while(chessBord[nx][ny]==chessBord[x][y])	lr++,ny--;
	nx=x,ny=y+1;
	while(chessBord[nx][ny]==chessBord[x][y])	lr++,ny++;
	nx=x-1,ny=y;	//上下方向
	while(chessBord[nx][ny]==chessBord[x][y])	ud++,nx--;
	nx=x+1,ny=y; 
	while(chessBord[nx][ny]==chessBord[x][y])	ud++,nx++;
	nx=x-1,ny=y-1;	//左上到右下
	while(chessBord[nx][ny]==chessBord[x][y])	ld++,nx--,ny--;
	nx=x+1,ny=y+1; 
	while(chessBord[nx][ny]==chessBord[x][y])	ld++,nx++,ny++;
	nx=x+1,ny=y-1;	//左下到右上
	while(chessBord[nx][ny]==chessBord[x][y])	lu++,nx++,ny--;
	nx=x-1,ny=y+1;
	while(chessBord[nx][ny]==chessBord[x][y])	lu++,nx--,ny++;
	if(lr>=5||ud>=5||ld>=5||lu>=5)	return 1;
	else	return 0; 
}

八十多行就解决了,并不难,有一定基础就能做


免费的图片及实例下载链接

免费的图片及实例下载链接:基于EGE的c++五子棋人对人小游戏-其他文档类资源-CSDN文库

感谢支持!

2022.4.30
14:11

标签: ege红外传感器p60074

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司