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

2016/3/30

Game Over Screen

1
2
Sir_brandon_ Sir_brandon_

2016/3/30

#
Can someone plz tell me how to create a game over screen for when both of my players die. Also, can someone put in step by step I'm kinda new with this
danpost danpost

2016/3/30

#
I will presume that when both players die that there will no longer be ANY players in the world. You can use this condition to trigger the game over screen being added to the world. If the scenario is to continue running after the game over screen is added to the world, then a check that one of its class is not in the world should also be used as part of the condition. If the scenario is stopped (by using the Greenfoot class method 'stop'), then you may want to override the 'started' method of the World class to perform these checks again so that the scenario is not re-started after it is programmatically stopped:
1
2
3
4
public void started()
{
    if ( ! getObjects(GameOver.class).isEmpty() ) Greenfoot.stop();
}
Sir_brandon_ Sir_brandon_

2016/3/30

#
Where would i put this code the code you just gave me
danpost danpost

2016/3/30

#
The code I gave was only secondary -- and only if you stop the scenario when you display the game over screen. You need to show the class you will be using for the game over screen before any specific help can be given.
Sir_brandon_ Sir_brandon_

2016/3/30

#
ok but i dont have a class for a gameover screen so how do i do that?
danpost danpost

2016/3/30

#
First you need to decide if you want to create an Actor object to display a game over image or if you want a different World object to do it with its background (or with an Actor object to do it there). Then you need to create a subclass of whichever one you have chosen (World or Actor). Then you would assign or build an image for an object of that class to display. Once the class is coded, post its code so we can proceed from there.
Sir_brandon_ Sir_brandon_

2016/3/30

#
ok here is the code i went with a new world:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import greenfoot.*;
 
/**
 * Write a description of class GameOverScreen here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class GameOverScreen extends World
{
 
    /**
     * Constructor for objects of class GameOverScreen.
     *
     */
    public GameOverScreen()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
 
        prepare();
    }
 
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Back_button back_button2 = new Back_button();
        addObject(back_button2, 294, 333);
        GameOverText gameovertext = new GameOverText();
        addObject(gameovertext, 309, 175);
        gameovertext.setLocation(300, 176);
    }
}
What do i do from here?
danpost danpost

2016/3/30

#
Now, your main world class needs to ask if no players are in the world -- and if there are none, create and set a new GameOverScreen world active. This would go in your main world act method.
Sir_brandon_ Sir_brandon_

2016/3/30

#
ok whats the line of code? because i dont know it. is it if (players = !null) or something like that
danpost danpost

2016/3/30

#
if (players = !null)
(a) 'players' is not defined (b) '=' is an assignment operator (not a comparison operator, use '==' to compare values) (c) 'null' is not a boolean value and cannot be negated using '!' There is a World class method you can use to get a list of objects of a specified type (refer to the World class API documentation). There is also are List class methods you could use to get the number of elements in a list or to let you know if the list contains any elements at all or not (refer to the List class API documentation).
Sir_brandon_ Sir_brandon_

2016/3/30

#
i Cant access
danpost wrote...
Edit: Can you plz tell me the line or lines of code i need.
danpost danpost

2016/3/30

#
Sir_brandon_ wrote...
i Cant access
List class API documentation
The List class has one method called 'size' that requires no parameters. It returns the number of elements in a List object. The List class has another method called 'isEmpty' that also requires no parameters. It returns a boolean value indicating whether the list is empty or not.
Sir_brandon_ Sir_brandon_

2016/3/30

#
also what is the code that asks if any players in he world. could you plz give me an example? im refering to this you posted earlier:
DanPost wrote...
Now, your main world class needs to ask if no players are in the world -- and if there are none, create and set a new GameOverScreen world active. This would go in your main world act method.
danpost danpost

2016/3/30

#
Sir_brandon_ wrote...
also what is the code that asks if any players in he world. could you plz give me an example?
I gave similar code to that above.
Sir_brandon_ Sir_brandon_

2016/3/30

#
do you mean
1
2
3
4
public void started()
{
    if ( ! getObjects(GameOver.class).isEmpty() ) Greenfoot.stop();
}
There are more replies on the next page.
1
2