C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C语言斗兽棋

利用C语言实现经典游戏斗兽棋

作者:考拉爱睡觉鸭~

《斗兽棋》是一款棋类游戏,整个游戏画面是分为两块区域,中间有河流分割两块区域,有桥梁可以让彼此的动物过河,要取得胜利,必须占领那一边动物的巢穴获胜利。本文将用C语言实现这一游戏,需要的可以参考一下

效果图

核心代码

#include<stdio.h>
#include<easyx.h>
#include<stdlib.h>
#include<time.h>
 
#define IMAGE_NUM_ANIMAL 8	//动物图片数量
/* 动物类型枚举 从弱到强*/
enum AnimalType
{
	AT_None,		//没有
	AT_Mouse,		//老鼠
	AT_Cat,			//猫
	AT_Dog,			//狗
	AT_Wolf,		//狼
	AT_Leopard,		//豹子
	AT_Tiger,		//老虎
	AT_Lion,		//狮子
	AT_Elephant,	//大象
	AT_Max
};
enum BothType
{
	BT_Red,
	BT_Blue
};
 
IMAGE img_animals[17];
IMAGE imgs[2];			//背景 前面间距28 水平间距42 上面间距65 垂直间距43
void loadResource()
{
	loadimage(img_animals + 0, "./res/Road.png");
	loadimage(img_animals + 1, "./res/BMouse.png");
	loadimage(img_animals + 2, "./res/BCat.png");
	loadimage(img_animals + 3, "./res/BDog.png");
	loadimage(img_animals + 4, "./res/BWolf.png");
	loadimage(img_animals + 5, "./res/BLeopard.png");
	loadimage(img_animals + 6, "./res/BTiger.png");
	loadimage(img_animals + 7, "./res/BLion.png");
	loadimage(img_animals + 8, "./res/BElephant.png");
 
	loadimage(img_animals + 9, "./res/RMouse.png");
	loadimage(img_animals + 10, "./res/RCat.png");
	loadimage(img_animals + 11, "./res/RDog.png");
	loadimage(img_animals + 12, "./res/RWolf.png");
	loadimage(img_animals + 13, "./res/RLeopard.png");
	loadimage(img_animals + 14, "./res/RTiger.png");
	loadimage(img_animals + 15, "./res/RLion.png");
	loadimage(img_animals + 16, "./res/RElephant.png");
 
	loadimage(imgs + 0, "./res/BackGround.png");
	loadimage(imgs + 1, "./res/Card.png");
}
IMAGE* getAnimalImage(int type)
{
	//如果格子已经加密了
	if (type > 20)
	{
		return imgs + 1;
	}
	if (type >= 0 && type < AT_Max)
	{
		return img_animals + type;
	}
	else if (type > 8 && type < 2 * AT_Max - 1)
	{
		return img_animals + type;
	}
	return NULL;
}
 
 
struct Card
{
	int row;
	int col;
	int animalType;		//动物类型
	//int bothType;		//双方类型 红 蓝
};
#define CardW()	    img_animals[0].getwidth()
#define CardH()	    img_animals[0].getheight()
#define CardX(col) (28 + col * (CardW() + 42))	//28 左边的间隔  42 横向中间的间隔
#define CardY(row) (65 + row * (CardH() + 43))  //65 上边的间隔  43 横向中间的间隔
 
void card_init(Card* card, int row, int col, int aniType)
{
	card->row = row;
	card->col = col;
	card->animalType = aniType;
}
 
void card_draw(Card* card)
{
	putimage(CardX(card->col), CardY(card->row), getAnimalImage(card->animalType));
}
//获取
int card_type(Card* card)
{
	if (card->animalType <= 8)
	{
		return BT_Blue;
	}
	else if (card->animalType > 8 && card->animalType < AT_Max * 2 - 1)
	{
		return BT_Red;
	}
}
 
//两张牌类型是否相同
bool card_sameType(Card* card1, Card* card2)
{
	return card_type(card1) == card_type(card2);
}
//消费者是否能吃食物
bool card_eat(Card* food, Card* consumer)
{
	//先把大于8的,也转成小于八的,方便比较
	int ftype = food->animalType;
	int ctype = consumer->animalType;
	ftype = ftype > 8 ? food->animalType - 8 : ftype;
	ctype = ctype > 8 ? consumer->animalType - 8 : ctype;
 
	if (!card_sameType(food, consumer) &&			//不是同一方
		ftype <= ctype ||	/*大吃小*/ (ftype == AT_Elephant && ctype == AT_Mouse)//老鼠吃大象
		)
	{
		return true;
	}
	return false;
}
 
