I'm trying to figure out a way to change the level in my game, I've got level 1 & 2 as subclasses of World, trying to figure out where to put the loop that checks the condition that if there's no fruit left, the next world will load with more fruit to eat and another enemy to dodge. I've hardcoded the amount of fruit and enemies in the level 2 because I'm only going to have about 5 levels, would like to increase the speed as well per level but not necessary at this point.
Have included LevelOne, LevelTwo, Player, Fruit and Counter (even though it only displays the score). I've commented in the code where I've tried things, any help would be appreciated.
import greenfoot.*;
public class LevelOne extends World
{
private Counter theCounter;
private int fruitAmount;
public LevelOne()
{
super(600, 400, 1);
GreenfootImage bg = new GreenfootImage("space-1.jpg");
bg.scale(getWidth(), getHeight());
setBackground(bg);
prepare();
theCounter = new Counter();
addObject(theCounter, 10, 20);
}
public Counter getCounter()
{
return theCounter;
}
private void prepare()
{
Player car = new Player();
addObject(car,76,50);
newFruit(9);
newEnemy(1);
}
private void newFruit(int fruitCount)
{
for(int f = 0; f <= fruitCount; f++)
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
if(x < 32)
{
x+=32;
}
if(y < 32)
{
y+=32;
}
if(x > getWidth()-32)
{
x-=32;
}
if(y > getHeight()-32)
{
y-=32;
}
addObject(new Fruit("cherries.png"), x, y);
}
fruitAmount = fruitCount;
if (fruitCount == 2) //tried this.fruitAmount and still nothing
{
changeLevel();
}
}
private void newEnemy(int rocketCount)
{
for(int r = 0; r <= rocketCount; r++)
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
if(x < 32)
{
x+=32;
}
if(y < 32)
{
y+=32;
}
if(x > getWidth()-32)
{
x-=32;
}
if(y > getHeight()-32)
{
y-=32;
}
addObject(new Enemy(), x, y);
}
}
public void changeLevel()
{
LevelTwo newLevel = new LevelTwo(); //create new object of LevelTwo
Greenfoot.setWorld(newLevel); //set the world with that object?
}
}import greenfoot.*;
public class LevelTwo extends World //this is pretty much a carbon copy of LevelOne
{
private Counter theCounter;
public LevelTwo()
{
super(600, 400, 1);
GreenfootImage bg = new GreenfootImage("space-2.jpg");
bg.scale(getWidth(), getHeight());
setBackground(bg);
prepare();
theCounter = new Counter();
addObject(theCounter, 10, 20);
}
public Counter getCounter()
{
return theCounter;
}
private void prepare()
{
Player car = new Player();
addObject(car,76,50);
newFruit(12);
newEnemy(2);
}
private void newFruit(int fruitCount)
{
for(int f = 0; f <= fruitCount; f++)
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
if(x < 32)
{
x+=32;
}
if(y < 32)
{
y+=32;
}
if(x > getWidth()-32)
{
x-=32;
}
if(y > getHeight()-32)
{
y-=32;
}
addObject(new Fruit("bananas.png"), x, y);
}
}
private void newEnemy(int rocketCount)
{
for(int r = 0; r <= rocketCount; r++)
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
if(x < 32)
{
x+=32;
}
if(y < 32)
{
y+=32;
}
if(x > getWidth()-32)
{
x-=32;
}
if(y > getHeight()-32)
{
y-=32;
}
addObject(new Enemy(), x, y);
}
}
}
import greenfoot.*;
public class Player extends Actor //it's a spaceship that eats fruit
{
public void act()
{
if(Greenfoot.isKeyDown("up"))
{
move(8);
}
if(Greenfoot.isKeyDown("down"))
{
move(-12);
}
if (Greenfoot.isKeyDown("left"))
{
turn(-5);
}
if (Greenfoot.isKeyDown("right"))
{
turn(5);
}
Actor fruit;
fruit = getOneObjectAtOffset(0, 0, Fruit.class);
if (fruit != null)
{
int fruitCount = 0;
World world;
world = getWorld();
world.removeObject(fruit);
eatFruit();
fruitCount++;
if (fruitCount == 2) //no errors but nothing happens
{
LevelTwo newLevel = new LevelTwo();
Greenfoot.setWorld(newLevel);
//not even sure if I should do it in this class
}
}
}
private void eatFruit()
{
LevelOne newWorld = (LevelOne) getWorld();
Counter counter = newWorld.getCounter();
counter.bumpCount(5);
}
}
import greenfoot.*;
public class Fruit extends Actor
{
private String image;
public Fruit(String image)
{
setImage(image);
}
public void act()
{
moveAround();
}
public void moveAround()
{
move(4);
if (Greenfoot.getRandomNumber(100) < 10)
{
turn(Greenfoot.getRandomNumber(90) - 45);
}
if(getX() <= 5 || getX() >= getWorld().getWidth() - 5)
{
turn (180);
}
if(getY() <= 5 || getY() >= getWorld().getHeight() - 5)
{
turn (180);
}
}
}
import greenfoot.*;
public class Counter extends Actor //displays the counter
{
private int totalCount = 0;
public Counter()
{
setImage(new GreenfootImage("0", 50, Color.WHITE, Color.BLACK));
}
public void bumpCount(int amount)
{
totalCount += amount;
setImage(new GreenfootImage(" " + totalCount, 50, Color.WHITE, Color.BLACK));
}
}

