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

2016/10/14

adding time limit to my game

NAMYA1403 NAMYA1403

2016/10/14

#
hey guys i want a timer/reversed count down on the top right corner of the world which if reaches zero the game should stop and show game over code--
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
/**
 * Write a description of class MeteoritePopOut here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MeteoritePopOut extends World
{
    Counter counter = new Counter();
    private int timer = 360;
public void act()
    {
        if (timer>3600)
        {
            timer--;
            if(timer == 0) Greenfoot.stop();
        }
    }

}
danpost danpost

2016/10/14

#
NAMYA1403 wrote...
hey guys i want a timer/reversed count down on the top right corner of the world which if reaches zero the game should stop and show game over < Code Omitted >
For the condition on line 19 to be true, the value of 'timer' must be '1' before line 18. This value is less than '3600', so the condition on line 16 must then be false. As well as this, the value is initialized to '360' which is already less than '3600'. This means that the game will never stop with this code. On top of all this, the code given cannot be the entire class code (although, you have made it appear that way). I am not sure why you have a condition on the running of the timer to begin with. There does not appear to be any reason for it. The Counter object (referenced by 'counter' on line 12) is not being used at all within the code, making it quite pointless. If this is an object to display the timer value, then, usually, the counter value itself would be the timer, not another value declared outside of the Counter class. Although, I guess you could use the class by just calling a 'setValue' method to update its image (or the value displayed within the image). The object would still need to added into the world and updated within the act method each time the timer drops how ever many units (act cycles) you require per change. You would use something like the following:
counter.setValue(timer/60);
NAMYA1403 NAMYA1403

2016/10/15

#
so shall i delete the whole code what i inserted heres the whole class code--
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
/**
 * Write a description of class MeteoritePopOut here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MeteoritePopOut extends World
{
    Counter counter = new Counter();
    private int timer = 360;
    /**
     * Constructor for objects of class MeteoritePopOut.
     * 
     */
    public MeteoritePopOut()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        
        
        prepare();
    }
    
    public Counter getCounter()
    {
        return counter;
    }
    

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        
       addObject(counter, 100, 40);
        meteorite[] meteorites = new meteorite[100];
        for(int i=0; i<meteorites.length; i++)
        {
            meteorites[i] = new meteorite();
            int meteoriteX = Greenfoot.getRandomNumber(getWidth());
            int meteoriteY = Greenfoot.getRandomNumber(getHeight());
            addObject(meteorites[i], meteoriteY, meteoriteX);
        }
        

        addObject(new meteorite(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        rocket rocket = new rocket();
        addObject(rocket,526,341);
       
    }
    
    private int score = 0; //declare 'score' as private
 
    public int getScore() //to be able to access this field from outside this class
     {
    return score;
    }
    
    public void act()
    {
        if (timer>3600)
        {
            timer--;
            if(timer == 0) Greenfoot.stop();
        }
    }

}
danpost danpost

2016/10/16

#
NAMYA1403 wrote...
so shall i delete the whole code what i inserted
Not all of it. What you should remove is lines 66, 67 and 71 (at least, to start). At least your timer will run, then (and the scenario will stop). You may then want to increase the initial value of 'timer' on line 13 (so the scenario does not stop too soon). The only thing left -- is whether you want to use the Counter (instead of the 'timer' field) or just use a Text type class to display a representation of the decreasing time (unless I am mistaken as to why you have included a Counter object in the class)..
NAMYA1403 NAMYA1403

2016/10/16

#
counter was not for this timer thing, it was for the score counter thing and is not releated to it
You need to login to post a reply.