Greenfoot: Asteroids With Explosions

(This started out as BalloonWorld.)

The World

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
*
* A world of balloons.
*
* Balloons will be created and flow from the bottom to the top of the screen.
*
* @author Poul Henriksen
*/

public class BalloonWorld extends World
{
Counter counter = new Counter("Score: ");

/**
* Constructor for objects of class MyWorld.
*
*/

public BalloonWorld()
{
super(800,600, 1);

// Make sure actors are painted in the correct order.
setPaintOrder(ScoreBoard.class, Explosion.class, Bomb.class, Balloon.class, Counter.class);

addObject(new slider(), Greenfoot.getRandomNumber(600), 580);
// Add the initial actors
populate();
}

/**
* Creates balloons at the bottom of the world.
*/

public void act()
{
if(Greenfoot.getRandomNumber(100) < 3) {
addObject(new Balloon(), Greenfoot.getRandomNumber(700), 0);
}

}

/**
* Count one popped balloon.
*/

public void countPop()
{
counter.add(20);
}

/**
* Called when game is up. Stop running and display score.
*/

public void gameOver()
{
addObject(new ScoreBoard(counter.getValue()), getWidth()/2, getHeight()/2);
Greenfoot.playSound("buzz.wav");
Greenfoot.stop();
}

/**
* Populate the world with bombs and a crosshair.
*/

private void populate()
{
addObject(new Bomb(), 750, 410);
addObject(new Bomb(), 750, 480);
addObject(new Bomb(), 750, 550);

addObject(counter, 100, 560);
}

}

The Bottom Object (Shooter)

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class slider here.
*
* @author (your name)
* @version (a version number or a date)
*/

public class slider extends Actor
{
/**
* Act - do whatever the slider wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/

private int x;
private int y;

public void act()
{
if (Greenfoot.isKeyDown("left")){
setLocation(getX()-5,getY());
}
if (Greenfoot.isKeyDown("right")){
setLocation(getX()+5,getY());
}
if (Greenfoot.isKeyDown("space")){
if(Greenfoot.getRandomNumber(100) < 5){
getWorld().addObject (new missile(), getX(), getY()-40);
}
}
}


}

ScoreBoard

import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;

/**
* The ScoreBoard is used to display results on the screen. It can display some
* text and several numbers.
*
* @author M Kolling
* @version 1.0
*/

public class ScoreBoard extends Actor
{
public static final float FONT_SIZE = 48.0f;
public static final int WIDTH = 400;
public static final int HEIGHT = 300;

/**
* Create a score board with dummy result for testing.
*/

public ScoreBoard()
{
this(100);
}

/**
* Create a score board for the final result.
*/

public ScoreBoard(int score)
{
makeImage("Game Over", "Score: ", score);
}

/**
* Make the score board image.
*/

private void makeImage(String title, String prefix, int score)
{
GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);

image.setColor(new Color(0, 0, 0, 160));
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(new Color(255, 255, 255, 100));
image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
Font font = image.getFont();
font = font.deriveFont(FONT_SIZE);
image.setFont(font);
image.setColor(Color.WHITE);
image.drawString(title, 60, 100);
image.drawString(prefix + score, 60, 200);
setImage(image);
}
}

 

Missile

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class missile here.
*
* @author (your name)
* @version (a version number or a date)
*/

public class missile extends Actor
{
/**
* Act - do whatever the missile wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/

public void act()
{
setLocation(getX(), getY()-5);
Actor balloon = getOneIntersectingObject(Balloon.class);
if(balloon != null) {
//Greenfoot.playSound("pop.wav");
((BalloonWorld) getWorld()).countPop();
getWorld().addObject(new Explosion(), getX(), getY());
getWorld().removeObject(balloon);

}
if (getY() < 10) {
getWorld().removeObject(this);
}
//destroy balloon when missile strikes
//add to score
}
}

The Balloon (Falling Object)

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* A balloon flows from bottom to top.
*
* @author Poul Henriksen
*/

public class Balloon extends Actor
{
/**
* Make the balloon go down.
*/

public void act()
{
setLocation(getX(), getY() + 1);
if (getY() >= 599) {
((BalloonWorld) getWorld()).gameOver();
}
}

/**
* Pop this balloon.
*/

public void pop()
{
//Greenfoot.playSound("pop.wav");
((BalloonWorld) getWorld()).countPop();
getWorld().removeObject(this);
}
}

 

The Counter

import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Font;

/**
* Counter that displays a number.
*
* @author Michael Kolling
* @version 1.0.1
*/

public class Counter extends Actor
{
private int value = 0;
private int target = 0;
private String text;
private int stringLength;

public Counter()
{
this("");
}

public Counter(String prefix)
{
text = prefix;
stringLength = (text.length() + 2) * 16;

setImage(new GreenfootImage(stringLength, 24));
GreenfootImage image = getImage();
Font font = image.getFont();
image.setFont(font.deriveFont(24.0F)); // use larger font

updateImage();
}

public void act() {
if(value < target) {
value++;
updateImage();
}
else if(value > target) {
value--;
updateImage();
}
}

public void add(int score)
{
target += score;
}

public void subtract(int score)
{
target -= score;
}

public int getValue()
{
return value;
}

/**
* Make the image
*/

private void updateImage()
{
GreenfootImage image = getImage();
image.clear();
image.drawString(text + value, 1, 18);
}
}

 

Explosion

 import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

import java.util.*;

/**
* An explosion. It starts by expanding and then collapsing.
* The explosion will explode other objects that the explosion intersects.
*
* @author Poul Henriksen
* @version 1.0.1
*/

public class Explosion extends Actor
{
/** How many images should be used in the animation of the explostion */
private final static int IMAGE_COUNT= 4;

/**
* The images in the explosion. This is static so the images are not
* recreated for every object (improves performance significantly).
*/

private static GreenfootImage[] images;

/** Current size of the explosion */
private int size=0;

/** How much do we increment the index in the explosion animation. */
private int increment=1;

public Explosion() {

initialiseImages();
setImage(images[0]);
Greenfoot.playSound("explosion.wav");
}

/**
* Create the images for explosion.
*/

public synchronized static void initialiseImages() {
if(images == null) {
GreenfootImage baseImage = new GreenfootImage("explosion.png");
int maxSize = baseImage.getWidth()*2;
int delta = maxSize / (IMAGE_COUNT+1);
int size = 0;
images = new GreenfootImage[IMAGE_COUNT];
for(int i=0; i < IMAGE_COUNT; i++) {
size = size + delta;
images[i] = new GreenfootImage(baseImage);
images[i].scale(size, size);
}
}
}

/**
* EXPLODE!
*/

public void act()
{
setImage(images[size]);

size += increment;
if(size>=IMAGE_COUNT) {
increment = -increment;
size += increment;
}

//explodeOthers();   reduces impact of explosion
if(size <= 0) {
getWorld().removeObject(this);
}
}

/**
* Explodes all intersecting objects.
*/

private void explodeOthers()
{
List explodeEm = getIntersectingObjects(null);
Iterator i = explodeEm.iterator();
while(i.hasNext()) {
Actor a = (Actor) i.next();
if( a instanceof Balloon) { //Don't explode other explosions
int x = a.getX();
int y = a.getY();
//Replace other object with an explosion
((Balloon) a).pop();

//Enable for cascading explossions
//getWorld().addObject(new Explosion(), x, y);

}
}
}
}