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;
}
}
}
