gpt4 book ai didi

Java Space Invaders Game Rapid Fire 作弊触发游戏获胜过早

转载 作者:行者123 更新时间:2023-12-02 04:05:18 25 4
gpt4 key购买 nike

所以我是一个新程序员,正在修改/添加一些东西到已经制作的太空入侵者游戏中,以更熟悉游戏的编程方式。我正在尝试向游戏中添加作弊,但快速作弊给我带来了一些麻烦。当你选择快速射击并通过关卡时,游戏有时会在还剩下一些外星人的情况下提前触发notifyWin。这是因为有时两枪同时击中同一个敌人,使计算机认为有两个不同的敌人被杀死,因此有时会在“你赢了!”的同时留下一些外星人。屏幕弹出。当外星人计数达到 0 时触发胜利,对同一个敌人射击两次会减少外星人计数 2 而不是 1。我似乎无法弄清楚这一点。

// reduce the alient count, if there are none left, the player has won
alienCount--;

if (alienCount == 0)
{
notifyWin ();
}

这是射击与敌人碰撞的代码

/**
* Notification that this shot has collided with another
* entity
*
* @parma other The other entity with which we've collided
*/
public void collidedWith(Entity other) {
// prevents double kills, if we've already hit something,
// don't collide
if (used) {
return;
}

// if we've hit an alien, kill it!
if (other instanceof AlienEntity) {
// remove the affected entities
game.removeEntity(this);
game.removeEntity(other);

// notify the game that the alien has been killed
game.notifyAlienKilled();
used = true;
}
}

最佳答案

一种可能的解决方案是将敌人作为对象添加到列表中。当他们被 Gunicorn 后,将他们从名单中删除,并事先检查他们是否仍然在那里。当列表为空时执行notifyWin。这样,如果对敌人进行碰撞检测的逻辑产生太多命中,它仍然可以正常工作。

这个列表很可能已经存在,以便在屏幕上渲染敌人。

编辑:现在我看到了该方法,您可能只需在 collidedWith 顶部执行以下操作:

if (used || !game.containsEntity(other)) {
return;
}

其中方法 containsEntity 检查保存游戏参与者(实体)的内部列表或结构中实体是否存在。

关于Java Space Invaders Game Rapid Fire 作弊触发游戏获胜过早,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34348384/

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