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

2015/5/9

Timer that goes up in seconds!!

1
2
Adam2323 Adam2323

2015/5/9

#
Hi. I am making a game where the player is a rocket and you have to dodge asteroids. How do I make a timer that goes up in seconds to see how long the player survives? There is a bit more information that might make this difficult. I have 3 different levels, easy, medium and hard. When the player dies, there is a game over screen and a button. When the button is pressed, it goes back onto the level chooser. I NEED HELP!!
danpost danpost

2015/5/9

#
You have left out some important information: * Are the three levels (easy, medium and hard) different worlds? -- if so, do they all subclass World or is there an intermediate World subclass that they all extend? * Why is there no code given? The code for the class(es) with the timer (with timer display actor) and level fields should be given. * Is(are) your world(s) bounded or not? Show the world super constructor call statement(s). * Are you implementing a scrolling system? (would be important to keep the timer display location static).
Adam2323 Adam2323

2015/5/10

#
The three levels are different Subclasses of World, I gave no code because I don't know what code to give, what do you mean by bounded? (I am new to Greenfoot.) No, I am not implementing a scrolling system.
Adam2323 Adam2323

2015/5/10

#
I don't currently have Timer class...
danpost danpost

2015/5/10

#
Adam2323 wrote...
I don't currently have Timer class...
You could use any one of a number of class types for the timer or use the 'setText' method on the world to display the timer text. The most versatile Actor subclass that can be used is the one that is simplest. The entire class code is here:
1
public class SimpleActor extends greenfoot.Actor {}
It can be used for Counter, Text, Message, Lives, Health, Timer, and many other applications. A Timer class is just a Text class with a time field whose value is controlled within the class. The time field is basically an int counter field. A Text class is just an Actor class whose actors have images containing rendered text. Most times the actions of these type actors are controlled from outside the class; so, instead of telling the actor class to do something, we are just doing it straight away. So basically, you create an instance of the SimpleActor class, give it a name that describes what it is (assign it to a field). For example, see line 3 below:
1
2
3
4
5
public class MyWorld extends World
{
    private Actor timerDisplay = new SimpleActor(); // to display the timer text
    private int timeElapsed; // to count sets of act cycles
    private int timeCounter; // to count act cycles in a single set
The timeCounter will be incremented every act cycles and checked for some nominal value (like 55, which is around the standard number of act cycles per second in scenarios running at standard speed). Each time that value is reached the timeElapsed field will be incremented and the timeCounter field will be zeroed. Also, the timerDisplay actor will be given a new image to display.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// in 'act' method of world
timeCounter = (timeCounter+1)%55;
if (timeCounter == 0)
{
    timeElapsed++;
    updateTimerDisplay();
}
 
// add the following method to world class code
/** assigns a new image to the 'timerDisplay' actor */
private void updateTimerDisplay()
{
    timerDisplay.setImage(new GreenfootImage("Time: "+timeElapsed, 24, null, null));
}
I put the code to give the timer display actor its image in a separate method because you may wish to initialize the image in the world constructor before adding it into the world:
1
2
updateTimerDisplay(); // initializes image
addObject(timerDisplay, 80, 20); // adds to world
Adam2323 Adam2323

2015/5/10

#
This is the code in my "easy" World. How do I make the timer appear in my world? By the way, thank you for you help so far :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import greenfoot.*;
public class AvoiderWorldEasy extends World
{
    private Actor timerDisplay = new SimpleActor();
    private int timeElapsed;
    private int timeCounter;
    public AvoiderWorldEasy()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);
        prepare();
        act();
        endGame1();
        updateTimerDisplay();
        addObject(timerDisplay, 80, 20);
    }
 
    public void act()
    {
        if(Greenfoot.getRandomNumber(1000) < 20)//This is to create an endless horde os enemies
        {
            Enemy e = new Enemy();
            addObject(e, Greenfoot.getRandomNumber(getWidth()-20)+10, -30);
        }
        timeCounter = (timeCounter+1)%55;
        if (timeCounter == 0)
        {
            timeElapsed++;
            updateTimerDisplay();
        }
    }
 
    private void updateTimerDisplay()
    {
        timerDisplay.setImage(new GreenfootImage("Time: "+timeElapsed, 24, null, null));
    }
 
    public void endGame1()//when the Actor dies, the Game Over screen will appear
    {
        AvoiderGameOverScreen go1 = new AvoiderGameOverScreen();
        Greenfoot.setWorld(go1);
    }
     
    private void prepare()
    {
        AvatarE avatare = new AvatarE();
        addObject(avatare, 309, 341);
    }
}
Adam2323 Adam2323

2015/5/10

#
When I play, I can't see the timer.
danpost danpost

2015/5/10

#
Adam2323 wrote...
How do I make the timer appear in my world?
Remove lines 12 and 13. The 'act' methods are called repeatedly by the greenfoot framework when the scenario is running and the 'endGame1' should be called by the player actor when it dies.
danpost danpost

2015/5/10

#
Adam2323 wrote...
When I play, I can't see the timer.
What color is the background of your world? NVM (rockets and asteroids -- I guessing dark). Change 'null, null' in the 'updateTimerDisplay' method to 'java.awt.Color.white, null'.
Adam2323 Adam2323

2015/5/10

#
THANK YOU SO MUCH! This really helped me out.
Adam2323 Adam2323

2015/5/10

#
One last question- Will I need to crate a new timer for each level, or can I just use the one?
Adam2323 Adam2323

2015/5/10

#
Oh and also, how can I show the player how long (s)he lasted in the game over screen?
Adam2323 Adam2323

2015/5/10

#
(In the game over world, how can I put something like: You survived x seconds... Play Again?)
danpost danpost

2015/5/10

#
Adam2323 wrote...
Will I need to crate a new timer for each level, or can I just use the one?
You can create a new one in each level world.
how can I show the player how long (s)he lasted in the game over screen?
Add a method to the AvoiderGameOverScreen class code to create and show the final survival time.
1
2
3
4
5
6
public void showSurvivalTime(int time)
{
    // create a SimpleActor
    // give it its image
    // add it to world
}
Then, in the middle of your 'endGame1' method in the AvoiderWorldEasy class, insert the following line:
1
go1.showSurvivalTime(timeElapsed);
Adam2323 Adam2323

2015/5/10

#
Okay... What code do I put into the "public void showSurvivalTime(int time)"?
There are more replies on the next page.
1
2