在 2D 阵列板中移动并防止玩家移出板

Moving in a 2d array board and preventing player from moving off board

本文关键字:玩家 移出 移动 2D 阵列      更新时间:2023-10-16

我正在尝试从网上找到的练习中制作一个基本游戏。我在地牢地图中使用了 2D 数组。现在对于玩家移动,我想防止玩家离开棋盘,所以我将功能设为playerPositionLimiter。这有效,并且播放器X和玩家Y没有超过9.board

void playerPositionLimiter(){
    //The if condition below prevents the player to move off the board
    if((playerX>9||playerY>9)||(playerX<0||playerY<0)){
        if(playerX>9) playerX=9;
        else if(playerX<0) playerX=0;
        else if(playerY>9) playerY=9;
        else if(playerY<0) playerY=0;
    }
}

现在我想使用相同的函数来防止敌人X和Y位置移出板,所以我将代码更改为

void playerPositionLimiter(int posX, int posY){
//Prevents the player from moving off the board
if((posX>9||posY>9)||(posX<0||posY<0)){
    if(posX>9) posX=9;
    else if(posX<0) posX=0;
    else if(posY>9) posY=9;
    else if(posY<0) posY=0;
}}

现在,如果我继续向右移动,X 值将继续增加,并在超过 9.秒后移动到下面的行

完整代码:

#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
/* Dungeon Crawl*/
/*Player variables*/
int turn=0;
int playerXprev;
int playerYprev;
int playerX=0;
int playerY=0;
int enemyX=rand()%10+1;
int enemyY=rand()%10+1;
/*Game variables*/
bool gameOver=false;
//Declaring functions
void gameScreenUpdate();
void playerMovementHandling();
//
// Char array for dungeon map
char dungeonMap[10][10]={
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','T','.','.','T','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','T','.','.','.'},
{'.','.','.','.','T','.','.','.','.','.'},
{'.','.','.','.','.','.','.','T','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','X'},
};
// Function for printing the map
void printDungeonMap(){
    int dR=0;
    while(dR<10){
        for(int dC=0;dC<10;dC++){
            cout << dungeonMap[dR][dC];
        }
        cout << endl;
        dR++;
    }
}
// Sets the position of player in array and clears the previous position
void playerPositionPrint(){
    dungeonMap[playerYprev][playerXprev]='.';
    dungeonMap[playerY][playerX]='P';
}
// Prevents the player to move off the grid
/*
void playerPositionLimiter(){
    //The if condition below prevents the player to move off the board
    if((playerX>9||playerY>9)||(playerX<0||playerY<0)){
        if(playerX>9) playerX=9;
        else if(playerX<0) playerX=0;
        else if(playerY>9) playerY=9;
        else if(playerY<0) playerY=0;
    }
}
*/
void playerPositionLimiter(int posY, int posX){
    //Prevents the player from moving off the board
        if(posX>9) posX=9;
        if(posX<0) posX=0;
        if(posY>9) posY=9;
        if(posY<0) posY=0;
}
// Game Conditions status | checking for winning / losing condition
void checkPlayerCondition(){
    string gameMessage;
/*
switch (dungeonMap[playerX][playerY]){
case 'X':
gameMessage = "You win!";
gameOver=true;
break;
case 'E':
gameMessage = "You ran into an enemy and died!";
gameOver=true;
break;
case 'T':
gameMessage = "You ran into a trap and died!";
gameOver=true;
break;
case '.':
gameOver=false;
break;
default:
gameOver=false;
}*/
    if(dungeonMap[playerY][playerX]=='X'){
        //gameScreenUpdate();
        gameMessage = "You win!";
        gameOver=true;
    }else if(dungeonMap[playerY][playerX]=='E'){
        //gameScreenUpdate();
        gameMessage = "You ran into an enemy and died!";
        gameOver=true;
    }else if(dungeonMap[playerY][playerX]=='T'){
        //gameScreenUpdate();
        gameMessage = "You ran into a trap and died!";
        gameOver=true;
    }else if(dungeonMap[playerY][playerX]=='.'){
        //gameScreenUpdate();
        gameOver=false;
    }
    gameScreenUpdate();
    cout << gameMessage;
}
// Takes user input and changes the X Y co-ordinates of the player
void userInputHandling(){
    playerXprev=playerX;
    playerYprev=playerY;
    char playerChar;
    cout << "Next move: ";
    playerChar=_getch();
    switch (playerChar){
    case 'w':
    playerY--;
    break;
    case 's':
    playerY++;
    break;
    case 'd':
    playerX++;
    break;
    case 'a':
    playerX--;
    break;
    default:
    cout << "(w/a/s/d)" << endl;
    userInputHandling();
}
}
// Takes user input -> checks whether player position is in limits -> checks player conditions(game conditions)
void playerPositionHandling(){
    userInputHandling();
    playerPositionLimiter(playerX,playerY);
    checkPlayerCondition();
}
//gameScreenUpdate clears and updates game screen
void gameScreenUpdate(){
    system("cls");
    playerPositionPrint();
    printDungeonMap();
}
// Moving enemies
void enemyPosition(){
    dungeonMap[enemyX][enemyY] = 'E';
}
// Move enemy
/*
void moveEnemy(){
    if(turn%5){
        dungeonMap[enemyY][enemyX]='.';
        enemyX=enemyX+(rand()%1+(-1));
        enemyY=enemyY+(rand()%1+(-1));
    }
    enemyPosition();
}*/
// Prints the game screen for the first time then loops till game over is true
int main(){
    srand(632);
    enemyPosition();
    gameScreenUpdate();
    while(!gameOver){
    playerPositionHandling();
    //moveEnemy();
    turn++;
    //cout << turn;
    cout << "X:" << playerX;
    cout << "Y:" << playerY;
    }
}

再看看你的代码:

if((posX>9||posY>9)||(posX<0||posY<0)){
    if(posX>9) posX=9;
    else if(posX<0) posX=0;
    else if(posY>9) posY=9;
    else if(posY<0) posY=0;
}}

它将在所有有问题的情况下进入if,但如果有两个单独的问题,如 x <0 和 y> 9,posX将设置为 0,posY将保持不变。修复建议:

if(posX>9) posX=9;
if(posX<0) posX=0;
if(posY>9) posY=9;
if(posY<0) posY=0;

外部if被移除,单独的if解决了每个问题。

编辑

另一个问题是posXposY是局部参数,您需要将它们的引用传递给函数,因为函数运行后 posXposY 参数将不复存在,即使您在参数中存储了正确的值,外部值也将保持不变。