這篇文章主要介紹“C++怎么實現推箱子小游戲”,在日常操作中,相信很多人在C++怎么實現推箱子小游戲問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++怎么實現推箱子小游戲”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
本文實例為大家分享了C++實現推箱子小項目的具體代碼,供大家參考,具體內容如下

代碼:
#include <bits/stdc++.h>
#include<conio.h>
using namespace std;
const int MAXN =10;
class GM
{ int x=1,y=1; //x縱坐標 ,y橫坐標
int count =0;
public:
char map[MAXN][MAXN];
GM();
int RUN();
int check(int x1,int y1,int t,int C);//檢查
void Display(); //打印
void Show();
void Start();
};
void GM::Start()
{
while(1)
{
Show();
int h;
cin>>h;
if(h==521)
{
Display();
while(1) {
int h=RUN();
if(h==-1){system("cls");
cout<<"I LOVE YOU!";
system("pause");
exit(0);
}
else if(h==4)
{ system("cls");
GM k;
k.Start();
}
else Display();
}
}
else
{system("cls");
cout<<"輸錯了再試一次(521)";
}
}
}
void GM::Show()
{
cout<<"__________________________"<<endl;
cout<<"| WXK&&WSN(共1關) |"<<endl;
cout<<"| 游戲規則: |"<<endl;
cout<<"| &:人 O:箱子 |"<<endl;
cout<<"| H:墻 *:目的地 |"<<endl;
cout<<"| 操作說明 Q:退出 |"<<endl;
cout<<"| W:上 S:下 |"<<endl;
cout<<"| D:右 A:左 |"<<endl;
cout<<"| 開始輸入:521 |"<<endl;
cout<<"|_________________________|"<<endl;
}
void GM:: Display() //打印
{ system("cls");
cout<<"Count:"<<count<<"/ 2"<<endl;
for(int i=0;i<10;i++)
{
for(int j=0; j<10;j++)
if(map[i][j]!='0')
cout<<map[i][j];
else cout<<" ";
cout<<endl;
}
}
GM::GM() //初始化地圖
{
fill(map[0],map[0]+MAXN*MAXN,'0');
for(int i=0;i<MAXN;i++)
{
map[0][i]='H';
map[i][0]='H';
map[MAXN-1][i]='H';
map[i][MAXN-1]='H';
}
map[2][2]='O';
map[6][2]='O';
map[1][1]='P';
map[1][3]='H';
map[2][3]='H';
map[3][3]='H';
map[2][5]='H';
map[6][4]='H';
map[5][4]='H';
map[7][4]='H';
map[3][5]='H';
map[3][6]='H';
map[3][8]='H';
map[4][8]='H';
map[6][5]='*';
}
int GM::check(int x1,int y1,int t,int L)
{ int M=0,N=0;
if(t==1)
{ M=L;
N=0;
}
else if(t==-1)
{
M=0;
N=L;
}
if(map[x1][y1]=='H'||(map[x1][y1]=='O'&&(map[x1-M][y1+N]=='H'||map[x1-M][y1+N]=='O')))//有箱子但是會被主檔和直接是墻
return -1;
else if(map[x1][y1]=='O'&&map[x1-M][y1+N]!='H')//當有箱子且不會被墻檔
{ if(map[x1-M][y1+N]=='*')
return 3;
return 1;
}
else if(map[x1][y1]!='H'&&map[x1][y1]!='*')return 2;
}
int GM::RUN()
{
char ch =getch();
int t =1,L=1;//t判斷上下還是左右,L判斷加減
if(ch =='a'||ch=='d'||ch=='A'||ch=='D')t=-1;
if(ch=='a'||ch=='s'||ch=='A'||ch=='S')L=-1;
if(ch=='Q'||ch=='q')return 4;
if(t==1&&check(x-L,y,t,L)==1)//往上或下有箱子了話
{
map[x][y]='0';
x-=L;
map[x][y]='P';
map[x-L][y]='O';
}
else if(t==1&&check(x-L,y,t,L)==2)//往上或者往下能走了話
{
map[x][y]='0';
x-=L;
map[x][y]='P';
}
else if(t==1&&check(x-L,y,t,L)==3)
{ map[x][y]='0';
x-=L;
map[x][y]='P';
if(count==2)
return -1;
}
else if(t==-1&&check(x,y+L,t,L)==1)//往左或右有箱子了話
{
map[x][y]='0';
y+=L;
map[x][y]='P';
map[x][y+L]='O';
}
else if(t==-1&&check(x,y+L,t,L)==2)//往左或者往右能走了話
{
map[x][y]='0';
y+=L;
map[x][y]='P';
}
else if(t==-1&&check(x,y+L,t,L)==3)
{ map[x][y]='0';
y+=L;
map[x][y]='P';
count++;
if(count==2)
return -1;
}
}
int main() {
system("color E4");
GM k;
k.Start();
}到此,關于“C++怎么實現推箱子小游戲”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。