在 2D 阵列板中移动并防止玩家移出板
Moving in a 2d array board and preventing player from moving off board
我正在尝试从网上找到的练习中制作一个基本游戏。我在地牢地图中使用了 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
解决了每个问题。
编辑
另一个问题是posX
和posY
是局部参数,您需要将它们的引用传递给函数,因为函数运行后 posX
和 posY
参数将不复存在,即使您在参数中存储了正确的值,外部值也将保持不变。
相关文章:
- 将字符移出范围的危险
- 将移出参数的最佳做法
- 是否可以将值分配给移出位置?
- 将键移出 std::map<> &&
- 移出某些元素后无法从矢量中删除
- 当鼠标移出窗口时,SFML 窗口会自行关闭
- 虚拟析构函数将对象移出 rodata 部分
- 将代码移出类定义时未扩展参数包
- 如何将变量从 for 循环中移出?
- 在移出向量上调用 size() 方法是否安全?
- 从移出对象移动
- 在 2D 阵列板中移动并防止玩家移出板
- 可以将std::getline()与从std::string中移出的字符串一起使用吗
- 是否可以将 std::move 对象移出函数?(C++11).
- 是否应将移出对象保留为"safe"状态?
- 如何将unique_ptr移出矢量<Foo><unique_ptr>?
- 如何使用默认移动构造函数将数据移出范围
- 移出 C++11 中 std priority_queue 元素
- 头文件中定义的多个结构 - 我是否应该将它们移出单独的 h 和 cpp 文件中
- 将资源移出泛型类