英文:
Trying to restrict movement within tile based game
问题
如果(玩家 Y == 15 && choice.equals("d")) {
// 什么都不做
}
请注意,上述代码只是为了回答您的问题而提供的示例代码,可能并不适用于完整的上下文。在实际应用中,您需要根据游戏的逻辑和需求来调整代码。
英文:
NOOBIE Question... please help! I have a tile-based game I'm building where I'm moving an icon up down left and right. The dimensions of the game is 15 tiles x 15 tiles. When I'm approaching the boundaries, how can I prevent the icon from moving off the "map". I'm using W (up) A (left) S (down) and D (right) for directional input.
What would be a good way to prevent that icon from going off the map once it's reached the maximum point on the X or Y axis? I defined PlayerY and PlayerX as points where the player exists on the map, but this is the code i currently have for movement.
if (choice.equals("w")){
playerY--;
}
else if (choice.equals("s")){
playerY++;
}
else if (choice.equals("d")){
playerX++;
}
else if (choice.equals("a")){
playerX--;
}
Would you put something here where you say something like player is at max value and d inputted, do nothing? But I don't know how you'd say "do nothing"...
if (player Y == 15 && choice.equals("d")){
________;
}
Sorry again for the dumb question... I'm relatively new to Java and still trying to get my bearings
答案1
得分: 1
我建议为最大的 x 和 y 尺寸保持一些常量。然后,在执行任何移动之前,添加检查边界的逻辑:
final int X_DIM = 15; // 假设棋盘由一个数组表示,那么14是最大的索引
final int Y_DIM = 15; // 假设棋盘由一个数组表示,那么14是最大的索引
if (choice.equals("w")) {
if (playerY > 0) {
playerY--;
}
else {
System.out.println("错误:无法进行越界移动");
}
}
else if (choice.equals("s")) {
if (playerY < Y_DIM - 1) {
playerY++;
}
else {
System.out.println("错误:无法进行越界移动");
}
}
else if (choice.equals("d")) {
if (playerX < X_DIM - 1) {
playerX++;
}
else {
System.out.println("错误:无法进行越界移动");
}
}
else if (choice.equals("a")) {
if (playerX > 0) {
playerX--;
}
else {
System.out.println("错误:无法进行越界移动");
}
}
英文:
I suggest maintaining some constants for the maximum x and y size. Then, add logic which checks the boundaries before making any move:
final int X_DIM = 15; // assuming the board be represented by an array, then 14 is the
final int Y_DIM = 15; // maximum index
if (choice.equals("w")) {
if (playerY > 0) {
playerY--;
}
else {
System.out.println("Error: Cannot make an off-board move");
}
}
else if (choice.equals("s")) {
if (playerY < Y_DIM - 1) {
playerY++;
}
else {
System.out.println("Error: Cannot make an off-board move");
}
}
else if (choice.equals("d")) {
if (playerX < Y_DIM - 1) {
playerX++;
}
else {
System.out.println("Error: Cannot make an off-board move");
}
}
else if (choice.equals("a")) {
if (playerX > 0) {
playerX--;
}
else {
System.out.println("Error: Cannot make an off-board move");
}
}
答案2
得分: 1
只需检查是否在最后或最前的方块上,就像这样:
if (choice.equals("w")) {
if (playerY - 1 >= 0) { // 如果玩家在Y轴上的下一个移动是正向的,则移动
playerY--;
}
} else if (choice.equals("s")) {
if (playerY + 1 < 15) { // 如果玩家在Y轴上的下一个移动小于15(因为有15个方块),则移动
playerY++;
}
} else if (choice.equals("d")) {
if (playerX + 1 < 15) { // 如果玩家在X轴上的下一个移动小于15(因为有15个方块),则移动
playerX++;
}
} else if (choice.equals("a")) {
if (playerX - 1 >= 0) { // 如果玩家在X轴上的下一个移动是正向的,则移动
playerX--;
}
}
英文:
Just check if it is on the last or first tiles or not, like this:
if (choice.equals("w")){
if ( playerY - 1 >= 0 ) { // if the next player move in Y axis is positive, then move
playerY--;
}
}
else if (choice.equals("s")){
if ( playerY + 1 < 15 ) { // if the next player move in Y axis is less than 15 (because you have 15 tiles), then move
playerY++;
}
}
else if (choice.equals("d")){
if ( playerX + 1 < 15 ) { // if the next player move in X axis is less than 15 (because you have 15 tiles), then move
playerX++;
}
}
else if (choice.equals("a")){
if ( playerX - 1 >= 0 ) { // if the next player move in X axis is positive, then move
playerX--;
}
}
答案3
得分: 1
在执行移动之前,您必须检查移动是否有效。如果移动是有效的,执行移动。如果无效,则不要执行移动,并可以为玩家显示警告。
boolean isValidMove(String choice) {
int nextX = playerX;
int nextY = playerY;
if (choice.equals("w")) {
nextY = playerY - 1;
} else if (choice.equals("s")) {
nextY = playerY + 1;
} else if (choice.equals("d")) {
nextX = playerX + 1;
} else if (choice.equals("a")) {
nextX = playerX - 1;
}
// 如果X或Y超出棋盘范围,立即返回false
if (nextX < 0 || nextX >= 15) return false;
if (nextY < 0 || nextY >= 15) return false;
return true; // 如果nextX和nextY在棋盘内,返回true
}
在运行游戏的主方法中:
choice = // 用户输入
if (isValidMove(choice)) {
// 执行移动请求并表示棋盘游戏
} else {
// 可选择向玩家显示警告
}
英文:
Before make an move, you have to check whether the move is valid or not. If the move is valid, move it. If not, don't make the move and optionally show a warning for the player.
boolean isValidMove(choice){
int nextX, nextY;
if (choice.equals("w")){
nextY = playerY-1;
}
else if (choice.equals("s")){
nextY = playerY+1;
}
else if (choice.equals("d")){
nextX = playerX+1;
}
else if (choice.equals("a")){
nextX = playerX-1;
}
//immediately return false if X or Y out of board
if(nextX<0||nextX>=15) return false;
if(nextY<0||nextY>=15) return false;
return true; //return true if nextX and nextY is in the board
}
In main method where run the game:
choice = //user input
if(isValidMove(choice)){
//do the move request and represent board game
} else{
//optionally show a warning to players
}
答案4
得分: 1
我建议从一个辅助方法开始,来确定任何给定位置是否在地图上:
private boolean isOnMap(int x, int y) {
return x >= 0 && x < 15
&& y >= 0 && y < 15;
}
然后,你可以使用这个辅助方法来决定用户请求的移动是否有效,只有在有效时才进行移动。我会将逻辑分为两个步骤,以便更容易处理:第一步是根据用户想要移动到的位置进行简单计算,然后第二步是判断该新位置是否是一个有效的移动。
// 步骤1:从请求的移动中计算新位置
int newX = playerX;
int newY = playerY;
if (choice.equals("w")){
newY--;
}
else if (choice.equals("s")){
newY++;
}
else if (choice.equals("d")){
newX++;
}
else if (choice.equals("a")){
newX--;
}
// 步骤2:应用新位置,但仅在移动有效时才进行
if (isOnMap(newX, newY)) {
playerX = newX;
playerY = newY;
}
英文:
I suggest starting with a helper method to determine whether or not any given position is on the map or not:
private boolean isOnMap(int x, int y) {
return x >= 0 && x < 15
&& y >= 0 && y < 15;
}
Then, you can use that helper method to decide whether or not the move the user requested is valid, and only make the move if it is. I'd split the logic into two steps to make this easier to work with: first step is to naively calculate the new position that the user wants to move to, and then the second step is to decide if that new position is a valid move or not
// Step 1: calculate new position from requested move
int newX = playerX;
int newY = playerY;
if (choice.equals("w")){
newY--;
}
else if (choice.equals("s")){
newY++;
}
else if (choice.equals("d")){
newX++;
}
else if (choice.equals("a")){
newY--;
}
// Step 2: apply the new position, but only if the move is valid
if (isOnMap(newX, newY)) {
playerX = newX;
playerY = newY;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论