# Create a simple timer in cake defender

**URL:** <https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612>\
**Category:** Unit and Lesson Discussion\
**Tags:** csd-unit-3, csd-unit-3-lesson-20\
**Created:** [February 22, 2018, 2:48pm UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612 "2018-02-22T14:48:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![painterc](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@painterc](https://forum.code.org/u/painterc)\
**Post date:** [February 22, 2018, 2:48pm UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/1 "2018-02-22T14:48:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![karenma](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/karenma/32/4924_2.png) [@karenma](https://forum.code.org/u/karenma)\
**Post date:** [February 23, 2018, 4:54am UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/2 "2018-02-23T04:54:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![painterc](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@painterc](https://forum.code.org/u/painterc)\
**Post date:** [February 23, 2018, 12:54pm UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/3 "2018-02-23T12:54:54Z")

</div>

Thank you 😁 I appreciate the help!

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/josh/32/868_2.png) [@josh](https://forum.code.org/u/josh)\
**Post date:** [February 23, 2018, 4:25pm UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/4 "2018-02-23T16:25:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![painterc](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@painterc](https://forum.code.org/u/painterc)\
**Post date:** [February 23, 2018, 8:56pm UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/5 "2018-02-23T20:56:02Z")

</div>

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.

> **[Check out what I made](https://studio.code.org/projects/gamelab/ztpc2eqPTKxdwBNtT_OUBWEP8hXruFkBIDJaHIUkM70)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/josh/32/868_2.png) [@josh](https://forum.code.org/u/josh)\
**Post date:** [February 23, 2018, 9:04pm UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/6 "2018-02-23T21:04:27Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![painterc](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@painterc](https://forum.code.org/u/painterc)\
**Post date:** [February 24, 2018, 1:07am UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/7 "2018-02-24T01:07:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jold](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jold/32/5811_2.png) [@jold](https://forum.code.org/u/jold)\
**Post date:** [February 27, 2018, 5:43pm UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/8 "2018-02-27T17:43:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![painterc](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@painterc](https://forum.code.org/u/painterc)\
**Post date:** [February 27, 2018, 6:57pm UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/9 "2018-02-27T18:57:48Z")

</div>

> **[Check out what I made](https://studio.code.org/projects/gamelab/ztpc2eqPTKxdwBNtT_OUBWEP8hXruFkBIDJaHIUkM70)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![painterc](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@painterc](https://forum.code.org/u/painterc)\
**Post date:** [February 27, 2018, 7:00pm UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/10 "2018-02-27T19:00:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jold](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jold/32/5811_2.png) [@jold](https://forum.code.org/u/jold)\
**Post date:** [February 27, 2018, 7:20pm UTC](https://forum.code.org/t/create-a-simple-timer-in-cake-defender/14612/11 "2018-02-27T19:20:18Z")

</div>

@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.

```auto
//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.
    }

}
  

```
