我正在编写一个吃豆人风格的游戏,其中假设怪物在迷宫中以随机方向移动。但我无法弄清楚如何让他们适应他们想要的正确方向。我正在使用一些常量作为方向。
public class Monster extends GObject {
private final int DIRECTION_UP = 0;
private final int DIRECTION_DOWN = 1;
private final int DIRECTION_LEFT = 2;
private final int DIRECTION_RIGHT = 3;
private int DIRECTION = 2;
private float speed = 0.06f;
public Monster(float x, float y){
super(Treasure.MONSTER_LEFT);
setX(x);
setY(y);
DIRECTION = random(3);
}
public void update(long elapsedTime){
setVelocityX(0);
setVelocityY(0);
switch (DIRECTION){
case DIRECTION_UP :
setVelocityY(-speed);
setImage(Treasure.MONSTER_UP);
break;
case DIRECTION_DOWN :
setVelocityY(speed);
setImage(Treasure.MONSTER_DOWN);
break;
case DIRECTION_LEFT :
setVelocityX(-speed);
setImage(Treasure.MONSTER_LEFT);
break;
case DIRECTION_RIGHT :
setVelocityX(speed);
setImage(Treasure.MONSTER_RIGHT);
break;
}
changeDirection();
}
private int random(int x){
return (int)Math.floor(Math.random() * (x+1));
}
public void collision(GObject other){
if (other instanceof Wall){
switch (DIRECTION){
case DIRECTION_UP :
setY(other.getY()+other.getHeight());
break;
case DIRECTION_DOWN :
setY(other.getY()-getHeight());
break;
case DIRECTION_LEFT :
setX(other.getX()+other.getWidth());
break;
case DIRECTION_RIGHT :
setX(other.getX()-getWidth());
break;
}
}
}
}
这是我使用的 changeDirection() 方法。
private void changeDirection(){
int random = random(1);
boolean opposite = (random==1);
int dir = DIRECTION;
switch (Explorer.DIRECTION){
case DIRECTION_UP :
if (opposite){
dir = DIRECTION_DOWN;
} else {
dir = DIRECTION_UP;
}
break;
case DIRECTION_DOWN :
if (opposite){
dir = DIRECTION_UP;
} else {
dir = DIRECTION_DOWN;
}
break;
case DIRECTION_LEFT :
if (opposite){
dir = DIRECTION_RIGHT;
} else {
dir = DIRECTION_LEFT;
}
break;
case DIRECTION_RIGHT :
if (opposite){
dir = DIRECTION_LEFT;
} else {
dir = DIRECTION_RIGHT;
}
break;
}
switch (dir){
case DIRECTION_UP :
if (Map.isObjectCollisionFree(getX(), getY()-1, true, this)){
DIRECTION = dir;
}
break;
case DIRECTION_DOWN :
if (Map.isObjectCollisionFree(getX(), getY()+1, true, this)){
DIRECTION = dir;
}
break;
case DIRECTION_LEFT :
if (Map.isObjectCollisionFree(getX()-1, getY(), true, this)){
DIRECTION = dir;
}
break;
case DIRECTION_RIGHT :
if (Map.isObjectCollisionFree(getX()+1, getY(), true, this)){
DIRECTION = dir;
}
}
}
任何人都可以帮助我如何让怪物在迷宫中随机移动......
我目前面临的问题是怪物没有移动......他们在游戏中跳跃。如果他们在视野之外,他们就永远不会回来。监控他们说我他们的坐标达到了-12000并停在那里。这就是我检查墙壁的方式。
if (Map.isObjectCollisionFree(<x-pos>, <y-pos>, <solid-state>, <object>))
这对玩家有用。在碰撞方法中,我将怪物与地图的网格对齐。
编辑:更改了 changeDirection() 方法
private void changeDirection(){
DIRECTION = random(3);
}
the update() method
public void update(long elapsedTime){
if (MapView.isVisible(this)){
if ((getX()%Map.TILE_SIZE==0) && (getY()%Map.TILE_SIZE==0)){
changeDirection();
}
setVelocityX(0);
setVelocityY(0);
switch (DIRECTION){
case DIRECTION_UP :
setVelocityY(-speed);
setImage(Treasure.MONSTER_UP);
break;
case DIRECTION_DOWN :
setVelocityY(speed);
setImage(Treasure.MONSTER_DOWN);
break;
case DIRECTION_LEFT :
setVelocityX(-speed);
setImage(Treasure.MONSTER_LEFT);
break;
case DIRECTION_RIGHT :
setVelocityX(speed);
setImage(Treasure.MONSTER_RIGHT);
break;
}
}
}