Hey, How do i use generic class type List<> to calculate how many asteroids are left in the world background and add new asteroids when all have been cleared away. new ones appear, one more every time.
you should insert the block of code of calculating how many asteroids are left in the world background, only at the place where it is necessary. If your code of calculating how many asteroids are left is placed directly in the act() method of a class, that means in every act() cycle your code of calculating remaining asteroids will run, and that is not considered efficient.
//Space.java
import greenfoot.*;
/**
* Space. Something for rockets to fly in.
*
* @author Michael Kölling
* @version 1.2
*/
public class Space extends World
{
private Counter scoreCounter;
private int startAsteroids = 2;//Game will start with 2 asteroids
/**
* Create the space and all objects within it.
*/
public Space()
{
super(600, 500, 1);
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
createStars(300);
Rocket rocket = new Rocket();
addObject(rocket, getWidth()/2 + 100, getHeight()/2);
addAsteroids(startAsteroids);
scoreCounter = new Counter("Score: ");
addObject(scoreCounter, 60, 480);
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);
}
}
/**
* 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() );
int color = 120 - Greenfoot.getRandomNumber(100);
background.setColor(new Color(color,color,color));
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(scoreCounter.getValue()), getWidth()/2, getHeight()/2);
}
/**
* This is for to update the score
*/
public void countScore(int change)
{
scoreCounter.add(change);
}
}
//Asteroid class
import greenfoot.*;
/**
* A rock in space.
*
* @author Poul Henriksen
* @author Michael Kölling
*/
public class Asteroid extends SmoothMover
{
/** Size of this asteroid */
private int size;
/** When the stability reaches 0 the asteroid will explode */
private int stability;
/**
* Create an asteroid with default size and random direction of movement.
*/
public Asteroid()
{
this(50);
}
/**
* Create an asteroid with a given size and random direction of movement.
*/
public Asteroid(int size)
{
super(new Vector(Greenfoot.getRandomNumber(90), 1));
setSize(size);
}
/**
* Create an asteroid with a given size and direction of movement.
*/
public Asteroid(int size, Vector velocity)
{
super(velocity);
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;
((Space)getWorld()).countScore(10);//player will get 10 points everytime it hit the asteroid
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()
{
Greenfoot.playSound("Explosion.wav");
if (size <= 16) {
((Space)getWorld()).countScore(20);//Player will get 20 points for removing final pieces of asteroid
getWorld().removeObject(this);
}
else {
int r = getVelocity().getDirection() + Greenfoot.getRandomNumber(45);
double l = getVelocity().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();
((Space)getWorld()).countScore(15);//Player will get 15 points more for spliting the asteroid
getWorld().removeObject(this);
}
}
}
//Rocket class
import 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 Kölling
*
* @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 = 500; // 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");
/**
* Initialise this rocket.
*/
public Rocket()
{
reloadDelayCount = 5;
protonDelayCount = 500;
addToVelocity(new Vector(13, 0.7)); // 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()
{
ignite(Greenfoot.isKeyDown("up"));
if (Greenfoot.isKeyDown("left"))
{
turn(-5);
}
if (Greenfoot.isKeyDown("right"))
{
turn(5);
}
if (Greenfoot.isKeyDown("space"))
{
fire();
}
if (Greenfoot.isKeyDown("z"))
{
startProtonWave();
}
}
/**
* Check whether we are colliding with an asteroid.
*/
private void checkCollision()
{
Asteroid a = (Asteroid) getOneIntersectingObject(Asteroid.class);
if (a != null)
{
Space space = (Space) getWorld();
space.addObject(new Explosion(), getX(), getY());
space.removeObject(this);
space.gameOver();
}
}
/**
* Should the rocket be ignited?
*/
private void ignite(boolean boosterOn)
{
if (boosterOn)
{
setImage(rocketWithThrust);
addToVelocity(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 (getVelocity(), 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;
}
}
}
//counter class
import greenfoot.*;
/**
* A Counter class that allows you to display a numerical value on screen.
*
* The Counter is an actor, so you will need to create it, and then add it to
* the world in Greenfoot. If you keep a reference to the Counter then you
* can adjust its value.
*
* @author Neil Brown and Michael Kölling
* @version 1.1
*/
public class Counter extends Actor
{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
private int value;
private int target;
private String prefix;
public Counter()
{
this(new String());
}
/**
* Create a new counter, initialised to 0.
*/
public Counter(String prefix)
{
background = getImage(); // get image from class
value = 0;
target = 0;
this.prefix = prefix;
updateImage();
}
/**
* Animate the display to count up (or down) to the current target value.
*/
public void act()
{
if (value < target) {
value++;
updateImage();
}
else if (value > target) {
value--;
updateImage();
}
}
/**
* Add a new score to the current counter value. This will animate
* the counter over consecutive frames until it reaches the new value.
*/
public void add(int score)
{
target += score;
}
/**
* Return the current counter value.
*/
public int getValue()
{
return target;
}
/**
* Set a new counter value. This will not animate the counter.
*/
public void setValue(int newValue)
{
target = newValue;
value = newValue;
updateImage();
}
/**
* Sets a text prefix that should be displayed before
* the counter value (e.g. "Score: ").
*/
public void setPrefix(String prefix)
{
this.prefix = prefix;
updateImage();
}
/**
* Update the image on screen to show the current value.
*/
private void updateImage()
{
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
if (text.getWidth() > image.getWidth() - 20)
{
image.scale(text.getWidth() + 20, image.getHeight());
}
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
