C語(yǔ)言 完整游戲項(xiàng)目推箱子詳細(xì)代碼

話不多說(shuō)
我們今天就來(lái)創(chuàng)造出屬于我們自己的《推箱子》,GOGOGO?。?!
直接開(kāi)始吧
首先是我們用二維數(shù)組特定的數(shù)字描繪出這個(gè)地圖
int cas = 0;
int map[3][8][8] =
{
1,1,1,1,1,1,1,1,
1,3,4,0,0,4,3,1,
1,0,1,3,0,1,0,1,
1,0,1,4,0,1,0,1,
1,0,0,5,0,0,0,1,
1,0,1,0,0,1,0,1,
1,3,4,0,0,4,3,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,3,4,0,0,4,3,1,
1,0,1,3,0,1,0,1,
1,0,1,4,0,1,0,1,
1,3,4,5,0,0,0,1,
1,0,1,0,0,1,0,1,
1,3,4,0,0,4,3,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,3,4,0,0,4,3,1,
1,0,1,3,0,1,0,1,
1,0,1,4,0,1,0,1,
1,3,4,5,0,4,3,1,
1,0,1,0,0,1,0,1,
1,3,4,0,0,4,3,1,
1,1,1,1,1,1,1,1
};
然后來(lái)繪制我們的推箱子地圖
void drawGraph()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
//算貼圖的坐標(biāo)
int x = 50 * j;
int y = 50 * i;
switch (map[cas][i][j])
{
case 0:
//一個(gè)漢字符號(hào)占用兩個(gè)位置
//printf(" ");
putimage(x, y, img + 0);
break;
case 1:
putimage(x, y, img + 1);
//printf("■");
break;
case 3:
putimage(x, y, img + 2);
//printf("☆");
break;
case 4:
putimage(x, y, img + 3);
//printf("★");
break;
case 5:
case 8:
putimage(x, y, img + 4);
//printf("人");
break;
case 7:
putimage(x, y, img + 5);
//printf("●");
break;
}
}
//printf("\n");
}
}
之后就是我們的游戲函數(shù),怎樣去用什么按鍵去控制我們的角色
void keyDown()
{
int userKey = _getch(); //不可見(jiàn)輸入
//定位:找到人的位置
int i = 0;
int j = 0;
for (i = 1; i < 8; i++)
{
for (j = 1; j < 8; j++)
{
if (map[cas][i][j] == 5 || map[cas][i][j] == 8)
{
goto NEXT;
}
}
}
NEXT:
//我們這個(gè)游戲用什么按鍵去玩
switch (userKey)
{
case 'W':
case 'w':
case 72:
if (map[cas][i - 1][j] == 0 || map[cas][i - 1][j] == 3)
{
map[cas][i][j] -= 5;
map[cas][i - 1][j] += 5;
}
if (map[cas][i - 1][j] == 4 || map[cas][i - 1][j] == 7)
{
if (map[cas][i - 2][j] == 0 || map[cas][i - 2][j] == 3)
{
map[cas][i][j] -= 5;
map[cas][i - 1][j] += 1;
map[cas][i - 2][j] += 4;
}
}
break;
case 's':
case 'S':
case 80:
if (map[cas][i + 1][j] == 0 || map[cas][i + 1][j] == 3)
{
map[cas][i][j] -= 5;
map[cas][i + 1][j] += 5;
}
if (map[cas][i + 1][j] == 4 || map[cas][i + 1][j] == 7)
{
if (map[cas][i + 2][j] == 0 || map[cas][i +2][j] == 3)
{
map[cas][i][j] -= 5;
map[cas][i + 1][j] += 1;
map[cas][i + 2][j] += 4;
}
}
break;
case 'a':
case 'A':
case 75:
if (map[cas][i][j - 1] == 0 || map[cas][i][j - 1] == 3)
{
//a+=1 a=a+1 復(fù)合賦值運(yùn)算符
map[cas][i][j] -= 5;
map[cas][i][j - 1] += 5;
}
if (map[cas][i][j - 1] == 4 || map[cas][i][j - 1] == 7)
{
if (map[cas][i][j - 2] == 0 || map[cas][i][j - 2] == 3)
{
map[cas][i][j] -= 5;
map[cas][i][j - 1] += 1;
map[cas][i][j - 2] += 4;
}
}
break;
case 'd':
case 'D':
case 77:
if (map[cas][i][j + 1] == 0 || map[cas][i][j + 1] == 3)
{
map[cas][i][j] -= 5;
map[cas][i][j + 1] += 5;
}
if (map[cas][i][j + 1] == 4 || map[cas][i][j + 1] == 7)
{
if (map[cas][i][j + 2] == 0 || map[cas][i][j + 2] == 3)
{
map[cas][i][j] -= 5;
map[cas][i][j + 1] += 1;
map[cas][i][j + 2] += 4;
}
}
break;
}
}
再然后就是我們?nèi)绾稳ヅ袛嘤螒虻慕Y(jié)果
//勝負(fù)的判斷:
int gameOver()
{
//地圖上沒(méi)有箱子就可以結(jié)束
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (map[cas][i][j] == 4)
{
return 0;
}
}
}
return 1;
}
最后運(yùn)行我們的主函數(shù)就行啦
int main()
{
loadResource();
mciSendString("open 1.mp3", 0, 0, 0);
mciSendString("play 1.mp3 repeat", 0, 0, 0);
initgraph(50 * 8, 50 * 8);
while (1)
{
drawGraph();
if (gameOver())
{
cas++; //變換關(guān)卡
if(cas==3)
break;
}
keyDown();
//system("cls");
}
closegraph();
//printf("GameOver!\n");
return 0;
}
其實(shí)代碼并不是很多,當(dāng)然啦,如果同學(xué)們想更加完善,可以增加關(guān)卡設(shè)定,再優(yōu)化一下我們的開(kāi)始界面以及游戲界面也是可以的,大家快去嘗試吧!??! 希望看完了的同學(xué)可以獲得自己想要的知識(shí),也感謝大家的耐心觀看,在這里想得到大家一波關(guān)注,后續(xù)UP主還會(huì)發(fā)布更多的項(xiàng)目源碼以及學(xué)習(xí)資料,有什么問(wèn)題可以回帖留言,我盡量回答。希望和大家一起學(xué)習(xí)進(jìn)步?。?!

