推箱子(C語言版)-創(chuàng)新互聯(lián)

推箱子這個(gè)小游戲,當(dāng)初自己也寫了很久

成都創(chuàng)新互聯(lián)公司長期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為名山企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,名山網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

這個(gè)游戲:就是人將所有箱子推到目的地,便算是成功

主要思路:

1.用二維數(shù)組和枚舉繪制地圖(如果要增加關(guān)卡,就用三維數(shù)組(里面存放的元素是多個(gè)二維數(shù)組),這里我寫了三關(guān))

2.用數(shù)組的位置來寫人和箱子的移動,再配合switch語句,就可以實(shí)現(xiàn)用鍵盤操控人的移動。

3.游戲結(jié)束的標(biāo)志:空地上沒有箱子

源碼在這,如有需要,請自行領(lǐng)取。

#include   // 推箱子的幾個(gè)元素;
#include //  0 空地   1 墻  2 目的地   3 箱子 4 玩 5 箱子在目的地  6 玩家在目的地 
#include//_getch()函數(shù)的頭文件   //用_getch函數(shù),如果用getchar(),每次移動要回車鍵  // 上 72  下 80  左75   右77

enum Mine
{
	space,
	wall,
	destination,
	box,
	player,
};
int level = 0;
int map[3][10][10] =
{
	{
		{0, 0, 0, 1, 1, 1 ,1 ,1, 0, 0},
		{0, 0, 0, 1, 0, 0 ,2 ,1, 0, 0},
		{0, 0, 0, 1, 0, 3 ,0 ,1, 0, 0},
		{0, 1, 1, 1, 0, 0 ,0 ,1, 1, 1},
		{0, 1, 0, 2, 3 ,4 ,3, 0, 2, 1},
		{0, 1, 1, 1, 0, 3 ,0 ,1, 1, 1},
		{0, 0, 0, 1, 0, 2 ,0 ,1, 0, 0},
		{0, 0, 0, 1, 0, 0 ,0 ,1, 0, 0},
		{0, 0, 0, 1, 1, 1 ,1 ,1, 0, 0},
		{0, 0, 0, 0, 0, 0 ,0 ,0, 0, 0},
	},

	{
		{1, 1, 1, 1, 1, 1 ,1 ,1, 1, 1},
		{1, 2, 0, 0, 0, 0 ,0 ,0, 0, 1},
		{1, 0, 0, 0, 0, 0 ,3 ,0, 1, 0},
		{1, 0, 0, 3, 0, 0 ,0 ,0, 0, 1},
		{1, 0, 0, 3, 0 ,4 ,0, 0, 0, 1},
		{1, 0, 0, 0, 0, 0 ,1 ,0, 0, 1},
		{1, 2, 1, 1, 0, 0 ,1 ,0, 2, 1},
		{1, 1, 1, 0, 1, 0 ,1 ,0, 1, 0},
		{0, 0, 0, 0, 1, 0 ,1 ,0, 1, 0},
		{0, 0, 0, 0, 1, 1 ,1 ,1, 0, 0},
	},

	{
		{0, 0, 0, 1, 1, 1 ,1 ,0, 0, 0},
		{0, 0, 1, 0, 0, 0 ,0 ,1, 0, 0},
		{0, 1, 0, 3, 0, 3 ,0 ,0, 1, 0},
		{1, 2, 0, 0, 0, 0 ,1 ,0, 0, 1},
		{1, 1, 1, 1, 2 ,0 ,1, 0, 0, 1},
		{1, 0, 0, 1, 1, 1 ,1 ,0, 0, 1},
		{1, 2, 0, 0, 0, 4 ,0 ,0, 0, 1},
		{0, 1, 0, 0, 3, 0 ,3 ,0, 1, 0},
		{0, 0, 1, 0, 0, 0 ,2 ,1, 0, 0},
		{0, 0, 0, 1, 1, 1 ,1 ,0, 0, 0},
	}

};