Card cards[4][4];
int curBothType = BT_Blue;	//当前行棋方
enum State
{
	Begin,
	End
};
int press = Begin;//第一次点击还是第二次点击
struct Index
{
	int row;
	int col;
}beg = { -1,-1 }, end = { -1,-1 };
 
void init()
{
	srand(time(NULL));
 
	for (int i = 0; i < 4; i++)
	{
		for (int k = 0; k < 4; k++)
		{
			card_init(cards[i] + k, i, k, i * 4 + k + 1);
		}
	}
	//打乱数组
	for (int i = 0; i < 4; i++)
	{
		for (int k = 0; k < 4; k++)
		{
			int r = rand() % 4;
			int c = rand() % 4;
			Card t = cards[r][c];
			cards[r][c] = cards[i][k];
			cards[i][k] = t;
 
		}
 
	}
	//加密数组
	for (int i = 0; i < 4; i++)
	{
		for (int k = 0; k < 4; k++)
		{
			cards[i][k].animalType += 20;
			printf("%d ", cards[i][k].animalType);
		}
		putchar('\n');
	}
}
 
void draw()
{
	//绘制背景
	putimage(0, 0, imgs + 0);
 
	for (int i = 0; i < 4; i++)
	{
		for (int k = 0; k < 4; k++)
		{
			IMAGE* t = getAnimalImage(cards[i][k].animalType);
			putimage(CardX(k), CardY(i), t);
		}
	}
	if (beg.row != -1)
	{
		setlinestyle(PS_SOLID, 3);
		setlinecolor(RED);
		int x = CardX(beg.col);
		int y = CardY(beg.row);
		int w = CardW();
		int h = CardH();
		rectangle(x, y, x + w, y + h);
	}
}
 
//65 264 28 65 90 108
bool isIn(int x, int y, int left, int top, int w, int h)
{
	if (x > left && x < left + w && y > top && y < top + h)
	{
		return true;
	}
	return false;
}
 
void onMouseLbuttonDown(ExMessage* msg)
{
	for (int i = 0; i < 4; i++)
	{
		for (int k = 0; k < 4; k++)
		{
			//判断是否在卡牌上点击
			if (isIn(msg->x, msg->y, CardX(k), CardY(i), CardW(), CardH()))
			{
				//如果没有翻开,则翻开
				if (cards[i][k].animalType > 20)
				{
					cards[i][k].animalType -= 20;
				}
				//如果翻开了,可以移动或者吃
				else
				{
					//如果点击的是同一方的牌,直接改变begin
					//if (card_type(&cards[i][k]) == curBothType)
					//{
					//	press = Begin;
					//}
					//点击不同的牌在move函数里进行判断
 
					//移动或者吃,需要知道两次点击的牌
					if (press == Begin)
					{
						beg = { i,k };
						press = End;
						printf("begin(%d %d) %d\n", i, k, curBothType);
					}
					else if (press == End)
					{
						end = { i ,k };
						press = Begin;
						printf("end(%d %d) %d\n", i, k, curBothType);
					}
 
				}
			}
		}
	}
}
 
void move()
{
	//beg end不为-1 并且不是同一个位置
	if ((beg.row != -1 && end.row != -1) && !(beg.row == end.row && beg.col == end.col))
	{
		//在同一列或者同一行
		if ((abs(beg.row - end.row) == 1 && beg.col == end.col) || (abs(beg.col - end.col) == 1 && beg.row == end.row))
		{
			//吃
			bool ok = card_eat(&cards[end.row][end.col], &cards[beg.row][beg.col]);
			//移动
			if (ok || cards[end.row][end.col].animalType == AT_None)
			{
				cards[end.row][end.col] = cards[beg.row][beg.col];
				cards[beg.row][beg.col].animalType = AT_None;
				beg = end = { -1,-1 };
				//切换棋手
				curBothType = (curBothType + 1) % 2;
			}
		}
	}
}
 
int main()
{
	initgraph(540, 677/*, EW_SHOWCONSOLE*/);
	loadResource();
 
	init();
	while (true)
	{
		BeginBatchDraw();
		draw();
		EndBatchDraw();
 
		ExMessage msg;
		while (peekmessage(&msg, EM_MOUSE))
		{
			if (msg.message == WM_LBUTTONDOWN)
			{
				onMouseLbuttonDown(&msg);
			}
		}
 
		move();
	}
 
	getchar();
	return 0;
}

以上就是利用C语言实现经典游戏斗兽棋的详细内容,更多关于C语言斗兽棋的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文