Create a simple timer in cake defender


#1

I have a student who wants to create a simple timer that counts down 30 seconds on the cake defender game. Any suggestions that would work but only use the commands and skills they have learned thus far in this unit?


#2

I have forwarded your post to people that maybe able to help. I know this is possible.


#3

Thank you :grin: I appreciate the help!


#4

The simplest way to do this would be to use the World.seconds block, which returns the number of seconds since the game started. You could place that in a conditional in the draw loop to end the game after 30 seconds.


#5

I have added the link to me trying to add this element. I have tried several things that havent worked the way I wanted. This is the simplest version though of something that is not working for me.


#6

It looks like you’re only setting the var count to World.seconds once at the beginning of the game, so it will always stay at 0. You could actually get rid of the count variable entirely and just use World.seconds directly on line 116 - eg:

text("Timer:" + World.seconds, 20, 60, 200, 100);

#7

Thank you for your quick reply. Just out of curiosity, because I as well as this student will wonder. Why it would not work by setting World.seconds to a variable. We also tried setting the var count = World.seconds inside the draw loop but that didn’t seem to work either. We checked examples and created a very simple counter in just an open new project and it worked. However, it would just not work in the draw loop on this activity. Any ideas why?


#8

@painterc Its hard to tell without the seeing the code. It seems like you are on the right track conceptually. Post that code.


#9

#10

We have made several changes trying different things but ideally what we would like to happen. Is once the clock gets to a certain amount, then we want the game to end. Say after 30 seconds make the game stop and show the score


#11

@painterc

This is something that I worked with a lot of students on. Here is my general solution to have a game and then something else after the game is over. Next year, I’d really like to get the students to have a splash screen animation, the game, and then a whole other animation after the game is over.

//This variable controls whether the game is playing or not.
var playing = true ;


function draw()
{
    //Will execute if playing is true. Sometimes I write it as playing == true so students do not get confused.
    if (playing)
    {
          //Draw all of the stuff when the game is playing.
         // In here you need a conditional to change the state of the variable playing to false. In your case that is if the timer is 
        // greater than a certain value. 
    }
    else
    {
          //Draw all of the stuff after the game is over. 
         //The challenge here is to make some of the sprites visible and other not visible.
    }


}