void drawmap()
{
	for (int i = 0; i< 10; ++i)
	{
		for (int j = 0; j< 10; ++j)
		{
			switch (map[level][i][j])
			{
			case space:
				printf("  ");
				break;
			case wall:
				printf("▓ ");
				break;
			case destination:
				printf("☆");
				break;
			case box:
				printf("□");
				break;
			case player:
				printf("♀");
				break;
			case box + destination:
				printf("★");
				break;
			case player + destination:
				printf("♂");
				break;
			}
		}
		printf("\n");
	}
}
void playgame()
{
	int i = 0, j = 0;
	for (i = 0; i< 10; i++)
	{
		for (j = 0; j< 10; j++)
		{
			if (map[level][i][j] == player || map[level][i][j] == player + destination)
			{
				goto end;       //break跳出一層循環(huán),用  goto end; end:; 跳出所有的循環(huán)
			}
		}
	}
end:;
	int  key = _getch();
	switch (key)
	{
		case 'w':      // 上
		case 'W':
		case  72:
			if (map[level][i - 1][j] ==  space || map[level][i - 1][j] ==  destination)
			{
				map[level][i - 1][j] += player;
				map[level][i][j] -= player;
			}
			else
			{
				if (map[level][i - 1][j] == box || map[level][i - 1][j] == box + destination)
				{
					if (map[level][i - 2][j] == space || map[level][i - 2][j] == destination)
					{
						map[level][i - 2][j] += box;
						map[level][i - 1][j] = map[level][i - 1][j] - box + player;
						map[level][i - 1][j] -=  player;
					}
				}
			}
			break;

		case 's':     //  下
		case 'S':
		case 80:
			if (map[level][i + 1][j] == space || map[level][i + 1][j] == destination)
			{
				map[level][i + 1][j] = map[level][i + 1][j] + player;
				map[level][i][j] = map[level][i][j] - player;
			}
			else
			{
				if (map[level][i + 1][j] == box || map[level][i + 1][j] == box + destination)
				{
					if (map[level][i + 2][j] == space || map[level][i + 2][j] == destination)
					{
						map[level][i + 2][j] = map[level][i + 2][j] + box;
						map[level][i + 1][j] = map[level][i + 1][j] - box + player;
						map[level][i + 1][j] = map[level][i + 1][j] - player;
					}
				}
			}
			break;
		case 'a':     //  左
		case 'A':
		case 75:
			if (map[level][i][j - 1] == space || map[level][i][j - 1] == destination)
			{
				map[level][i][j - 1] += player;
				map[level][i][j] -= player;
			}
			else
			{
				if (map[level][i][j - 1] == box || map[level][i][j - 1] == box + destination)
				{
					if (map[level][i][j - 2] == space || map[level][i][j - 2 ] == destination)
					{
						map[level][i][j - 2] += box;
						map[level][i][j - 1] = map[level][i][j - 1] - box + player;
						map[level][i][j - 1] -= player;
					}
				}
			}			
			break;

		case 'd':      // 右
		case 'D':
		case 77:
			if (map[level][i][j + 1] == space || map[level][i][j + 1] == destination)
			{
				map[level][i][j + 1] += player;
				map[level][i][j] -= player;
			}
			else
			{
				if (map[level][i][j + 1] == box || map[level][i][j + 1] == box + destination)
				{
					if (map[level][i][j + 2] == space || map[level][i][j + 2] == destination)
					{
						map[level][i][j + 2] += box;
						map[level][i][j + 1] = map[level][i][j + 1] - box + player;
						map[level][i][j + 1] -= player;
					}
				}
			}
			break;
	}
}
bool deduce_success()
{
	for (int a = 0; a< 10; a++)
	{
		for (int b = 0; b< 10; b++)
		{
			if (map[level][a][b] == box)
			{
				return false;
			}
		}
	}
	return true;
}

int main()
{
	//cols 長  lines寬
	system("mode con cols=25 lines=15");

	while (1)
	{
		system("cls");
		drawmap();

		if (deduce_success())
		{
			level++;
			if (level >2)
			{
				printf("你贏了");
				printf("不愧是地表最強(qiáng)的人!?。。。?!");
				printf("恭喜通關(guān)?。。。。?!");
				break;
			}
		}

		playgame();
	}

	

	getchar();

	return 0;
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

當(dāng)前文章:推箱子(C語言版)-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://bm7419.com/article42/ceoiec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、軟件開發(fā)微信小程序、做網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司