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

2012/11/25

Mutliple random placements

1
2
Wavesludge Wavesludge

2012/11/28

#
I'm all done with the scenario, but I want a GameOver screen that shows your score and says "Game Over". I have looked at other topics and scenarios, but I couldn't get it to work in mine, so any help would be appreciated.
Gevater_Tod4711 Gevater_Tod4711

2012/11/28

#
first you need a condition when the game over screen should be printed on the screen. Then you only have to submit the values to the game over class and print them onto an image. But to help you getting the values I need to know in which classes the values are you want to print on the game over image.
Wavesludge Wavesludge

2012/11/28

#
I want to print the Score, I have a Score class to handle that.
Gevater_Tod4711 Gevater_Tod4711

2012/11/28

#
So all variables and values are in this score class. Ok then its easy but I forgot to ask if the variables are declared static. If they are it's easyer but it'll work anyway.
Wavesludge Wavesludge

2012/11/28

#
I don't really understand if it's static or not but here's my Score class:
import greenfoot.*;
import java.awt.Color;

public class Score extends Actor
{
    private int totalScore = 0;

    public Score()
    {
        setImage(new GreenfootImage(200, 30));
        GreenfootImage img = getImage();
        img.setColor(Color.BLACK);
        img.drawString("Score " , 30, 3);
    }
        
    public void addScore(int number)
    {
        totalScore += number;
        GreenfootImage img = getImage();
        img.clear();
        img.drawString("Score " + totalScore, 30, 30);
 
    }
    
    public void act() 
    {
        
    }    
}
Then i have this in my World:
import greenfoot.*;
import java.awt.Color;
import java.util.List;  

public class BG extends World
{
    private Score thisScore;
    
    public BG()
    {    
        super(800, 500, 1); 
        
        thisScore = new Score();
        addObject(thisScore, 75, 2);
Gevater_Tod4711 Gevater_Tod4711

2012/11/28

#
The variable is non-static. If it would be static it would be: private static int totalScore = 0; instead of private int totalScore = 0; First you need to add some new methods to your Score and BG class:
//add this method to your Score class;
public int getScore() {
    return totalScore;
}

//and add this method to your BG class;
public Score getScore() {
    return thisScore;
}
Now you can easily get the value of the totalScore int into your game over class:
//add this code to your gameOver class;

private int finalScore;

public GameOver() {
    finalScore = ((BG) getWorld()).getScore().getScore();//maybe you should give one of the methods another name because this is a little confusing;
    //now you just have to print this value onto your image using getImage().drawString(...);
    getImage().drawString("Final Score: " + finalScore, x, y);//x and y are the coordinates of the string on your image;
}
If you don't know the method drawString the Greenfoot API should be helpfull.
Wavesludge Wavesludge

2012/11/28

#
I've added everything in the places you said, but when it's game over, the scenario just stops. No text appears.
Gevater_Tod4711 Gevater_Tod4711

2012/11/28

#
have you added the gameOver object and have you done this before Greenfoot.stop()?
Wavesludge Wavesludge

2012/11/28

#
I have made the class, but when I try to add it in the world I get this error: java.lang.NullPointerException at GameOver.<init>(GameOver.java:9) at BG.<init>(BG.java:26)
Gevater_Tod4711 Gevater_Tod4711

2012/11/28

#
could you show me your source code. That would make it much easyer to help you.
Wavesludge Wavesludge

2012/11/28

#
I uploaded my scenario: My Snake
Gevater_Tod4711 Gevater_Tod4711

2012/11/29

#
I can't get the source of your scenario can you publish it?
Wavesludge Wavesludge

2012/11/29

#
Oh, must've been a mess up with the update, I'll fix it.
Gevater_Tod4711 Gevater_Tod4711

2012/11/30

#
ok I corected that. you just need to change some classes:
//in your obstacle class;
//change the void hit like this;
public void hit()
    {
        Actor Snake = getOneIntersectingObject(Snake.class);
        if (Snake !=null )
        {
            World background = getWorld();
            int score = ((BG) background).getScore().getScore();
            getWorld().addObject(new GameOver(score), 400, 250);
            Greenfoot.stop();
        }
    }
//add the class GameOver with only a constructor;
public GameOver(int score) {
        getImage().clear();
        getImage().scale(100, 50);
        getImage().drawString("Total Score: " + score, 10, 40);
    }
and then it should work
Wavesludge Wavesludge

2012/12/9

#
Thanks!
You need to login to post a reply.
1
2