放假期间试图制作了迷宫小游戏,用的软件是vc6,有很多不足之处,请多多指教。
代码:
#include <iostream>
#include <cstdio>
#include <conio.h>
#include <windows.h>
#define _UP 0
#define _DOWN 1
#define _LEFT 2
#define _RIGHT 3
using namespace std;
//利用结构体标出坐标
struct point {
int x, y;
};
enum SYMBOL//枚举函数,方便一一列出
{
Road='0',
Wall,
Player,
Start,
End,
Boom
};
const point PlayerStart = {10, 2};//玩家开始位置
const point PlayerEnd = {2, 20};//玩家结束位置
const int MapLenX = 11, MapLenY = 20;//画面大小
char Map[MapLenX][MapLenY];
const int MoveX[4] = {-1, 1, 0, 0}, MoveY[4] = {0, 0, -1, 1}; //UP, DOWN, LEFT, RIGHT方向
//关闭游戏
void Quit(){
exit(0);
}
//对玩家的控制
class PlayerActor {
public:
point m_Location;
bool m_IfWin;
PlayerActor();
~PlayerActor();
void PlayerMove(int _Direc);
void Refresh(void);
void CheckIfWin(void);
};
PlayerActor::PlayerActor() {
m_IfWin = false;
this->m_Location.x = PlayerStart.x;
this->m_Location.y = PlayerStart.y;
Map[this->m_Location.x][this->m_Location.y-1] = '3';//入口位置
Map[this->m_Location.x][this->m_Location.y] = '2';//玩家位置
Map[PlayerEnd.x][PlayerEnd.y] = '4';//出口位置
PlayerActor::Refresh();
return;
}
PlayerActor::~PlayerActor() {
}
void PlayerActor::PlayerMove(int _Direct) {
if ( Map[this->m_Location.x+MoveX[_Direct]][this->m_Location.y+MoveY[_Direct]] == '0'
||Map[this->m_Location.x+MoveX[_Direct]][this->m_Location.y+MoveY[_Direct]] == '5'
|| Map[this->m_Location.x+MoveX[_Direct]][this->m_Location.y+MoveY[_Direct]] == '4' ) {
// 判断玩家的移动
if( Map[this->m_Location.x+MoveX[_Direct]][this->m_Location.y+MoveY[_Direct]] == '5')
{
MessageBox(NULL, "You Lose!", "失败!", MB_OK);
Quit();
}
Map[this->m_Location.x][this->m_Location.y] = '0';
this->m_Location.x += MoveX[_Direct];
this->m_Location.y += MoveY[_Direct];
Map[this->m_Location.x][this->m_Location.y] = '2';
PlayerActor::Refresh();
PlayerActor::CheckIfWin();
}
return;
}
void PlayerActor::Refresh(void) {
system("cls"); //刷新画面
cout<<"#:陷阱"<<endl;
cout<<"w:上 s:下 a:左 d:右"<<endl;
for (int i=1; i<=MapLenX; i++) {
for (int j=1; j<=MapLenY; j++) {
if (Map[i][j] == '0')
printf(" ");
else if (Map[i][j] == '1')
printf("▇ ");
else if (Map[i][j] == '2')
printf("●");
else if (Map[i][j] == '3')
printf("入");
else if (Map[i][j] == '4')
printf("出 ");
else if (Map[i][j] == '5')
printf("##");
}
printf("\n");
}
return;
}
//判断胜利
void PlayerActor::CheckIfWin(void) {
if (this->m_Location.x == PlayerEnd.x && this->m_Location.y == PlayerEnd.y)
m_IfWin = true;
return;
}
//玩家控制
void PlayerControl(PlayerActor* Player, int _KEY) {
switch (_KEY) {
case 'w' : Player->PlayerMove(0);
break;
case 's' : Player->PlayerMove(1);
break;
case 'a' : Player->PlayerMove(2);
break;
case 'd' : Player->PlayerMove(3);
break;
default:
break;
}
return;
}
void menu(){
//游戏界面
printf("*************************************\n");
printf("* 迷 宫 小 游 戏 *\n");
printf("* *\n");
printf("* 请 选 择 关 卡 *\n");
printf("* *1 *\n");
printf("* *2 *\n");
printf("* *3 *\n");
printf("* *4 *\n");
printf("* *5 *\n");
printf("* *6退出 *\n");
printf("*************************************\n");
}
int main() {
menu();
int level;
while(1){
printf("你选择的关卡是:");
scanf("%d",&level);
fflush(stdin);
printf("\n");
//导入地图
if (level==1){
freopen("map5.txt", "r", stdin);
for (int i=1; i<=MapLenX; i++) {
for (int j=1; j<=MapLenY; j++) {
std::cin >> Map[i][j];
}
}
break;
}else if(level==2){
freopen("map4.txt", "r", stdin);
for (int i=1; i<=MapLenX; i++) {
for (int j=1; j<=MapLenY; j++) {
std::cin >> Map[i][j];
}
}
break;
}else if(level==3){
freopen("map3.txt", "r", stdin);
for (int i=1; i<=MapLenX; i++) {
for (int j=1; j<=MapLenY; j++) {
std::cin >> Map[i][j];
}
}
break;
}else if(level==4){
freopen("map2.txt", "r", stdin);
for (int i=1; i<=MapLenX; i++) {
for (int j=1; j<=MapLenY; j++) {
std::cin >> Map[i][j];
}
}
break;
}else if(level==5){
freopen("map1.txt", "r", stdin);
for (int i=1; i<=MapLenX; i++) {
for (int j=1; j<=MapLenY; j++) {
std::cin >> Map[i][j];
}
}
break;
}else if(level==6){
Quit();
}
}
//创建PLAYERACTOR
PlayerActor* Player = new PlayerActor;
while (!Player->m_IfWin) {
PlayerControl(Player, _getch());
}
system("cls");
MessageBox(NULL, "You Win!", "恭喜!", MB_OK);
delete Player;
return;
}
map1.txt
11111111111111111111 10000000000011010101 10555555555001050101 10000000550011010101 10151010010110010001 11100011010000110011 10001101010100100101 10505001010111101101 15501011010000100101 10005000010510000001 11111111111111111111
map2.txt
11111111111111111111 10000000000011010101 10151111111001010101 10000000110011010101 10101010010110010001 11100011010000110011 10001101010100100101 10101001010111101101 11101011010000100101 10000000015010000001 11111111111111111111
map3.txt
11111111111111111111 11111111110111111101 11111110000000005001 11111110111011101011 11000000111011100011 11010110111011101111 11150110111011101111 10000110000000000001 11101111101111111101 10000000000000000001 11111111111111111111
map4.txt
11111111111111111111 11150011110000000001 11111000110111101111 11111110110011101011 11000000111010000001 11010110111011101111 11510110111011101111 10000110000011111111 11111111111011110111 10000000000011110111 11111111111111111111
map5.txt
11111111111111111111 11111111111111000001 11111155111111011111 11111555111111011111 11111155111111011111 11111155111111011111 11111155111111011111 11115555551111011111 11111111111111011111 10000000000000011111 11111111111111111111
运行结果:
首页:

?第一关:
?第二关:
?第三关:

?第四关:

第五关:
?
?
|