LoginLogin

How to make a Countdown timer

Root / Programming Questions / [.]

ElzoBroCreated:
I want to have a program with a timer that counts down, Also if i could also have a timer that counts up from 0. Thank You!

I use this. I just insert this to my game loop: PRINT TIME 'or whatever variable you'd use for time INC TICK, 1 'to increase the smaller bits in the time, like the milliseconds of the seconds IF TICK==60 THEN 'you can increase this number for a longer wait between adds or shorter for less time INC TIME, 1 'change the one to negative for a countdown, leave it positive for a count up TICK=0 'reset the millisecond stuff ENDIF WAIT 1 GOTO BLAH 'whatever you call your main loop Also for a countdown you may want to state your starting time amount before the loop.

ok, this helps Thanks!

I would do it more like,
TIME=60 'This can be whatever you want
@LOOP
CLS
PRINT TIME
TIME=TIME-1
IF TIME<1 THEN BEEP 5
WAIT 60
GOTO@LOOP
That will wait one minute before beeping once every second.

I would do it more like,
TIME=60 'This can be whatever you want
@LOOP
CLS
PRINT TIME
TIME=TIME-1
IF TIME<1 THEN BEEP 5
WAIT 60
GOTO@LOOP
That will wait one minute before beeping once every second.
But this won't go back to sixty after beeping

I would do it more like,
TIME=60 'This can be whatever you want
@LOOP
CLS
PRINT TIME
TIME=TIME-1
IF TIME<1 THEN BEEP 5
WAIT 60
GOTO@LOOP
That will wait one minute before beeping once every second.
But this won't go back to sixty after beeping
Oh, I didn't know that was a requirement. Here's the altered code:
TIME=60 'This can be whatever you want
@LOOP
CLS
PRINT TIME
TIME=TIME-1
IF TIME<1 THEN BEEP 5 TIME=60
WAIT 60
GOTO@LOOP

And BTW, I made a help thread thing. Do you know how to flip sprites?

I would make it more dynamic:
CDTIME = 360 ' countdown time in frames - this is 6 seconds
CDSTART = MAINCNTL

@LOOP
TIMELEFT = CDTIME - (MAINCNTL - CDSTART)

PRINT TIMELEFT/60
IF TIMELEFT <= 0 THEN BEEP 5: CDSTART = CDSTART + CDTIME

VSYNC 1 'completely optional, can be any number of frames
GOTO @LOOP
I hope I got the syntax right. This works consistently and works 'in the background', by which I mean you can have other code in your loop without worrying that the countdown will be wrong somehow. It even loops correctly and consistently when you have long and/or inconsistent loop times. The only thing left to fix is when MAINCNTL overflows into MAINCNTH, but that's too much of a headache.

The only thing left to fix is when MAINCNTL overflows into MAINCNTH, but that's too much of a headache.
MAINCNTL and MAINCNTH were replaced with one system variable MAINCNT, which is an int type. Thanks to the high range of the int type, the overflow time is on the order of around one year, so you don't have to account for it.