So I am currently working on an asteroid game where the player has to protect four homes from oncoming asteroids. It is nearly finished but I am having some trouble making the game end. I want to make the game so that if three of the four homes are destroyed, the game ends and a message pops up saying how many asteroids you destroyed and how many bullets you used. I can't seem to get the message to pop up or even for the game to end. Here's the code for the asteroid subclass:
The two relevant bits of code, the hitHome() method and checkCount() method are at the bottom. I have a counter that should be increasing by one (called houseCount) everytime a house is hit by the asteroid. In theory, once this value equals three the game is supposed to end a message pop up. Here's the code for the pop up message (I took it from a discussion board on the greenfoot site and made a couple minor adjustments):
Let me know if you need anything more information. I may not be able to respond until I get back from work tonight. Thank you!
public class asteroid extends movers
{
/**
* Act - do whatever the asteroid wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public int speed = 2;
public static int asteroidRotation = 90;
public int houseCount = 0;
public void act()
{
setRotation(asteroidRotation);
move(speed);
hitHome();
checkCount();
}
public boolean canSee(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
public void destroy(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
if(actor != null)
{
getWorld().removeObject(actor);
}
}
public void hitHome()
{
if (canSee(home.class))
{
//getWorld().addObject(new explosion(), getX(), getY());
destroy(home.class);
getWorld().removeObject(this);
houseCount = houseCount + 1;
}
}
public void checkCount()
{
if(houseCount == 3)
{
getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);
Greenfoot.stop();
}
}
import greenfoot.*;
import java.awt.Color;
public class GameOver extends SubActor
{
String text = " GAME OVER! You destroyed "+ hitCount +" asteroids and used " + bulletCount + "bullets!";
GreenfootImage inner = new GreenfootImage(text, 48, Color.black, new Color(0, 0, 0, 96));
public GameOver()
{
}
// create a gameover image the size of the world
public void addedToWorld(World world)
{
int wide = world.getWidth();
int high = world.getHeight();
GreenfootImage outer = new GreenfootImage(wide, high);
int leftX = (wide - inner.getWidth())/2;
int topY = (high - inner.getHeight())/2;
outer.drawImage(inner, leftX, topY);
setImage(outer);
}
// the following is optional
// remove pop-up when clicked on
public void act()
{
if (Greenfoot.mouseClicked(this))
{
getWorld().removeObject(this);
}
}
}
