C++を使った簡単な迷宮ゲーム

5276 ワード

迷宮ゲームとは、プレイヤーが地図を移動し、ゴールに移動するとゲームが終了するゲームです.
自分で文書ドキュメントの手で小さな地図を打って、0は空白を表して、1は壁を表して、ファイル名は勝手にして、私はmapに変えました.MapData.そしてプログラムでグローバル変数char Map[MapLenX][MapLenY];(縦横カスタム)動作X、列Y.char型定数RoadSymbol='0',WallSymbol='1',PlayerSymbol='+'を定義します.
このゲームはオブジェクト向けに書かれているので、クラスを設計します.データは座標とbool型のストレージが終点に到達するかどうかを必要とします.構造体格納座標をカスタマイズしました
struct point
{
    int x, y;
};

また,コンストラクション関数,コンストラクション関数,次に移動する関数PlayerMove()を書き,終点に到達するか否かを判断する関数CheckIfWin()を書く必要がある.一歩歩くたびに画面がリフレッシュされるので、関数Refresh()を書いてPlayerActorクラスが完了します.
class PlayerActor
{
public:
    point  m_Location;
    bool m_IfWin;
    PlayerActor();
    ~PlayerActor();
    void PlayerMove(int _Direc);
    void Refresh(void);
    void CheckIfWin(void);
};

コンストラクタ解析関数は焦らずにPlayerMove()を定義します.考え方は、移動可能かどうかを判断することです.できれば、現在位置の地図マークをRoadSymbolに設定し、移動すると座標を更新し、新しい座標位置は地図上でPlayerSymbolとマークし、画面をリフレッシュし、勝負を判断する.Refresh()は、system(「cls」)でスクリーンをクリアし、行単位で印刷することを考えています.地図上の点がRoadSymbol出力スペースの場合、WallSymbol出力'*'、PlayerSymbol出力'+'です.
次に、プレイヤーの開始位置と終了位置PlayerStartとPlayerEndを定義して初期化する.main関数の大まかな流れは以下の通りです:地図を読み込んで、PlayerActorをインスタンス化して、!m_IfWinはキーボードボタンを受信して移動し、m_IfWInでプロンプトボックスがポップアップされ、終了します.したがって、キーを受信してPlayerMove()を呼び出すグローバル関数PlayerControlも必要である.
これで,コンストラクション関数の流れも明らかになった.初期化m_IfWinとm_Locationは、マップ上でプレイヤーの位置とポイントの位置を表示し、画面をリフレッシュし、なくなりました.そして定数と定義できるものを定数として位置づけ、細部を修正すると、粗末な迷宮遊びが得られます.
#include
#include
#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("
"); } } 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; }

地図MapData:
1111111111
1000000001
1011111111
1010000001
1011111101
1000000101
1111110101
1000010101
1011110101
1000000001
1111111111