Hi
I have a start screen the shows before you start playing the game. I also have a game over screen that shows when you lose. When it's game over, I can start the game again with "enter". But when i do that, the start screen shows up for a short moment before it goes to the playable game. I want to go directly to the playable game without showing the start screen again when i start the game from the game over screen. How do i solve this problem?
Here it's the game and down below it's my code for the subworld class.
Lab 6 – Snake
import greenfoot.*;
import java.util.List;
public class Board extends World {
public Snake[] snake;
private Score thisScore; // store the reference to Score
private Item item;
private Item2 item2;
private static int world;
private int endGame;
int score; // For counting to add gold item
public Board()
{
super(500 , 500, 1);
Greenfoot.setSpeed(35);
addObject(new Score("start"), getWidth()/2, getHeight()/2); // Start screen
world = 0;
endGame = 0;
}
public void act()
{
//(Greenfoot.getKey()!=null)
if(world==0){
if(Greenfoot.isKeyDown("enter"))
world = 1;
return;
}
else if(world==1)
{
removeObjects(getObjects(Score.class)); // Remove the start screen
snake = new Snake[3];
snake[0] = new Snake("head");
snake[1] = new Snake("body");
snake[2] = new Snake("body");
addObject(snake[0], 110, 50);
addObject(snake[1], 90, 50);
addObject(snake[2], 70, 50);
addObject(new Wall(), 150, 381);
addObject(new Wall(), 350, 119 );
thisScore = new Score();
addObject(thisScore, 130, 5);
world=2;
}
else if(world==2)
{
addItem();
}
else
{
restartGame();
}
}
public void addArray()
{
Snake[] newSnake = new Snake[snake.length +1]; // Makes a new snake array with the same length + 1.
System.arraycopy(snake, 0, newSnake, 0, snake.length); // Copy the old snake array to the snake array
snake = newSnake;
score();
}
public void addItem() // If there is no item in the world call update method
{
List<Item> item = getObjects(Item.class);
if (item.isEmpty())
{
update();
}
}
public Score getScore() // retrieve the value of "theCounter" so it can be accessed by the rocket
{
return thisScore;
}
private void score() // Counter to add a gold item everytime 10 object has been eaten
{
score++;
if(score==10)
{
update2();
score=0;
}
}
private void update() // Add steel item to the world att random location
{
item = new Item();
addObject(item,Greenfoot.getRandomNumber(24)*20+10,Greenfoot.getRandomNumber(24)*20+10);
}
private void update2() // Add gold item to the world att random location
{
item2 = new Item2();
addObject(item2,Greenfoot.getRandomNumber(24)*20+10,Greenfoot.getRandomNumber(24)*20+10);
}
public void endGame()
{
if (endGame == 0){
endGame=1;
removeObjects(getObjects(Score.class));
removeObjects(getObjects(Item.class));
removeObjects(getObjects(Item2.class));
removeObjects(getObjects(Wall.class));
addObject(new Score("gameover"), getWidth()/2, getHeight()/2); // Game over screen
world=3; // make addItem() inactive and make restartGame() active
}
}
public void restartGame()
{
if (Greenfoot.isKeyDown("enter") && endGame==1) {
Board board = new Board();
world=1; // Make the game skip startscreen when starting the game from game over screen.
Greenfoot.setWorld(board);
}
}
}
