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

2014/1/29

How do i display my end score in my winner and game over screen

virginia001 virginia001

2014/1/29

#
I want to add my end score into my game over screen and winnerscreen...I've tried a few things with system out println but it didn't work though
Gevater_Tod4711 Gevater_Tod4711

2014/1/29

#
If you have the score in your world class you can write down the score on your game over screen using the drawString method of the GreenfootImage class
virginia001 virginia001

2014/1/29

#
I've tried this but i get an error at drawString(); saying cannot find symbol
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
public class GAMEOVERSCHERM extends World
{
 
    /**
     * Constructor for objects of class GAMEOVERSCHERM.
     *
     */
 
    public GAMEOVERSCHERM()
    {   
        super(400,300,1);
        Greenfoot.playSound("fail-trombone-03.mp3");
 
        addObject(new TRYAGAIN(), 200,80);
         
        endScore();
 
    }
     
    public void endScore()
    {
        Vracht vracht = new Vracht();
        Counterve counterve  = new Counterve();
        drawString();
    }
}
danpost danpost

2014/1/29

#
I prefer to create a GreenfootImage object using the constructor that requires your String text and either use it directly as the image of an actor or draw this new image onto the world background or onto the image of an actor. For example (without creating a new World subclass for the game over screen), you could code this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// previously declared and used instance field
int score;
// for game over world
int wide = getWorld().getWidth() * getWorld().getCellSize();
int high = getWorld().getHeight() * getWorld().getCellSize();
World goWorld = new World(wide, high, 1){};
GreenfootImage bg = goWorld.getBackground();
bg.setColor(java.awt.Color.lightGray);
bg.fill();
String text = "Game Over\n\nScore: "+score;
GreenfootImage textImage = new GreenfootImage(text, 84, java.awt.Color.orange, null);
bg.drawImage(textImage, (bg.getWidth()-textImage.getWidth())/2, (bg.getHeight()-textImage.getHeight())/2);
Greenfoot.setWorld(goWorld);
Greenfoot.stop();
Line 12 could be removed and replace with the following lines to use an actor:
1
2
3
Actor textActor = new Actor(){};
textActor.setImage(textImage);
goWorld.addObject(textActor, wide/2, high/2);
You need to login to post a reply.