您好,我是编程的新手,这是我第一次在这里发布文章,因此我们将不胜感激:
我的问题是我正在尝试用Java创建某种2D射击游戏,但我不知道我的简单游戏循环是否良好,因为当我射击导弹时,它每20毫秒射击一次,而且射击速度太快一次要发射一吨导弹,有什么办法可以调整它吗?
如果我的代码有问题或编程错误,请告诉我!

这是我的游戏面板,其中大部分游戏都在其中发生,并且在其中我循环并添加导弹方法

public class GamePanel extends JPanel implements KeyListener {
Measurments mesure = new Measurments();
int panel_width = mesure.getUniversalWidth();
int panel_height = mesure.getUniversalHeight();
Timer timer;
Random rand = new Random();

ArrayList<Enemy> enemies = new ArrayList<>();
ArrayList<Missile> missiles = new ArrayList<>();

Player player = new Player(0, 0);

boolean up = false;
boolean down = false;
boolean right = false;
boolean left = false;

boolean isShooting = false;
boolean isRunning = true;

public boolean gameRunning() {
    return isRunning;
}

int count = 5;
int missilesCount = 6;

public GamePanel() {

    timer = new Timer(20, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            StartGame();
            repaint();

        }

    });

    setSize(panel_width, panel_height);

    addKeyListener(this);

    timer.start();

    for (int i = 0; i < count; i++) {
        addEnemy(new Enemy(rand.nextInt(750), rand.nextInt(500)));
    }
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    player.paint(g2d);
    for (int i = 0; i < enemies.size(); i++) {

        Enemy temp = enemies.get(i);
        temp.paint(g2d);
    }
    for (int i = 0; i < missiles.size(); i++) {
        Missile mis = missiles.get(i);
        mis.paint(g2d);
        mis.behave();
    }
}

public void StartGame() {
    if (isRunning) {
        runGame();
        setBackground(Color.YELLOW);

    } else {
        setBackground(Color.BLACK);

    }
}
public void runGame() {
    update();

};

public void update() {
    player.checkBorders();

    checkColls();

    if (up) {
        player.updateUp();
    }
    if (down) {
        player.updateDown();
    }
    if (right) {
        player.updateRight();
    }
    if (left) {
        player.updateLeft();
    }
    if (isShooting) {
        for (int i = 0; i < 5; i++) {
            missiles.add(new Missile(player.getX() + 16, player.getY() + 16));
        }

    }
    for (int i = 0; i < missiles.size(); i++) {
        Missile temp = missiles.get(i);
        if (temp.getX() == panel_width) {
            RemoveMissile(temp);
        }
    }

}

public void addEnemy(Enemy e) {
    enemies.add(e);

}

public void removeEnemy(Enemy e) {
    enemies.remove(e);
}

public void addMissile(Missile e) {
    missiles.add(e);
}

public void RemoveMissile(Missile e) {
    missiles.add(e);
}

public void checkColls() {
    for (int i = 0; i < enemies.size(); i++) {
        Enemy tempEnm = enemies.get(i);
        for (int e = 0; e < missiles.size(); e++) {
            Missile tempMis = missiles.get(e);
            if (tempMis.missileRect().intersects(tempEnm.enemyRect())) {
                enemies.remove(tempEnm);
                missiles.remove(tempMis);
            }
        }
    }

}



public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == e.VK_UP) {
        up = true;
    }
    if (key == e.VK_DOWN) {
        down = true;
    }
    if (key == e.VK_RIGHT) {
        right = true;
    }
    if (key == e.VK_LEFT) {
        left = true;
    }
    if (key == e.VK_ENTER) {
        isRunning = true;
    }
    if (key == e.VK_SPACE) {
        isShooting = true;

    }

}

public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == e.VK_UP) {
        up = false;
    }
    if (key == e.VK_DOWN) {
        down = false;
    }
    if (key == e.VK_RIGHT) {
        right = false;
    }
    if (key == e.VK_LEFT) {
        left = false;
    }
    if (key == e.VK_SPACE) {
        isShooting = false;
    }

}

public void keyTyped(KeyEvent e) {

}


}

提前致谢 !!

最佳答案

private long fired = 0L;

public void update() {
   ...
   // firing missiles: only if the missile count is less than the max., and the elapsed
   // time is more than a limit (100 ms)
   if ( isShooting && missiles.size() < missilesCount &&
        ( System.currentTimeMilis() - this.fired ) > 100 ) {
      missiles.add( new Missile( player.getX() + 16, player.getY() + 16 ) );
      // time of last firing
      this.fired = System.currentTimeMilis();
   }
   ...
}

public void RemoveMissile(Missile e) {
   // as Guest is asked in another answer, this method should remove, not add...
   missiles.remove(e);
}

10-07 22:26