NewbJava wrote...
this is what I have. There seems to be something wrong since the game does not switch to the game over screen.
public MyWorld(int w, int h, int c, boolean b)
{
super(w, h, c, b);
}import greenfoot.*;
public class MyWorld extends World // focuses on displaying of the score
{
static Actor scoreDisplay; // all static variable because they rest once the game is finished
static Actor highScoreDisplay;
public static int score;
static int highScore;
public MyWorld() // screen size and sets variable values,
{
super(600, 400, 1);
scoreDisplay = new SimpleActor();
highScoreDisplay = new SimpleActor();
score = -1;
highScore = -1;
adjustScore(1);
}
public MyWorld(int w, int h, int c)
{
super(w, h, c);
}
static void adjustScore(int trial) // score resseting and high score being saved for every round and being replaced if score is largr than high score
{
score += trial;
GreenfootImage img = new GreenfootImage("Score: "+score, 24, Color.RED, new Color(0, 0, 0, 0));
scoreDisplay.setImage(img);
if (score > highScore)
{
highScore = score;
img = new GreenfootImage("High score: "+highScore, 24, Color.RED, new Color(0, 0, 0, 0));
highScoreDisplay.setImage(img);
}
}
public void act() // getting from the MeteorsWorld() method in the sub class
{
Greenfoot.setWorld(new MeteorsWorld());
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class MeteorsWorld extends MyWorld // location of starting objects , meteor location and spawning, and qualifications of game over
{
public MeteorsWorld() // size of screen, methods within the class
{
super(600, 400, 1);
prepare();
adjustScore(-score);
}
public void addMeteor() // places a meteor all the way to left - 1 and at a random y-value
{
addObject(new Meteors(),getWidth()-1,Greenfoot.getRandomNumber(getHeight()));
}
private void prepare() // Location of rocket and where score and high score are located.
{
Rocket Rocket = new Rocket();
addObject(Rocket,100,200);
Rocket.setLocation(70,200);
addObject(scoreDisplay,55,380);
addObject(highScoreDisplay,525,25);
}
public void Switchscreen() // defining when the game is over
{
boolean noRockets = getObjects(Rocket.class).isEmpty();
if (noRockets == true)
{
Greenfoot.setWorld(new Playagain());
}
}
public void act() // Switchscreen() method and the frequency in which a meteor is spawned
{
Switchscreen();
if(Greenfoot.getRandomNumber(20)<1) // Greenfoot.getRandomNumber(20) must be zero to spawn
{
addMeteor();
}
}
}