Countdown Timer & Time Bonus
BlitzPlus Forums/BlitzPlus Beginners Area/Countdown Timer & Time Bonus
| ||
I've put together my first attempt at coding something. It works which has surprised me! ;) However, despite spending the last few days trying to read up on and learn how to, I've been unable to implement a countdown timer. I basically want each level in my game to run for say, 60 seconds and then end the game if the player runs out of time. The time should display on screen as something simple like, "TIME: 60" and countdown to 0. If the player completes the level and any time remains I'd like that time to be converted to points and added to their current score. I realise that might be a little above and beyond me to be honest. So if that is all a little too complex then just the basic countdown timer idea would be good for starters. If it's possible for somebody to show me a simple example or point me in the direction of one, it would be most appreciated. You can see what I've managed to do so far, though it isn't working properly (the countdown timer that is). It counts down, but if it reaches 0 it displays "Game Over" but the counter continues to countdown. Also, if you complete the level, the counter starts the next level from where it finished on the previous level, so at 39 seconds for example. My code |
| ||
That's not very complicated, but an important basic exercise: all your timers are based on the function MILLISECS(), which return the millisecs since the computer has started. It is an INTEGER value. some examples: shows this function working: Graphics 800,600 Repeat Print MilliSecs() Until KeyHit(1) shows a time difference, because the time once was stored into a variable: Graphics 800,600 Local time%=MilliSecs() Repeat Print MilliSecs()-time Until KeyHit(1) shows this difference rounded to seconds: Graphics 800,600 Local time%=MilliSecs() Repeat Cls Text 100,100,(MilliSecs()-time)/1000 Flip 1 Until KeyHit(1) so now we are very closed to the final target. To show a difference to a future time, you only have to add this difference at the variable Graphics 800,600 Local time%=MilliSecs()+ 60*1000 Repeat Cls Text 100,100,(time -MilliSecs())/1000 Flip 1 Until KeyHit(1) to get a reaction, when reaching the time, you only have to add an IF command. Inside the IF/ENDIF you could do whatever you want. Graphics 800,600 Local time%=MilliSecs()+ 5*1000 Repeat Cls Text 100,100,(time -MilliSecs())/1000 If time -MilliSecs()<0 End EndIf Flip 1 Until KeyHit(1) ...for example restart the timer again: Graphics 800,600 Local time%=MilliSecs()+ 5*1000 Repeat Cls Text 100,100,(time -MilliSecs())/1000 ;when reaching the end If time -MilliSecs()<0 time%=MilliSecs()+ 5*1000 EndIf ;when an event occurs If MouseHit(1) level=level+1 EndIf If level<>pre_level pre_level=level time%=MilliSecs()+ 5*1000 EndIf Text 100,200,"Level=" + level Flip 1 Until KeyHit(1) |
| ||
Thanks Midimaster. An extremely comprehensive reply! It's been a great help. I now have a rudimentary timer working. \o/ Once again, thanks. |