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

2012/2/26

Making the classic "Game Over" screen...

1
2
Omniscience Omniscience

2012/2/26

#
Well, I'm really tired right now, and I just can't seem to make one of the Game Over screens that are on everyone's games. I have tried making a rectangle, to which I will add some text. The code below has been inserted into the Act statement of an Actor subclass imaginatively named "GameOver". When I run the simulation, nothing appears; don't know the reason why: if (getWorld().getObjects(car.class).isEmpty()) { GreenfootImage gameOver = new GreenfootImage(100, 100); gameOver.setColor(java.awt.Color.RED); gameOver.fill(); setImage(gameOver); } As always, would really appreciate the help, especially if someone shows me a good technique to create these Game Over screens.
Duta Duta

2012/2/27

#
Before I try to work it out, have you got a call to setPaintOrder(...) in your world's constructor? If so you might have forgotten to include the new GameOver.class in it, meaning it gets painted under everything else - I did this once before.. Also, why only 100x100? Surely new GreenfootImage(getWorld().getWidth(), getWorld().getHeight()) would be better? Of course I don't know your game so you might be right
danpost danpost

2012/2/27

#
What I probably would do, is create a new class called 'GameOver'
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
import Greenfoot.*;
import java.awt.Color;
 
public class GameOver extends Actor
{
    String msgTxt = "GAME OVER"
 
    public GameOver()
    {
        updateImage();
    }
 
    public GameOver(String txt)
    {
        msgTxt = txt;
        updateImage();
    }
 
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(getWorld().getWidth(), getWorld().getHeight()); // or whatever size you want the object
        image.setColor(Color.cyan); // color of your choice
        image.fill();
        GreenfootImage txtImg = new GreenfootImage(msgTxt, 36, Color.black, new Color(0, 0, 0, 0)); // colors and font size of your choice
        image.drawImage(txtImg, (image.getWidth() - txtImg.getWidth()) / 2, (image.getHeight() - txtImg.getHeight) / 2);
        // whatever else you might want to do for the image
        setImage(image);
    }
}
I added the second constructor, in case you wanted to display a different text.
danpost danpost

2012/2/27

#
Your world act() method should have the code
1
2
3
4
5
6
if (getObjects(Car.class).isEmpty())
{
    addObject(new GameOver(), getWidth() / 2, getHeight() / 2);
    Greenfoot.stop();
    return;
}
davmac davmac

2012/2/27

#
What I probably would do, is create a new class called 'GameOver'
danpost, I think you should re-read the original post! :)
The code below has been inserted into the Act statement of an Actor subclass imaginatively named "GameOver".
Omniscience Omniscience

2012/2/27

#
Thanks danpost, and all who replied to me! And davmac, since you replied most recently, do you have any idea how I can make the backcolour of the rectangle translucent? Anyway, its not a big deal, but if you could responnd to me, that'd be great! :)
Omniscience Omniscience

2012/2/27

#
Or danpost, if you're available!
danpost danpost

2012/2/27

#
Use: new Color(0, 0, 0, 0)
davmac davmac

2012/2/27

#
do you have any idea how I can make the backcolour of the rectangle translucent?
Sure. Check out the constructors for the Color class. Some of the constructors take three values - one for the red, one for the green, and one for the blue components of the desired color. Other constructors add a fourth value - the "alpha" - which controls the transparency of the color. To make a translucent version of an existing color, you could do something like:
1
2
Color translucentRed = new Color(Color.RED.getRed(), Color.RED.getGreen(), Color.RED.getBlue(), 100);
gameOver.setColor(translucentRed);
(Make sure you "import java.awt.Color;" at the top of your source).
danpost danpost

2012/2/27

#
@davmac, thanks again. I read 'translucent' as 'transparent'! Not quite the same thing, huh. :fp
danpost danpost

2012/2/27

#
I think replacing lines 22 and 23 of the code I provided with
1
2
3
4
5
GreenfootImage back = new GreenfootImage(getWorld().getWidth(), getWorld().getHeight()); // size of the object
back.setColor(Color.cyan); // color of your choice
back.fill();
back.setTransparency(100); // alpha value of choice
image.drawImage(back, 0, 0);
would work also.
Omniscience Omniscience

2012/2/27

#
Hey, danpost, incorporated the code, and did see pleasant results. However, I'm suddenly being thrown the ole NullPointerException, listed as shown:
1
2
3
4
java.lang.NullPointerException
    at GameOver.<init>(GameOver.java:9)
    at LevelOne_Countryside.act(LevelOne_Countryside.java:130)
    at greenfoot.core.Simulation.actWorld(Simulation.java:513)
The compiler doesn't seem to like this bit of code, more accurately when getWorld() is used. I'm sure the world still exists though when GameOver.class is added to the world, hence why I'm confused by the NullPointerException:
1
2
3
4
5
6
7
8
9
10
11
12
private void updateImage()
   {
       GreenfootImage back = new GreenfootImage(getWorld().getWidth(), getWorld().getHeight());   // or whatever size you want the object
       GreenfootImage gameOver = new GreenfootImage(getWorld().getWidth(), getWorld().getHeight());// size of the object 
       back.setColor(Color.magenta);
       back.fill(); 
       back.setTransparency(60);
       gameOver.drawImage(back, 0, 0); 
       GreenfootImage txtImg = new GreenfootImage(msgTxt, 60, Color.black, new Color(0, 0, 0, 0));
       gameOver.drawImage(txtImg, (gameOver.getWidth() - txtImg.getWidth()) / 2, (gameOver.getHeight() - txtImg.getHeight()) / 2);
       setImage(gameOver);
   }
davmac davmac

2012/2/27

#
The compiler doesn't seem to like this bit of code, more accurately when getWorld() is used
Ok, first point is that the compiler doesn't throw exceptions - they happen at run time. So the problem is not that the compiler doesn't like the code, but that the code is doing something wrong :) Can you tell us which line of the updateImage() method (as posted above) corresponds to line 9 of the class (i.e. which line actually throws the exception)? Note that getWorld() returns the world the actor is in, not just "the world", so if the actor hasn't been inserted into the world yet then getWorld() will return null.
danpost danpost

2012/2/27

#
My bad! Remove all the 'getWorld()'s out of the updateImage() method of the 'GameOver' class and replace them with actual values. (Sorry)
Omniscience Omniscience

2012/2/27

#
@davmac You are right, but I wasn't speaking technically, just colloquially! Nonetheless, I liked your explanation, and will use it in my F453 OCR A Level Computing Exam come June ;) And you are right, of course, the GameOver.class can only be inserted at the end of the simulation, thus it is never inserted in the world in time enough to inherit its methods. NullPointerException understood.
There are more replies on the next page.
1
2