Space
Asteroid
Rocket
Scoreboard
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
/**
* Space. Something for rockets to fly in.
*
* @author Michael Kolling
* @version 1.1
*/
public class Space extends World
{
private Counter scoreCounter;
private int startAsteroids = 3;
private int startStars = 500;
public void countScore (int count)
{
scoreCounter.add(count);
}
public Space()
{
super(1200, 800, 1);
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
createStars(startStars);
createRandomStars(startStars);
createChallengeStars(startStars);
Rocket rocket = new Rocket();
addObject(rocket, getWidth()/2 + 100, getHeight()/2);
addAsteroids(startAsteroids);
scoreCounter = new Counter("Score: ");
addObject(scoreCounter, 60, 780);
Explosion.initializeImages();
ProtonWave.initializeImages();
}
/**
* Add a given number of asteroids to our world. Asteroids are only added into
* the left half of the world.
*/
private void addAsteroids(int count)
{
for(int i = 0; i < count; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Asteroid(), x, y);
}
}
private void addAsteroidUsingWhile(int count)
{
int i = 0;
while(i < count)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Asteroid(), x, y);
i++;
}
}
/**
* Crete a given number of stars in space.
*/
private void createChallengeStars(int number)
{
GreenfootImage background = getBackground();
for(int i=0; i < number; i++)
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
background.setColor(new Color(Greenfoot.getRandomNumber(256),Greenfoot.getRandomNumber(256),Greenfoot.getRandomNumber(256)));
final int r = ((Greenfoot.getRandomNumber(5) * 2) + 2);
background.fillOval(x, y, r, r);
}
}
/**
* Crete a given number of stars in space.
*/
private void createRandomStars(int number)
{
GreenfootImage background = getBackground();
for(int i=0; i < number; i++)
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
int color = 255 - Greenfoot.getRandomNumber(100);
background.setColor(new Color(color, color, color));
background.fillOval(x, y, 2, 2);
}
}
/**
* Crete a given number of stars in space.
*/
private void createStars(int number)
{
GreenfootImage background = getBackground();
for(int i=0; i < number; i++)
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
background.setColor(Color.WHITE);
background.fillOval(x, y, 2, 2);
}
}
/**
* This method is called when the game is over to display the final score.
*/
public void gameOver()
{
addObject(new ScoreBoard(), getWidth()/2, getHeight()/2);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* A rock in space
*
* @author Poul Henriksen
*/
public class Asteroid extends SmoothMover
{
/** Size of this asteroid */
private int size;
/** When the stability reaches 0 the asteroid will explode */
private int stability;
public Asteroid()
{
this(50);
}
public Asteroid(int size)
{
super(new Vector(Greenfoot.getRandomNumber(360), 2));
setSize(size);
}
public Asteroid(int size, Vector speed)
{
super(speed);
setSize(size);
}
public void act()
{
move();
}
/**
* Set the size of this asteroid. Note that stability is directly
* related to size. Smaller asteroids are less stable.
*/
public void setSize(int size)
{
stability = size;
this.size = size;
GreenfootImage image = getImage();
image.scale(size, size);
}
/**
* Return the current stability of this asteroid. (If it goes down to
* zero, it breaks up.)
*/
public int getStability()
{
return stability;
}
/**
* Hit this asteroid dealing the given amount of damage.
*/
public void hit(int damage)
{
stability = stability - damage;
if(stability <= 0)
breakUp ();
}
/**
* Break up this asteroid. If we are still big enough, this will create two
* smaller asteroids. If we are small already, just disappear.
*/
private void breakUp()
{
Space space = (Space) getWorld();
Greenfoot.playSound("Explosion.wav");
if(size <= 16) // Removing an Asteroid is worth 25 points
{
getWorld().removeObject(this);
space.countScore(25);
}
else // Splitting an Asteroid is worth 10 points
{
int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
double l = getMovement().getLength();
Vector speed1 = new Vector(r + 60, l * 1.2);
Vector speed2 = new Vector(r - 60, l * 1.2);
Asteroid a1 = new Asteroid(size/2, speed1);
Asteroid a2 = new Asteroid(size/2, speed2);
getWorld().addObject(a1, getX(), getY());
getWorld().addObject(a2, getX(), getY());
a1.move();
a2.move();
getWorld().removeObject(this);
space.countScore(10);
}
}
}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 static final int protonReloadTime = 2; // The minimum delay between proton wave bursts.
private int reloadDelayCount; // How long ago we fired the gun the last time.
private int protonDelayCount; // How long ago we fired the proton wave the last time.
private GreenfootImage rocket = new GreenfootImage("rocket.png");
private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");
/**
* Initilise this rocket.
*/
public Rocket()
{
reloadDelayCount = 5;
protonDelayCount = 500;
int b = Greenfoot.getRandomNumber(360);
addForce(new Vector(b, 0.3)); // initially slowly drifting
}
/**
* 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();
checkCollision();
reloadDelayCount++;
protonDelayCount++;
}
/**
* Check whether there are any key pressed and react to them.
*/
private void checkKeys()
{
slowdown(Greenfoot.isKeyDown("down"));
ignite(Greenfoot.isKeyDown("up"));
if (Greenfoot.isKeyDown("left"))
{
setRotation(getRotation() - 5);
}
if (Greenfoot.isKeyDown("right"))
{
setRotation(getRotation() + 5);
}
if (Greenfoot.isKeyDown("space"))
{
fire();
}
if (Greenfoot.isKeyDown("z"))
{
startProtonWave();
}
}
/**
* Check whether we are colliding with an asteroid.
*/
private void checkCollision()
{
Actor a = getOneIntersectingObject(Asteroid.class);
if (a != null)
{
Space space = (Space) getWorld();
space.addObject(new Explosion(), getX(), getY());
space.removeObject(this);
space.gameOver();
}
}
private void slowdown (boolean reverseThrust)
{
if ( reverseThrust) {
addForce (new Vector(getRotation(), -0.1));
}
}
/**
* Should the rocket be ignited?
*/
private void ignite(boolean boosterOn)
{
if (boosterOn)
{
setImage (rocketWithThrust);
addForce (new Vector(getRotation(), 0.3));
}
else
{
setImage(rocket);
}
}
/**
* 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;
}
}
/**
* Release a proton wave (if it is loaded).
*/
private void startProtonWave()
{
if (protonDelayCount >= protonReloadTime)
{
ProtonWave wave = new ProtonWave();
getWorld().addObject (wave, getX(), getY());
protonDelayCount = 0;
}
}
}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 = 24.0f;
public static final int WIDTH = 500;
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("Asteroids", "By: Alric Woo 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(255,255,255, 128));
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(new Color(0, 0, 0, 128));
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);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
/**
* A proton wave that expands and destroys things in its path.
*
* @author Michael Kolling
* @version 1.0
*/
public class ProtonWave extends Actor
{
/** The damage this wave will deal */
private static final int DAMAGE = 30;
/** How many images should be used in the animation of the wave */
private static final int NUMBER_IMAGES= 30;
/**
* The images of the wave. This is static so the images are not
* recreated for every object (improves performance significantly).
*/
private static GreenfootImage[] images;
/** Current size of the wave */
private int imageCount = 0;
/**
* Create a new proton wave.
*/
public ProtonWave()
{
initializeImages();
setImage(images[0]);
Greenfoot.playSound("proton.wav");
}
/**
* Create the images for expanding the wave.
*/
public static void initializeImages()
{
if (images == null)
{
GreenfootImage baseImage = new GreenfootImage("wave.png");
images = new GreenfootImage[NUMBER_IMAGES];
for (int i = 0; i < NUMBER_IMAGES; i++)
{
int size = (i+1) * ( baseImage.getWidth() / NUMBER_IMAGES );
images[i] = new GreenfootImage(baseImage);
images[i].scale(size, size);
}
}
}
/**
* Act for the proton wave is: grow and check whether we hit anything.
*/
public void act()
{
checkCollision();
grow();
}
/**
* Explode all intersecting asteroids.
*/
private void checkCollision()
{
int range = getImage().getWidth() / 2;
List<Asteroid> asteroids = getObjectsInRange(range, Asteroid.class);
for (Asteroid a : asteroids) {
a.hit (DAMAGE);
}
}
/**
* Grow this wave. If we get to full size, remove.
*/
private void grow()
{
if (imageCount >= NUMBER_IMAGES)
{
getWorld().removeObject(this);
}
else
{
setImage(images[imageCount]);
imageCount++;
}
}
}