This site requires JavaScript, please enable it in your browser!
Greenfoot back
Malmotri
Malmotri wrote ...

2012/11/26

Avoid start screen

Malmotri Malmotri

2012/11/26

#
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);
        }
    }
}
danpost danpost

2012/11/27

#
Put the condition 'if (world == 0)' on line 18 and remove line 19. Let me know if it did what you wanted.
Malmotri Malmotri

2012/11/27

#
I change the code now but it's not showing the start screen att all now.
public Board()
    {
        super(500 , 500, 1);
        Greenfoot.setSpeed(35);

        if(world==0){     
            if(Greenfoot.isKeyDown("enter"))
                                            addObject(new Score("start"), getWidth()/2, getHeight()/2); // Start screen
               world = 1;

        }

        endGame = 0;
    }
danpost danpost

2012/11/28

#
Remove line 7. (how did that sneak in there) Looks like line 9 found its way in there also (remove it).
Malmotri Malmotri

2012/11/28

#
Some other thing is wrong wrong now. This shows up java.lang.ArrayIndexOutOfBoundsException: 3 at Snake.act(Snake.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203) I uploaded this scenario so you you can see whats wrong. I would be very grateful. http://www.greenfoot.org/scenarios/6773 This is how i did it.
public Board()
    {
        super(500 , 500, 1);
        Greenfoot.setSpeed(35);
        if(world==0){   
        addObject(new Score("start"), getWidth()/2, getHeight()/2); // Start screen
        } 
        endGame = 0;
    }

    public void act()
    {
            //(Greenfoot.getKey()!=null)
        
        if(Greenfoot.isKeyDown("enter")){
            world = 1;
            return;
        }
You need to login to post a reply.