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

2017/7/10

timer for a game

kiks4 kiks4

2017/7/10

#
i need a timer that starts once the play button is selected and it goes until 59 minutes
Super_Hippo Super_Hippo

2017/7/10

#
import greenfoot.*;
public class Timer extends Actor
{
    private int time = 0;
    
    public Timer()
    {
        updateImage();
    }
    
    public void act()
    {
        if (++time % 55 == 0) updateImage();
    }
    
    public void updateImage()
    {
        GreenfootImage img = new GreenfootImage(125, 30);
        img.setColor(Color.GREEN);
        img.fill();
        img.setColor(Color.RED);
        int minutes = time/55/60;
        String pre = minutes<10 ? "0" : "";
        String min = pre+minutes;
        int seconds = (time-minutes*55*60)/55;
        pre = seconds<10 ? "0" : "";
        String sec = pre+seconds;
        img.setFont(new Font("Courier", 20));
        img.drawString("Time: " + min + ":" + sec, 10, 23);
        
        
        if (minutes == 60) Greenfoot.stop();
        else setImage(img);
    }
}
kiks4 kiks4

2017/7/10

#
what do i need for this?
Super_Hippo Super_Hippo

2017/7/10

#
Nothing, you only need to add a Timer object into the world. Well, you need Greenfoot version 3.1.0 or you need to import the java.awt Color and Font classes.
You need to login to post a reply.