Hi all and thanks in advance!!
I am altering a very simple asteroid game ( Think the original code was from a rescource website) to be able to then teach children how to make it.
I am looking to show the final score at the end of the game when the game has finihsed.
I cannot seem to put the score in the setImage at the end even though it does increase throughout the game.
Any help please.
Timer which when it hits 20 i want it to do the above:
Here is my counter for the score:
and the score is actually increased by the number in hitAnAsteroid(); below:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class Timer extends Counter
{
/**
* Act - do whatever the Timer wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int time = 20;
private int count = 65;
public void act()
{
// Add your action code here.
if(time == 0)
{
display2();
Greenfoot.stop();
return;
}
if(counter())
{
time--;
count = 65;
}
display();
}
private boolean counter()
{
if(count > 0)
{
count--;
}
return count == 0;
}
private void display()
{
setImage(new GreenfootImage("Game Ends : " + time, 30, Color.WHITE, Color.BLACK));
}
public void display2()
{
setImage(new GreenfootImage("Game Over : " + "" , 50, Color.WHITE, Color.BLACK));
}
public void setTime()
{
time = 20;
}
public boolean isTimeUp()
{
return time == 0;
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class Counter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Counter extends Actor
{
/**
* Act - do whatever the Counter wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
public Counter()
{
setImage(new GreenfootImage("0",20,Color.WHITE,Color.BLACK));
}
public int totalScore = 0;
public int finalScore;
public void countScore(int amount)
{
totalScore += amount;
setImage(new GreenfootImage("" + totalScore,20,Color.WHITE,Color.BLACK));
}
public void finalScore( )
{
setImage(new GreenfootImage("" + finalScore,20,Color.WHITE,Color.BLACK));
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class Shot here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Shot extends Actor
{
private Rocket myShip;
public Shot(Rocket myShip)
{
this.myShip = myShip;
}
void hitAnAsteroid()
{
Space spaceWorld = (Space) getWorld(); //get a reference to the world
Counter counter = spaceWorld.getCounter();//get a reference to the counter
counter.countScore(5);
}
void hitAnAsteroid2()
{
Space spaceWorld = (Space) getWorld(); //get a reference to the world
Counter counter = spaceWorld.getCounter();//get a reference to the counter
counter.countScore(1);
}
/**
* Act - do whatever the Shot wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
int yPosition = getY();
if(yPosition >0)
{
yPosition = yPosition - 5;
setLocation(getX(),yPosition);
Actor rock = getOneIntersectingObject(Rock.class);
Actor pretendRock = getOneIntersectingObject(PretnedRock.class);
if(rock != null)
{
hitAnAsteroid();
getWorld().removeObject(rock);
getWorld().removeObject(this);
}
if(pretendRock != null)
{
hitAnAsteroid2();
getWorld().removeObject(pretendRock);
getWorld().removeObject(this);
}
}
else
{
getWorld().removeObject(this);
}
}
}
