 Okay, hey everybody. This is one of the most common questions I get on my YouTube channel This is how do I create a timer for my game? So I'm gonna go ahead and show you how to do that. So let me talk real quick about the code you see on the screen So this is based on the Pause code that I did the other day. Actually just check my channel. It's in there somewhere about how to pause a game So here's what we have so far. So I'm using the genie editor. You can see it up here I'm hitting function and f5 Okay, so I just have a real simple Character going around the screen if I push the P key it pauses push P it unpauses so P P That is it So what we want to do is we want to add a timer to our game And there's a few things we got to do to make that happen So but it's not complicated. So let me just walk you through that. I think you'll find it pretty straightforward so Just again structure of this program Up here is basically where we set everything up. We create our functions We create our objects etc etc, and then here is where the game actually runs This is the main game loop and you see as I mentioned just now I'm using a pause function to control the game. So if the game is not paused This is what I'm gonna be doing. This is where we shooting things and doing this and that and this is what I'm Pause I just want to keep the screen updated and just wait until I'm not paused anymore So if you're not sure how to do the pause watch that video so The reason I say that is it's gonna determine where we put some of this code and to keep time We're gonna use something called the go figure time module And the time module it just gives you a lot of really cool things you can do with time so what I have to do is To create first I want to create my time limit And and just for the purposes of this tutorial, I'm gonna say five seconds and Just before I start this while true loop. I need to set my current start time okay, and To do that only use the time module Time method and I'm gonna go ahead and print that out and show what that looks like start time And then it's gonna look a little weird and I'll explain what what it is and why it is that way Okay, so you can see here that is the current time on my computer Okay, so this is actually the number of seconds since the beginning of the Unix epic and that's what they call it. I don't know why but To find that that time and this is this is consistent across Unix based machines. This is a Mac So it's has a Unix underpinnings Linux has of course Unix underpinnings don't know about Windows But I'm gonna curious that one myself, but I assume it's roughly the same So so I'm gonna go into Python 3 here. Just kind of show you an import time and I'm gonna say time GM time Zero and that's gonna tell us actually from when it's counting. So you can see it says here 1970 so 50 years ago The first month so January the first day January 1st Zero hour zero minutes and zero seconds. I don't know what these ones mean. Sorry. So that is when it's counting from so that is the number of Seconds and you can see that number if I run that again That number is down in till actually goes down the miller Geez miller quadrilla whatever whatever seconds that is it goes down quite in detail So you can get very very detailed now in our case. We don't need that level detail. We just need seconds Okay, so if I know the start time I Can find the current time and I just use a little subtraction and that will tell me the information I need So inside my not paused Section here my little conditional I'm gonna go ahead and put my timer code So what I need to find is the elapsed time So how long has the game been running since I set the start time? So to do that. I'm gonna say elapsed oops elapsed time Equals I need the current time, which is time that time minus the start time Let's go ahead and print that see what that looks like Don't know why I always do that Okay, so I'm gonna run it and move that over a little bit and you can see one seconds two seconds three seconds four seconds five seconds Six seconds, and you notice you had the decimal places here as well. So you can see that it's pretty accurate Okay So that's also useful information because we're gonna need that as well Now if you don't want to print out that whole decimal part I can just go ahead and for printing purposes cast this which means convert into an integer So let's go ahead and run that see what that looks like Zero one two three four five and so on and so forth Now you're probably you might be thinking hey, I don't want to see counting up. I want to count down So to do that what you would just do is you would just do time oops wrong spot. You would say print time limit Limit minus the time that's been elapsed and that will give us a count down Okay, so let's take a look at that five four three two one and zero And of course it keeps going because we haven't coded that part yet Okay, so now so obviously we don't want that to go on forever so if our elapsed time is Greater than our time limit We can say print you know game over and And we'll just exit the game. We won't bother restarting in this in a simple example Okay, so I'm gonna save that and I'm gonna run that and see what that looks like function f5 for three two one zero Game over which is pretty darn cool Okay, so again now if I wanted to make this one minute I would make that 60 if I wanted two minutes make it 120 You know you might have in your game a case where you can get a power up And then the time limit increases so you can you extend the time limit of the game if you're if you have levels All you have to do is change the start time to the current time as soon as you restart So there's a lot of flexibility here now one little if you're thinking ahead You might realize well, what if you pause the game? What's gonna happen? And so let's take a look at that option So I'm gonna hit P So it's there's pause nothing's going on here and I still have three seconds left So I'm gonna hit P and see what happens Okay, so you can see how of course the time is still going on in the background's time is still passing So to fix that it's pretty straightforward inside of our Else statement here. This is what happens when the game is paused. We basically need to reset The start time So what we got to do is say start time equals The current time Minus the elapsed Time Hey does it hopefully that makes sense. So if the game has been running for five seconds Since the original start time then when we hit pause We just changed the start time to the current time minus what's already passed So when we break out of this this part of the code and get back to actually running it the start times changed to Say fives in that case five seconds before okay, so let's go ahead and test that keep on five four three I'm gonna hit pause and So in our last example it kept running and it got down like negative something or other So let's hit P and you can see it's still at two now at one and then zero game over so that my friends is how you make a Timer Function for your game. It's pretty straightforward again choose a time limit need to find the start time Calculate the elapsed time again you can print it not print it However, you want to display it on the screen check if your elapsed time is greater than your time limit and then in the game If you are using a pause method you need to Recalibrate the start time to the current time minus the elapsed time Real quick some people might ask well, why didn't you just do Equals the time limit and let me go ahead and run that real quick and See if that works And you see how it keeps going and the reason is if you recall This time is actually like 1.682 side five seconds So the elapsed time will almost certainly never be exactly five It'll end up being like five point zero two nine two five for example Okay, so that's why instead of using equals we use greater than that way It's guaranteed to catch it as soon as it goes over that timeline. So yeah, so if any questions put them in the comments below I do check them. I do keep track of them and if you haven't subscribed please subscribe and Yeah, so thanks for watching and keep on coding and yeah, have a great day. Take care