在我的游戏中,有 4 个怪物正在移动到相同的随机生成坐标。这意味着随机不工作。
public void run() {
while (true) {
// paints all Sprites, and graphics
updateScreen(getGraphics());
try {
Thread.sleep(sleep);
} catch (Exception e) {
}
}
}
private void updateScreen(Graphics g) {
loops through all monsters and moves them a bit
for (int gi = 0; gi < bottX.length; gi++) {
bot(gi); // moves a specified monster or gets new coordinates
}
}
private void bot(int c) {
// some stuff to move a monster
// if a monster is in desired place, generate new coordinates
if(isInPlace()){
// g]randomly generates new coordinates X and Y
botX(c);
botY(c);
}
}
public void botX(int c) {
// monsters walking coordinates are between 30 px from the spawn zone.
Random r1 = new Random();
int s = r1.nextInt(3);
// number 0 - left 1 - right 2 - don`t go in X axis
// monster spawn coordinate
int botox = spawnnX[c];
int einamx;
if (s == 0) {
einamx = r1.nextInt(30) + (botox - 30);
// [botox-30; botox)
} else if (s == 1) {
einamx = r1.nextInt(29) + (botox + 1); // (botoX+1 botoX+30]
} else {
einamx = botox;
}
// sets to where the monster should go
einammX[c] = einamx;
return;
}
所以在这个游戏中是 4 个怪物,它们的生成坐标是相等的,你只能看到 1 个怪物,因为它们移动相同。顺便说一句,如果我设置不同的生成坐标,我可以看到 4 个移动相同的怪物。