放假期間試圖制作了迷宮小游戲,用的軟體是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
運行結果:
首頁:

第一關:
第二關:
第三關:

第四關:

第五關:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/293260.html
標籤:其他
