欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用C語(yǔ)言實(shí)現(xiàn)經(jīng)典游戲斗獸棋

 更新時(shí)間:2022年03月18日 09:43:21   作者:考拉愛睡覺鴨~  
《斗獸棋》是一款棋類游戲,整個(gè)游戲畫面是分為兩塊區(qū)域,中間有河流分割兩塊區(qū)域,有橋梁可以讓彼此的動(dòng)物過河,要取得勝利,必須占領(lǐng)那一邊動(dòng)物的巢穴獲勝利。本文將用C語(yǔ)言實(shí)現(xiàn)這一游戲,需要的可以參考一下

效果圖

核心代碼

#include<stdio.h>
#include<easyx.h>
#include<stdlib.h>
#include<time.h>
 
#define IMAGE_NUM_ANIMAL 8	//動(dòng)物圖片數(shù)量
/* 動(dòng)物類型枚舉 從弱到強(qiáng)*/
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)
{
	//如果格子已經(jīng)加密了
	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;		//動(dòng)物類型
	//int bothType;		//雙方類型 紅 藍(lán)
};
#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);
}
//消費(fèi)者是否能吃食物
bool card_eat(Card* food, Card* consumer)
{
	//先把大于8的,也轉(zhuǎn)成小于八的,方便比較
	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;	//當(dāng)前行棋方
enum State
{
	Begin,
	End
};
int press = Begin;//第一次點(diǎn)擊還是第二次點(diǎn)擊
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);
		}
	}
	//打亂數(shù)組
	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;
 
		}
 
	}
	//加密數(shù)組
	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++)
		{
			//判斷是否在卡牌上點(diǎn)擊
			if (isIn(msg->x, msg->y, CardX(k), CardY(i), CardW(), CardH()))
			{
				//如果沒有翻開,則翻開
				if (cards[i][k].animalType > 20)
				{
					cards[i][k].animalType -= 20;
				}
				//如果翻開了,可以移動(dòng)或者吃
				else
				{
					//如果點(diǎn)擊的是同一方的牌,直接改變begin
					//if (card_type(&cards[i][k]) == curBothType)
					//{
					//	press = Begin;
					//}
					//點(diǎn)擊不同的牌在move函數(shù)里進(jìn)行判斷
 
					//移動(dòng)或者吃,需要知道兩次點(diǎn)擊的牌
					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 并且不是同一個(gè)位置
	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]);
			//移動(dòng)
			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語(yǔ)言實(shí)現(xiàn)經(jīng)典游戲斗獸棋的詳細(xì)內(nèi)容,更多關(guān)于C語(yǔ)言斗獸棋的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論