控制台推箱子小游戏

控制台推箱子小游戏

 
 
 
 
 
 // 游戏菜单.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include<iostream>
#include"windows.h"
#define KEY_DOWN(vk_code) (GetAsyncKeyState(vk_code)&0x8000?1:0) using namespace std; sDATA g_Player; sDATA g_Box[]; int _tmain(int argc, _TCHAR* argv[])
{
int nKeyState = ;
bool bBox = false;
int nBoxWin = ; //初始化人物
g_Player.X = ;
g_Player.Y = ;
g_Player.XOD = ;
g_Player.YOD = ;
//初始化箱子
for (int m = ; m < ; m++)
{
g_Box[m].X = m + ;
g_Box[m].Y = m + ;
g_Box[m].XOD = m + ;
g_Box[m].YOD = m + ;
} //键盘输入
while (true)
{
system("CLS"); if (nKeyState < )
{
if (KEY_DOWN(VK_DOWN))
{
nKeyState = abs(++nKeyState % );
}
else if (KEY_DOWN(VK_UP))
{
--nKeyState;
if (nKeyState < )
{
nKeyState = ;
}
}
if (KEY_DOWN(VK_RETURN))
{
if (nKeyState == n_Menu_PlayGame)
{
nKeyState = n_GO_PlayGame;
}
}
} //备份箱子坐标
for (int m = ; m < ; m++)
{
g_Box[m].XOD = g_Box[m].X;
g_Box[m].YOD = g_Box[m].Y;
}
//推箱子
for (int m = ; m < ; m++)
{
if (g_Player.X == g_Box[m].X && g_Player.Y == g_Box[m].Y)
{
g_Box[m].X += (g_Player.X - g_Player.XOD);
g_Box[m].Y += (g_Player.Y - g_Player.YOD);
}
}
//判断箱子是否撞墙
for (int m = ; m < ; m++)
{
if (nGameFrame[g_Box[m].X][g_Box[m].Y] == )
{
g_Box[m].X = g_Box[m].XOD;
g_Box[m].Y = g_Box[m].YOD;
g_Player.X = g_Player.XOD;
g_Player.Y = g_Player.YOD;
}
}
//判断箱子是否撞箱子
for (int m = ; m < ; m++)
{
for (int j = ; j < ; j++)
{
if (m != j&&g_Box[m].X == g_Box[j].X&&g_Box[m].Y == g_Box[j].Y)
{
g_Box[m].X = g_Box[m].XOD;
g_Box[m].Y = g_Box[m].YOD;
g_Player.X = g_Player.XOD;
g_Player.Y = g_Player.YOD;
}
}
} //判断胜利
for (; nBoxWin < ; nBoxWin++)
{
if (nGameFrame[g_Box[nBoxWin].X][g_Box[nBoxWin].Y] != )
{
break;
}
}
if (nBoxWin >= )
{
nKeyState = n_Go_Win;
} // 渲染
if (nKeyState < )
{
cout << "■■■■■■■■■■■■" << endl;
cout << "■■■■■■■■■■■■" << endl;
if (nKeyState == n_Menu_PlayGame)
{
cout << "■■ >-开始游戏 ■■" << endl;
cout << "■■ 游戏设置 ■■" << endl;
cout << "■■ 结束游戏 ■■" << endl; }
else if (nKeyState == n_Menu_GameSet)
{
cout << "■■ 开始游戏 ■■" << endl;
cout << "■■ >-游戏设置 ■■" << endl;
cout << "■■ 结束游戏 ■■" << endl;
}
else if (nKeyState == n_Menu_EndGame)
{
cout << "■■ 开始游戏 ■■" << endl;
cout << "■■ 游戏设置 ■■" << endl;
cout << "■■ >-结束游戏 ■■" << endl;
}
cout << "■■■■■■■■■■■■" << endl;
cout << "■■■■■■■■■■■■" << endl;
}
else if (nKeyState == n_GO_PlayGame)
{
for (int i = ; i < ; i++)
{
for (int a = ; a < ; a++)
{
for (int m = ; m < ; m++)
{
if (i==g_Box[m].X&&a==g_Box[m].Y)
{
bBox = true;
break;
}
else
{
bBox = false;
} }
if (nGameFrame[i][a] == )
{
cout << "■";
}
else if (i==g_Player.X&&a==g_Player.Y)
{
cout << "♀";
}
else if (bBox == true)
{
cout << "¤";
}
else if (nGameFrame[i][a] == )
{
cout << "×";
}
else
{
cout << " ";
}
}
cout << endl;
}
}
else if (nKeyState == n_Go_Win)
{
cout << "胜利!" << endl;
} //人物移动
g_Player.XOD = g_Player.X;
g_Player.YOD = g_Player.Y;
if (KEY_DOWN(VK_UP))
{
g_Player.X--;
}
else if (KEY_DOWN(VK_DOWN))
{
g_Player.X++;
}
else if (KEY_DOWN(VK_LEFT))
{
g_Player.Y--;
}
else if (KEY_DOWN(VK_RIGHT))
{
g_Player.Y++;
} if (nGameFrame[g_Player.X][g_Player.Y] == )
{
g_Player.X = g_Player.XOD;
g_Player.Y = g_Player.YOD;
} } return ;
}

控制台推箱子小游戏

05-11 11:37