My game consists of a Rocket(.class) that shoots Boop(.class), trying to hit Asteroid(.class). I would like the Rocket to shoot the Boops one at a time, with a .5 second delay between each one, and the Boop shoots wherever the Rocket is facing. If the Boop hits an Asteroid, it "kills" it, removing it from screen. If the Boop hits the edge of the screen, it disappears. I want the asteroids to spawn at random intervals at 40, 30. The spawning should have a 80% chance that it spawns moving 5 every act, and a 20% chance that it spawns moving 15 every act. If the Asteroid hits the edge of the world, it dies, too.
I currently have this code for the Rocket:
This for the Asteroid:
And this for the Boop:
There's also some stuff in MyWorld, which is:
Stuff in "Object" class, that all other classes inherit :
There are multiple errors that I cannot fix, including:
- Error message pops up when Boop hits edge of world, saying that it cannot locate the object that it needs to remove.
- Asteroids don't spawn unless one of them are already in there, and when one is, they all spam in the top left corner , different ones spawning at 5 pixels right at 15 pixels right.
- Rocket shoots Boop correctly, but space bar must be held down for 0.5 seconds before one shoots. And when it hits the edge of the world, the error message mentioned in Error #1 appears.
Any help would be greatly appreciated.
Thanks.
import greenfoot.*; /** * Rocket for my game. */ public class Rocket extends Object { private int delayTimer = 0; private boolean delay() { if(delayTimer >0) delayTimer--; return delayTimer > 0; } /** * Act method - executes everything inside it when the Run or Act button is pressed. */ public void act() { checkKeys(); checkTurn(); fire(); } /** * Resizes the Rocket and faces it in a certain direction. */ public Rocket(int direction) { setRotation(270); GreenfootImage image = getImage(); image.scale(90 + 45, 28 + 45); setImage(image); } public Rocket() { } /** * Turns the Rocket, preparing it to shoot. */ public void checkKeys() { if(Greenfoot.isKeyDown("left")) { turn(-5); } if(Greenfoot.isKeyDown("right")) { turn(5); } } /** * These are just methods to check parameters of the Rocket, for resizing purposes. */ public int imageWidth() { GreenfootImage image = getImage(); int x = image.getWidth()-45; return x; } public int imageHeight() { GreenfootImage image = getImage(); int y = image.getHeight()-45; return y; } /** * Stopping the Rocket from turning once it turns to a certain direction. This took a LONG time. */ private int getRotation; private void checkTurn() { if (getRotation() >= 1 && getRotation() <= 90) { setRotation(1); } if (getRotation() <= 180 && getRotation() >= 90) { setRotation(180); } } public void fire() { if(!delay()) { checkFire(); delayTimer = 30; } } /** * Firing the Boops out of the Rocket. */ public void checkFire() { if (Greenfoot.isKeyDown("space")) { getWorld().addObject(new Boop(getRotation()), getX(), getY()); } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * This is what the Rocket tries to shoot. */ public class Asteroid extends Object { private int delayTimer = (Greenfoot.getRandomNumber(3) * 60); private boolean delay() { if (delayTimer > 0) delayTimer--; return delayTimer > 0; } /** * Calls every method in the class. */ public void act() { if(!delay()) { newAsteroid(); delayTimer = (Greenfoot.getRandomNumber(3) * 60); } } public void newAsteroid() { if(Greenfoot.getRandomNumber(100) < 20) { getWorld().addObject(new Asteroid(),40, 30); move(15); if(atWorldEdge()) { getWorld().removeObject(this); } } else { getWorld().addObject(new Asteroid(),40, 30); move(5); if(atWorldEdge()) { getWorld().removeObject(this); } } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Boop is what the Rocket fires. Basically, the bullets. */ public class Boop extends Object { /** * This enables the Rocket to fire the Boops, from whatever direction the Rocket is facing. */ private int direction, speed; public Boop(int dir) { speed = 15; direction = dir; } public void act() { setRotation(direction); testEdge(); hitAsteroid(); move(); } public void move() { move(speed); } public void testEdge() { if(atWorldEdge()) { getWorld().removeObject(this); } } public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } } public void hitAsteroid() { if(canSee(Asteroid.class)) { eat(Asteroid.class); } } }
import greenfoot.*; // (Game, MyWorld, background.png) /** * My World! :) */ public class MyWorld extends World { public MyWorld() { super(800, 600, 1); prepare(); } public int x = (new Rocket()).getImage().getWidth(); public int y = (new Rocket()).getImage().getWidth(); /** * Create objects in Save the World. */ private void prepare() { Rocket rocket = new Rocket(); addObject(new Rocket(270),410,510); rocket.setLocation(387,532); rocket.setLocation(403,537); rocket.setLocation(394,519); rocket.setLocation(405,513); } }
import greenfoot.*; /** * Animal. This is the base class for all animals. In addition to the standard Actor * methods, it provides methods to eat other animals and check for the edge of the world. * * @author Michael Kölling * @version 2.0 */ public class Object extends Actor { /** * Test if we are close to one of the edges of the world. Return true is we are. */ public boolean atWorldEdge() { if(getX() < 10 || getX() > getWorld().getWidth() - 10) return true; if(getY() < 10 || getY() > getWorld().getHeight() - 10) return true; else return false; } /** * Return true if we can see an object of class 'clss' right where we are. * False if there is no such object here. */ public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } /** * Try to eat an object of class 'clss'. This is only successful if there * is such an object where we currently are. Otherwise this method does * nothing. */ public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } } }