This site requires JavaScript, please enable it in your browser!
Greenfoot back
Mack
Mack wrote ...

2015/10/12

Asteroids scenario

Mack Mack

2015/10/12

#
Hello! So I'm running the asteroids scenario and I need help with a couple codes please! 1) Add the functionality for the down key so when the key is pressed I want to add 0.5 units of force to the rocket in order to slow it down (or make it fly backwards if you press it too many times). 2) Make it so that the rocket with flames picture shows up when I press the up key and it goes back to the rocket without flames picture when I let go. this is the code I have for the rocket already... Thank you!!
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A rocket that can be controlled by the arrowkeys: up, left, right.
 * The gun is fired by hitting the 'space' key. 'z' releases a proton wave.
 * 
 * @author Poul Henriksen
 * @author Michael Kolling
 * 
 * @version 1.0
 */
public class Rocket extends SmoothMover
{
    private static final int gunReloadTime = 5;         // The minimum delay between firing the gun.

    private int reloadDelayCount;               // How long ago we fired the gun the last time.
    
    private GreenfootImage rocket = new GreenfootImage("rocket.png");    
    private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");

    /**
     * Initilise this rocket.
     */
    public Rocket()
    {
        reloadDelayCount = 5;
        addForce(new Vector(13,0.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()
    {
        move();
        checkKeys();
        reloadDelayCount++;
    }
    
    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        if (Greenfoot.isKeyDown("space")) 
        {
            fire();
        }
        if(Greenfoot.isKeyDown("up"))
        {
            /**
             * The book uses an ignite method for this.  DON'T USE THE IGNITE METHOD
             * When the up key is pushed, I want to apply a force that pushes the rocket 
             * from behind (pushes it forward).  I also want to set the flame picture when
             * I press the up key, and then when I let go I want it to change back to the
             * non-flame rocket.  Look at your piano for inspiration on how to do this.
             */
            addForce(new Vector(getRotation(),0.5));
            move();
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setRotation(getRotation()-5);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation(getRotation()+5);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            /**
             * I want the down key to act like the breaks.
             * That means that instead of the force pushing the rocket from the back (pushing forward)
             * it should push from the front (pushing backwards)
             */
        }
    }
    
    /**
     * Fire a bullet if the gun is ready.
     */
    private void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) 
        {
            Bullet bullet = new Bullet (getMovement().copy(), getRotation());
            getWorld().addObject (bullet, getX(), getY());
            bullet.move ();
            reloadDelayCount = 0;
        }
    }
    
}
danpost danpost

2015/10/12

#
(1) You should be able to use pretty much the same thing as what is on line 61, with a negative force value. (2) If you add a boolean field to track the state of the "up" key, you can use it in conjunction with its current state (and updating its value when the state changes) to control the flame image.
Mack Mack

2015/10/13

#
Okay. So for 'add the functionality for the down key' I would keep the code that's in line 61, but with a -0.5.. so it would look like this "addForce(new Vector(getRotation(),-0.5));" I'm getting lost with the second part Danpost, can you please show me that code?
danpost danpost

2015/10/13

#
Mack wrote...
I'm getting lost with the second part Danpost, can you please show me that code?
The following code only gets the current state of the key:
boolean upKeyDown = Greenfoot.isKeyDown("up");
If this value (of 'upKeyDown') is saved in a field and later compared to a later state of the key, we can get when the state of the key is changing:
// instance field
private boolean upKeyDown;
// checking for change in key
if (upKeyDown != Greenfoot.isKeyDown("up"))
{
    // save new state
    upKeyDown = !upKeyDown;
    // change the image
    setImage(upKeyDown ? rocketWithThrust : rocket)
}
You need to login to post a reply.