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

2011/4/21

Help game bar

jhadad jhadad

2011/4/21

#
Hi, i am making a game, and im trying to make it so when you have a key pressed, it will start a counter counting. also, when you press the key, a bar will come up on the screen that will keep extending as you hold it, up to a point, but if you let go before this point, the bar erases and the counter goes back to 0. Please help me with this Thanks John
davmac davmac

2011/4/23

#
Hi John, Lots of people would be willing to help you, but the way you've talked about your problem makes it hard. You really need to break it down into smaller parts and make it clear exactly what you're having problem with. For instance, do you know how to create a counter, generally? If not then that's probably the first question you should be asking. Davin
webmessia webmessia

2011/4/23

#
This may help slightly :P
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; //Java Color class
/**
 * Counts up in seconds and increases a bars height when
 * a key is pressed down. If the key is released the bar falls
 * back to zero. If the count reaches the duration the bar
 * and count remain there.
 * 
 * @author Michael Clapham
 * @version v1.0
 */
public class CounterBar  extends Actor
{
    private long startTime; //The system time we started counting in miliseconds
    private long count = -1; //The number of miliseconds since we started counting
    private long duration = 4000; //The number of miliseconds needed to fill the bar
    private boolean finished = false;//This is set to true once the bar reaches a certain length
    private int barMaxHeight = 80;//The max height the bar can reach
    private String keyString = "a";//The key that is pressed down to increase the count
    private Color barColor = Color.RED;
    private Color textColor = Color.MAGENTA;
    
    /**
     * Constructor for CounterBar objects. This paints the bar when it is created
     */
    public CounterBar(){
        paint();
    }
    
    /**
     * Act - do whatever the CounterBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.isKeyDown(keyString)){
            if(count == -1){
                //If the count is -1 we have only just started counting so we take the
                //start time in MILISECONDS
                startTime = System.currentTimeMillis();
            }
            //We set the count to the difference between the current time and the start time
            count = System.currentTimeMillis() - startTime;
        } else {
            count = -1;
        }
        paint();
    }    
    
    /**
     * Paints the bar and counter
     */
    public void paint(){
        //Creates a new GreenfootImage with width 20 and height 100
        GreenfootImage img = new GreenfootImage(20,100);
        
        //Calculates the height of the bar
        int barHeight = (int) (barMaxHeight * count / duration);
        //If the height is greater than the max height we are finished
        if(barHeight > barMaxHeight-2){
            finished = true;
        }
        if(finished){
            //If we are finished we draw the bar filled
            img.setColor(barColor);
            img.fillRect(0,0,20,barMaxHeight);
            //And the duration in seconds is drawn as the timer text
            img.setColor(textColor);
            img.drawString("" + (duration/1000), 8, 90);
        } else {
            //Else we draw the bar starting in the top left corner (0,0) with a width of 20
            //and a height that we calculated before.
            img.setColor(barColor);
            img.fillRect(0,0,20,barHeight);
            //We then draw the count divided by 1000 to give it in seconds rather
            //than miliseconds
            img.setColor(textColor);
            img.drawString("" + (count/1000), 4, 90);
        }
        //We then set the image we just painted on, to be the Actors image.
        setImage(img);
    }
}
You need to login to post a reply.