How to make a Countdown timer
Root / Programming Questions / [.]
ElzoBroCreated:
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.
Oh, I didn't know that was a requirement. Here's the altered code:I would do it more like,But this won't go back to sixty after beepingTIME=60 'This can be whatever you want @LOOP CLS PRINT TIME TIME=TIME-1 IF TIME<1 THEN BEEP 5 WAIT 60 GOTO@LOOPThat will wait one minute before beeping once every second.
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
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 @LOOPI 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.