gpt4 book ai didi

Java 2D 游戏编程 : Missile shooting is too fast?

转载 作者:太空宇宙 更新时间:2023-11-04 11:12:04 25 4
gpt4 key购买 nike

您好,我对编程相当陌生,这是我第一次在这里发帖,因此我们将不胜感激,因此:我的问题是,我正在尝试用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);
}

关于Java 2D 游戏编程 : Missile shooting is too fast?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45881463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com