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.


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() { } }
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);
//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; }
//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; }
//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); }