我正在用 Java 制作游戏,我想创建一个随机移动的角色。我做的那个很痉挛。我基本上想在生成的随机数之间添加延迟。我是初学者,所以不要评判我的代码哈哈
public class Monster extends Entity{
private World world;
Image monster;
public Monster(int x, int y, World world) {
super(x, y, world);
w = 32;
h = 32;
this.world = world;
}
public void render(GameContainer gc, Graphics g) throws SlickException{
super.render(gc, g);
monster = new Image("gfx/world/monster.png");
g.drawImage(monster, x, y);
}
public void update(GameContainer gc, int delta) throws SlickException{
super.update(gc, delta);
Random move = new Random();
int number;
for(int counter=1; counter<=1;counter++){
number = move.nextInt(4);
System.out.println(number);
if(number == 0){
setDy(-1);
}else if(number == 1){
setDx(-1);
}else if(number == 2){
setDy(5);
}else if(number == 3){
setDx(5);
}else{
setDx(0);
setDy(0);
}
}
}
}