Greenfoot: Modified Balloons

Code

Modified BalloonWorld

Just one small change:

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 slider/shooter at bottom of screen
// 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);
}

}

 

Modified balloon class:

Another small change to reverse the direction of the balloons:

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

 

New class slider

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

 

New class 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);  //asteroid falling down
Actor balloon = getOneIntersectingObject(Balloon.class);  //collision?
if(balloon != null) {     //yes!
Greenfoot.playSound("pop.wav");
((BalloonWorld) getWorld()).countPop();
getWorld().removeObject(balloon);
}
if (getY() < 10) {
getWorld().removeObject(this);
}
}
}