-1

现在我正在开发一种太空侵略者风格的游戏。您将角色移动到敌人所在的 y 坐标,然后射击。

玩家将射击四个窗口。其中一个总会有敌人。

下面是代码的工作方式:

enemylocation = 1;
*CHANGE VALUE EVERY X SECONDS
if(enemylocation==1){
enemy.draw(x, y, size);
}
if(enemylocation==2){
enemy.draw(x, y, size);
}
if(enemylocation==3){
enemy.draw(x, y, size);
}
if(enemylocation==4){
enemy.draw(x, y, size);
}

时间代码/方法是什么?谢谢

4

2 回答 2

0

大多数人会使用 Swing 计时器在给定的时间间隔内更新代码。我会使用 java.Math.Random 类将数字更改为 1-4。

public class game implements ActionListener{

    Timer enemyUpdate;
    int enemylocation;

    public game(){

    enemyUpdate = new Timer(1000, this); //1000ms = 1s

    enemyUpdate.start();


    }

    public void actionPerformed(){

        //utilize math.random to change enemylocation every second

    }

}
于 2013-07-25T02:00:36.110 回答
0

你会想要使用这样的东西:

public static void main(String[] args) throws InterruptedException
{
  while(true) //Makes the code run forever
  {
    enemylocation++; // increments 1
    Thread.sleep(1000); // Waits one second
  }
 }
于 2013-07-25T01:49:05.767 回答