到此這篇關(guān)于C語(yǔ)言 完整游戲項(xiàng)目推箱子詳細(xì)代碼的文章就介紹到這了,更多相關(guān)C語(yǔ)言 推箱子內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++淺析鄰接表拓?fù)渑判蛩惴ǖ膶?shí)現(xiàn)
這篇文章主要介紹了C/C++對(duì)于鄰接表拓?fù)渑判蛩惴ǖ膶?shí)現(xiàn),鄰接表是圖的一種鏈?zhǔn)酱鎯?chǔ)方法,其數(shù)據(jù)結(jié)構(gòu)包括兩部分:節(jié)點(diǎn)和鄰接點(diǎn)2022-07-07
c++中深淺拷貝以及寫(xiě)時(shí)拷貝的實(shí)現(xiàn)示例代碼
這篇文章主要給大家介紹了關(guān)于c++中深淺拷貝以及寫(xiě)時(shí)拷貝實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的推箱子小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的推箱子小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Qt creator中項(xiàng)目的構(gòu)建配置和運(yùn)行設(shè)置的步驟
使用 Qt Creator 集成開(kāi)發(fā)環(huán)境構(gòu)建和運(yùn)行程序是一件非常簡(jiǎn)單的事情,一個(gè)按鈕或者一個(gè)快捷鍵搞定全部,本文主要介紹了Qt creator中項(xiàng)目的構(gòu)建配置和運(yùn)行設(shè)置的步驟,感興趣的小伙伴們可以參考一下2021-11-11
C++命名空間using?namespace?std是什么意思
namespace中文意思是命名空間或者叫名字空間,傳統(tǒng)的C++只有一個(gè)全局的namespace,下面這篇文章主要給大家介紹了關(guān)于C++命名空間using?namespace?std是什么意思的相關(guān)資料,需要的朋友可以參考下2023-01-01
詳解C語(yǔ)言用malloc函數(shù)申請(qǐng)二維動(dòng)態(tài)數(shù)組的實(shí)例
這篇文章主要介紹了詳解C語(yǔ)言用malloc函數(shù)申請(qǐng)二維動(dòng)態(tài)數(shù)組的實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10
C++二叉搜索樹(shù)模擬實(shí)現(xiàn)示例
本文主要介紹了C++二叉搜索樹(shù)模擬實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單學(xué)生成績(jī)管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

