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

2013/5/31

Game Over

MBX5 MBX5

2013/5/31

#
ive finished my game off but i want to know how to add some sort of a game over showing the time that the player stayed alive what would be the best way to show this and how can i achieve this?
Gevater_Tod4711 Gevater_Tod4711

2013/5/31

#
In which part do you need help? The game over or just the displayed time?
MBX5 MBX5

2013/5/31

#
mainly the game over part but i want to show the time on that aswell
Gevater_Tod4711 Gevater_Tod4711

2013/5/31

#
Well for the game over you just need a class GameOver that is shown when the game ends. So when the game ends you execute this code:
//when the game ends:
getWorld().removeObjects(getWorld().getObjects(null));
getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);
//if you put this code in a world subclass you have to delete the getWorld(). parts;
For your counter: Do you want a counter like a clock that counts up and shows the time while the game is running or do you just want to show the time when the game ends.
MBX5 MBX5

2013/5/31

#
the counter has to count how long the player survives and it should stop when he is dead
Gevater_Tod4711 Gevater_Tod4711

2013/5/31

#
But do you want the counter to display the time while the game is running? If yes there would be a little clock counting up the time in your game.
MBX5 MBX5

2013/5/31

#
yea
Gevater_Tod4711 Gevater_Tod4711

2013/5/31

#
Ok then you need to declare a new class for the counter. The class should look like this:
import greenfoot.*;
import java.awt.Color;

public class Clock extends Actor {
    
    private long startingTime;
    
    private boolean counting = false;
    
    public void act {
        if (counting) {
            displayTime(System.currentTimeMillis() - startingTime);
        }
    }
    
    private void displayTime(long time) {
        time /= 1000;
        int minutes = time / 60;
        int seconds = time % 60;
        String time = minutes + ":" + seconds;
        setImage(new GreenfootImage(time, 30, Color.black, new Color(0, 0, 0, 0)));
    }
    
    public void startClock() {
        counting = true;
        startingTime = System.currentTimeMillis();
    }
    
    public String getTime() {
        time /= 1000;
        int minutes = time / 60;
        int seconds = time % 60;
        String time = minutes + ":" + seconds;
        return time;
    }
}
When you construct the level you need to add this code:
Clock clock = new Clock();
getWorld().addObject(clock, 50, 30);
clock.startClock();
When the game ends you can get the current time using the method getTime() in the clock class.
roeilthegreat roeilthegreat

2014/8/21

#
Hey, so sorry for reviving a year-old thread but when I tried to compile this errors keep popping up after I fix the previous ones. Also, I can't use the getTime() method. Not sure if anyone will reply, but it was a worth a shot.
NikZ NikZ

2014/8/21

#
What are the errors?
danpost danpost

2014/8/21

#
@roeilthegreat, replace line 30 with these two lines;
if (!counting) return "0:0";
int time = (System.currentTimeMillis()-startingTime)/1000;
I added the first line so that an outrageous String would not be returned if the timer was not running. The second line was changed to simulate what the act method does with 'displayTime'
You need to login to post a reply.