溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用C++實(shí)現(xiàn)迷宮游戲

發(fā)布時(shí)間:2020-09-28 19:43:11 來(lái)源:腳本之家 閱讀:164 作者:Viitasca 欄目:編程語(yǔ)言

迷宮游戲就是玩家在地圖中移動(dòng),移動(dòng)至終點(diǎn)則游戲結(jié)束。

自己用文本文檔手打了個(gè)小地圖,0表示空白,1表示墻,文件名隨意,我改成了map.MapData。然后程序里定義一個(gè)全局變量char Map[MapLenX][MapLenY];(長(zhǎng)寬自定義)行為X,列為Y。定義char型常量RoadSymbol = '0', WallSymbol = '1', PlayerSymbol = '+'。

本游戲?yàn)槊嫦驅(qū)ο缶帉?xiě)的,所以就要設(shè)計(jì)一個(gè)類(lèi)。數(shù)據(jù)需要一個(gè)坐標(biāo)和一個(gè)bool型儲(chǔ)存是否到達(dá)終點(diǎn)。所以自定義了個(gè)結(jié)構(gòu)體儲(chǔ)存坐標(biāo)

struct point
{
 int x, y;
};

還需要構(gòu)造函數(shù),析構(gòu)函數(shù),然后寫(xiě)個(gè)移動(dòng)的函數(shù)PlayerMove(),再寫(xiě)個(gè)判斷是否到達(dá)終點(diǎn)的函數(shù)CheckIfWin()。每走完一步就要刷新屏幕,所以還需要寫(xiě)個(gè)函數(shù)Refresh(),然后PlayerActor類(lèi)就完成了。

class PlayerActor
{
public:
 point m_Location;
 bool m_IfWin;
 PlayerActor();
 ~PlayerActor();
 void PlayerMove(int _Direc);
 void Refresh(void);
 void CheckIfWin(void);
};

構(gòu)造函數(shù)析構(gòu)函數(shù)先不著急, 先定義一下PlayerMove()。思路是先判斷是否可移動(dòng)。若能,當(dāng)前位置的地圖標(biāo)記設(shè)為RoadSymbol, 移動(dòng)即更新坐標(biāo),新坐標(biāo)位置在地圖上標(biāo)記為PlayerSymbol, 刷新畫(huà)面,判斷輸贏。Refresh()思路為先用system("cls")清屏,然后逐行打印。若地圖上某點(diǎn)為RoadSymbol輸出空格, WallSymbol輸出'*', PlayerSymbol輸出'+'。

接下來(lái)定義玩家起始位置和終點(diǎn)PlayerStart和PlayerEnd并初始化。main函數(shù)大體流程如下:讀入地圖,實(shí)例化PlayerActor,當(dāng)!m_IfWin時(shí)接收鍵盤(pán)按鍵來(lái)移動(dòng),當(dāng)m_IfWIn時(shí)彈出提示框并結(jié)束。所以還需要一個(gè)全局函數(shù)PlayerControl來(lái)接收按鍵并調(diào)用PlayerMove()。

至此,構(gòu)造函數(shù)的流程也明確了。初始化m_IfWin和m_Location,在地圖上表明玩家位置和重點(diǎn)位置,刷新屏幕,沒(méi)了。然后再把能定義為常量的都定位常量,修改一下細(xì)節(jié),就能得到一個(gè)簡(jiǎn)陋的走迷宮游戲了。

#include<iostream>
#include<cstdio>
#include"conio.h"
#include"windows.h"
/////////////////////////
struct point
{
 int x, y;
};
 
///////////////////////
 
const point PlayerStart = {10, 2};
const point PlayerEnd = {2, 10};
 
const int MapLenX = 11, MapLenY = 10;
const char EndSymbol = '#', PlayerSymbol = '+', WallSymbol = '1', RoadSymbol = '0';
char Map[MapLenX][MapLenY];
 
const int MoveX[4] = {-1, 1, 0, 0}, MoveY[4] = {0, 0, -1, 1};  //// UP, DOWN, LEFT, RIGHT
const int _UP = 0, _DOWN = 1, _LEFT = 2, _RIGHT = 3;
 
///////// CLASS ///////////////
 
class PlayerActor
{
public:
 point m_Location;
 bool m_IfWin;
 PlayerActor();
 ~PlayerActor();
 void PlayerMove(int _Direc);
 void Refresh(void);
 void CheckIfWin(void);
};
 
/////////// MEMBER FUNCTIONS /////////////
 
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] = PlayerSymbol;
 Map[PlayerEnd.x][PlayerEnd.y] = EndSymbol;
 PlayerActor::Refresh();
}
 
PlayerActor::~PlayerActor()
{
 
}
 
void PlayerActor::PlayerMove(int _Direct)
{
 if ( Map[this-> m_Location.x+MoveX[_Direct]][this-> m_Location.y+MoveY[_Direct]] == RoadSymbol
 || Map[this-> m_Location.x+MoveX[_Direct]][this-> m_Location.y+MoveY[_Direct]] == EndSymbol )/////// JUDGE IF CAN MOVE
 {
  Map[this-> m_Location.x][this-> m_Location.y] = RoadSymbol;
  this-> m_Location.x += MoveX[_Direct];
  this-> m_Location.y += MoveY[_Direct];
  Map[this-> m_Location.x][this-> m_Location.y] = PlayerSymbol;
  PlayerActor::Refresh();
  PlayerActor::CheckIfWin();
 }
}
 
void PlayerActor::Refresh(void)
{
 system("cls");  //////CLEAR SCREEN
 for (int i=1; i<=MapLenX; i++)
 {
  for (int j=1; j<=MapLenY; j++)
  {
   if (Map[i][j] == RoadSymbol)
    printf(" ");
   else if (Map[i][j] == WallSymbol)
    printf("* ");
   else if (Map[i][j] == '+')
    printf("%c ", PlayerSymbol);
   else if (Map[i][j] == EndSymbol)
    printf("%c ",EndSymbol);
  }
  printf("\n");
 }
}
 
void PlayerActor::CheckIfWin(void)
{
 if (this-> m_Location.x == PlayerEnd.x && this-> m_Location.y == PlayerEnd.y)
  m_IfWin = true;
}
 
///////////// GLOBAL FUNCTION ////////////////
 
void PlayerControl(PlayerActor* Player, int _KEY)
{
 switch (_KEY)
 {
  case 119 : Player->PlayerMove(_UP); //// w 119
   break;
  case 115 : Player->PlayerMove(_DOWN); ///////s 115
   break;
  case 97 : Player->PlayerMove(_LEFT); //// a 97
   break;
  case 100 : Player->PlayerMove(_RIGHT); //// d 100
   break;
  default:
   break;
 }
}
 
//////// MAIN FUNCTION ///////////
 
int main()
{
 ///////// READ MAP /////////////
 freopen("map.MapData", "r", stdin);
 for (int i=1; i<=MapLenX; i++)
 {
  for (int j=1; j<=MapLenY; j++)
  {
   std::cin >> Map[i][j];
  }
 }
 //// CREATE PLAYERACTOR ////
 PlayerActor* Player = new PlayerActor;
 while (!Player->m_IfWin)
 {
  PlayerControl(Player, _getch());
 }
 system("cls");
 MessageBox(NULL, "You Win!", "Congratulations!", MB_OK);
 delete Player;
 return 0;
}

地圖map.MapData:

1111111111
1000000001
1011111111
1010000001
1011111101
1000000101
1111110101
1000010101
1011110101
1000000001
1111111111

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI