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

2013/7/15

Can we display timer in a game?

1
2
Kaiwalya Kaiwalya

2013/7/15

#
I am making a simulation for a rocket launch , i want to show a countdown and after the countdown, the rocket will move towards the sky. If its possible can u pls tell me how??
darkmist002 darkmist002

2013/7/15

#
ya, i made one based on number of acts. basically after a certain number of acts it counts down again. here's what i did for the timer:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class Timer extends Actor
{
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int time = 20;
    private int count = 65;
    public void act() 
    {
        // Add your action code here.
        if(time == 0)
        {
            return;
        }
        
        if(counter())
        {
            time--;
            count = 65;
        }
        display();
    }    
    
    private boolean counter()
    {
        if(count > 0)
        {
            count--;
        }
        return count == 0;
    }
    
    private void display()
    {
        setImage(new GreenfootImage("time left before next wave: " + time, 30, Color.WHITE, Color.BLACK));
    }
    
    public void setTime()
    {
        time = 20;
    }
    
    public boolean isTimeUp()
    {
        return time == 0;
    }
    
}
if you add that to the world class (addObject), you can see it countdown. change the time variable to a different number depending on how many seconds you want to count down. (for this it's 20) i guestimated about 65 acts in a second off of what i saw with the computer i'm on right now. basically it stops it when it hits 0, otherwise it counts from 20 to 0 taking off a second of the time every 65 acts.
danpost danpost

2013/7/15

#
You just need a simple Timer actor class. Even though it may seem logical to use the system time to run the timer, it would not be wise. If the scenario speed was altered far from a normal running speed, the running of the timer and the speed of the launch of the ship would be inconsistent with each other. Better is to count act cycles so that if the speed was fast the timer and launch would be accelerated; and, if the speed was slow, both again would be slowed. A simple Timer class follows:
/** This class creates a countdown timer object.
 * It displays like a digital timer clock.
 * The value to set the timer is given in seconds; however, is converted to approximate act cycles within the class.
 * All timers created from this class will start automatically unless using the 'Timer(int, boolean)' constructor with boolean set to 'false'.
 */
import greenfoot.*;
import java.awt.Color;

public class Timer extends Actor
{
    private int count; // the counter field
    private int initialCount; // the initial time given before event occurs
    private boolean running;

    public Timer()
    {
        this(0, true);
    }

    public Timer(int timeBeforeEvent)
    {
        this(timeBeforeEvent, true);
    }

    public Timer(int timeBeforeEvent, boolean getsStarted) // int value given in seconds
    {
        setTimer(timeBeforeEvent);
        updateImage();
        running = getsStarted;
    }

    public void setTimer(int timeBeforeEvent)
    {
        initialCount = 60 * timeBeforeEvent;
        count = -initialCount;
    }

    private void updateImage()
    {
        String prefix = "T - ";
        if (count >= 0) prefix = "T + ";
        int time = count * (int)Math.signum(count);
        time = time / 60;
        int secs = time % 60;
        time = (time - secs) / 60;
        int mins = time % 60;
        int hrs = (time - mins) / 24;
        String h = "00"+hrs;
        while (h.length() > 2) h = h.substring(1);
        String m = "00"+mins;
        while (m.length() > 2) m = m.substring(1);
        String s = "00" + secs;
        while (s.length() > 2) s = s.substring(1);
        String text = prefix + h + "h : " + m + "m : " + s + "s";
        GreenfootImage textImage = new GreenfootImage(text, 20, Color.black, new Color(0, 0, 0, 0));
        GreenfootImage image = new GreenfootImage(textImage.getWidth()+20, textImage.getHeight()+10);
        image.drawRect(0, 0, image.getWidth()-1, image.getHeight()-1);
        image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, (image.getHeight()-textImage.getHeight())/2);
        setImage(image);
    }

    public void act()
    {
        if (running)
        {
            count++;
            if ((count + initialCount) % 60 == 0) updateImage();
        }
    }

    public int getTime()
    {
        return count / 60;
    }

    public void start()
    {
        running = true;
    }

    public void stop()
    {
        running = false;
    }
}
darkmist002 darkmist002

2013/7/15

#
you can use the isTimeUp method to signal when to remove the timer/do whatever else you want to do after it hits zero.
danpost danpost

2013/7/15

#
danpost wrote...
A simple Timer class follows:
I was going to change this to: 'A modified Timer class follows'.
danpost danpost

2013/7/15

#
I corrected the 'getTime' method and added an 'isRunning' method:
// changed this method
public int getTime()
{
    return (count + initialCount) / 60 - initialCount / 60;
}
// added this method
public boolean isRunning()
{
    return running;
}
danpost danpost

2013/7/16

#
One more correction: line 47 should be
int hrs = (time - mins) / 60;
Kaiwalya Kaiwalya

2013/7/16

#
Well both didnt work and i am not understanding how to make it move automatically after pressing a button.
Kaiwalya Kaiwalya

2013/7/16

#
I mean to say that once there will be a countdown shown from 10 - 1 , a message in a huge letter should be displayed on the screen that (for eg.) "Press 'enter' to launch the rocket", and then after it exits the screen , the background should be changed is it possible?
danpost danpost

2013/7/16

#
Create a class called 'CountDown' with an int field called 'count' set to 10. Add another int field called 'timer' to regulate the counting down. In the constructor, call a method called 'updateImage' to create its image showing the value of 'count'. Each act, add one to the timer, when it hits some number (something like 60), reset it back to zero, decrement the 'count' field and update the image of the object. When the value of 'count' turns to zero change its image to the message text and wait for enter to be pressed. When enter is pressed (while count is zero), remove the object. In the class for the rocket, when a CountDown object is detected in the world, set a boolean called 'ready' to true. If that boolean is set to true and a CountDown object is no longer detected in the world, launch the rocket.
Kaiwalya Kaiwalya

2013/7/17

#
Will it count 10 seconds? And will it display the timer? And i dont know how to make it move upwards?
danpost danpost

2013/7/17

#
Yes, yes (if you add the object to the world), and ... how to make what (the rocket) move upwards?
Kaiwalya Kaiwalya

2013/7/18

#
yes the rocket
JetLennit JetLennit

2013/7/18

#
setLocation(getX(), getY() - 5);
Kaiwalya Kaiwalya

2013/7/19

#
danpost can u give me the coding of the class CountDown you told.
There are more replies on the next page.
1
2