Java 为什么赢了';我的火箭碰撞后寿命会减少吗?
好的,在我的游戏设计课上,我们用绿足制作了一个游戏。她要求我们尽可能地制作最难的小行星游戏,同时让游戏变得公平。所以我决定增加一定数量的火箭寿命。我把一切都准备好了,它工作了,它从来不会减少火箭的寿命,那么我该怎么做呢 这是我的代码:Java 为什么赢了';我的火箭碰撞后寿命会减少吗?,java,greenfoot,Java,Greenfoot,好的,在我的游戏设计课上,我们用绿足制作了一个游戏。她要求我们尽可能地制作最难的小行星游戏,同时让游戏变得公平。所以我决定增加一定数量的火箭寿命。我把一切都准备好了,它工作了,它从来不会减少火箭的寿命,那么我该怎么做呢 这是我的代码: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.util.List; /** * A rocket that can be controlled
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
/**
* A rocket that can be controlled by the arrowkeys: up, left, right.
* The gun is fired by hitting the 'space' key.
*
* @author Poul Henriksen
* @author Michael Kölling
*
* @version 2.0
*/
public class Rocket extends Mover
{
private int gunReloadTime; // The minimum delay between firing the gun.
private int reloadDelayCount; // How long ago we fired the gun the last time.
private Vector acceleration; // A vector used to accelerate when using booster.
private Vector deacceleration;
private int shotsFired; // Number of shots fired.
private int rocketLives;
private int collisionDamage = 999;
private GreenfootImage rocket = new GreenfootImage("rocket.png");
private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");
/**
* Initialise this rocket.
*/
public Rocket()
{
gunReloadTime = 10;
reloadDelayCount = 0;
acceleration = new Vector(0, 0.035); // used to accelerate when thrust is on
deacceleration = new Vector(0, -0.035);
increaseSpeed(new Vector(13, 0.3)); // initially slowly drifting
shotsFired = 0;
rocketLives = 3;
}
/**
* Do what a rocket's gotta do. (Which is: mostly flying about, and turning,
* accelerating and shooting when the right keys are pressed.)
*/
public void act()
{
if (rocketLives == 0)
{
getWorld().removeObjects(getWorld().getObjects(null));
Greenfoot.stop();
}
else
{
move();
checkKeys();
checkCollision();
reloadDelayCount++;
}
}
/**
* Return the number of shots fired from this rocket.
*/
public int getShotsFired()
{
return shotsFired;
}
/**
* Set the time needed for re-loading the rocket's gun. The shorter this time is,
* the faster the rocket can fire. The (initial) standard time is 20.
*/
public void setGunReloadTime(int reloadTime)
{
gunReloadTime = reloadTime;
}
/**
* Check whether we are colliding with an asteroid.
*/
private void checkCollision()
{
Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class);
if (asteroid != null)
{
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().addObject(new Rocket(), (1350/2)-20, 1000/2);
if(isTouching(Asteroid.class)) {
asteroid.hit(999);
}
getWorld().removeObject(this);
rocketLives--;
}
}
/**
* Check whether there are any key pressed and react to them.
*/
private void checkKeys()
{
ignite(Greenfoot.isKeyDown("w"));
if(Greenfoot.isKeyDown("s")) {
deignite(Greenfoot.isKeyDown("s"));
}
if(Greenfoot.isKeyDown("a")) {
turn(-5);
}
if(Greenfoot.isKeyDown("d")) {
turn(5);
}
if(Greenfoot.isKeyDown("space")) {
fire();
}
if(Greenfoot.isKeyDown("shift")) {
int ran1 = (int)(Math.random()*1350);
int ran2 = (int)(Math.random()*1000);
getWorld().addObject(new Asteroid(), ran1, ran2);
}
}
/**
* Should the rocket be ignited?
*/
private void ignite(boolean boosterOn)
{
if (boosterOn) {
setImage(rocketWithThrust);
acceleration.setDirection(getRotation());
increaseSpeed(acceleration);
}
else {
setImage(rocket);
}
}
private void deignite(boolean boosterOn)
{
if (boosterOn) {
setImage(rocketWithThrust);
deacceleration.setDirection(getRotation());
increaseSpeed(deacceleration);
}
else {
setImage(rocket);
}
}
/**
* Fire a bullet if the gun is ready.
*/
private void fire()
{
if (reloadDelayCount >= gunReloadTime) {
Bullet b = new Bullet(getMovement().copy(), getRotation());
getWorld().addObject(b, getX(), getY());
b.move();
shotsFired++;
reloadDelayCount = 0; // time since last shot fired
}
}
}
我想我看到了您的错误(调试也应该显示这一点):
这是checkCollision()
中的以下部分:
在发生碰撞时,向世界添加一个新的火箭(初始化为3个生命),然后删除当前火箭。所以你总是有一个有三条生命的火箭。只需重复使用当前的火箭,直到它死掉。我想我看到了您的错误(调试也应显示此错误):
这是checkCollision()
中的以下部分:
在发生碰撞时,向世界添加一个新的火箭(初始化为3个生命),然后删除当前火箭。所以你总是有一个有三条生命的火箭。只需重复使用当前的火箭,直到它死掉。我只看到
rocketLives
在checkCollision()
中递减,因此请检查是否调用了该函数,以及火箭是否最终与小行星相交。顺便说一句,isTouching(Asteroid.class)
似乎是一些非常奇怪的语法,也就是说,对我来说,块读起来像“如果它碰到了任何一颗小行星,那么它就会以999的强度(或类似的强度)撞击到这颗小行星”。我只看到rocketLives
在checkCollision()中递减
因此,请检查是否调用此命令,以及火箭是否最终与小行星相交。顺便说一句,isTouching(Asteroid.class)
似乎有一些非常奇怪的语法,也就是说,对我来说,块读起来像“如果它碰到了任何一颗小行星,那么它就会以999的强度(或类似的强度)撞击到这颗小行星”。啊,我明白了我的错误,一旦火箭与小行星接触,我将如何移动它的位置?@JonCole我不知道你正在使用的框架,但你不能将位置设置为你需要的任何位置吗?我算出了,我使用了这个.setLocation(1350/2,1000/2)
以在middle@JonCole作为旁注:我不会在这里硬编码世界的维度,而是查询它是否更灵活,例如world.getWidth()/2
而不是1350/2
,或者可能更好world.getCenter().getX()
(或者如果可能this.setLocation(world.getCenter())
这将更具可读性)。啊,我明白我的错误,一旦火箭与小行星接触,我将如何移动火箭的位置?@JonCole我不知道你正在使用的框架,但你不能将位置设置为你需要的任何位置吗?我想出来了,我使用了这个。设置位置(1350/2,1000/2)
以在middle@JonCole作为旁注:我不会在这里硬编码世界的维度,而是查询它是否更灵活,例如world.getWidth()/2
而不是1350/2
,或者可能更好world.getCenter().getX()
(或者如果可能this.setLocation(world.getCenter())
这将更具可读性)。
getWorld().addObject(new Rocket(), (1350/2)-20, 1000/2); //here
if(isTouching(Asteroid.class)) {
asteroid.hit(999);
}
getWorld().removeObject(this); //and here