[4.5.9] Sprite Based "Super Mario" Style Countdown Timer (3 Digits)

Jonny

Well-known member
timer.gif

1. Set up a variable with 3 bytes in Zero Page RAM. For this tutorial I'm calling it "myTime"
This is what will be used for the 3 digits. Counter can be upto 999.

2. Add the following code as part of your doDrawSpriteHud.asm (always make backups)
Code:
;;; TIMER ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    TXA
    PHA 
    LDA #220                   ;; Y POS                   ;;
    STA tempA
    LDA #223                   ;; X POS                   ;;
    STA tempB
    LDX #$03                   ;; myTime BYTES            ;;
  
Timer:
    DEX
    LDA myTime,x
    CLC
    ADC #$73                   ;; ZERO IN TILESET         ;;
    STA tempC
    DrawSprite tempA, tempB, tempC, #$03
    LDA tempA
    CLC
    ADC #$08                   ;; OFFSET / SPACE BETWEEN  ;;
    STA tempA
    CPX #$01
    BCS Timer
    PLA
    TAX
Code from Dale Coop. Change ADC #$73 to whichever is 0 in your tileset. ADC #$08 can be changed if you want to space the numbers.

3. Add the following code as part of your HandleGameTimer.asm (always make backups)
Code:
;;; GAME TIMER ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    LDA gameTimerTicks
    CLC
    ADC #$04                 ;; 'SPEED' / 4 IS ABOUT 1 SEC ;;
    STA gameTimerTicks
    BCC checkTime
    SubtractValue #$03, myTime, #1, #$00

checkTime:
    LDA myTime+2             ;; IS 100 PLACE ZERO?         ;;
    BNE notZero
    LDA myTime+1             ;; IS 10  PLACE ZERO?         ;;
    BNE notZero
    LDA myTime               ;; IS 1   PLACE ZERO?         ;;
    BNE notZero
  
    WarpToScreen warpToMap, warpToScreen, #$01

notZero:
Counts down from whatever the timer is set to on screen load. When all places are at 0 WarpToScreen macro is run (put whatever you want to happen).

4. Add the following code as part of your PostScreenLoad.asm (always make backups)
Code:
    LDA #09
    STA myTime+1
    LDA #09
    STA myTime
This sets the initial figure for the timer. In this example timer starts at 99. You can set the timer upto 999 or add / reduce time during gameplay with pickups.

Please let me know if you see improvements. I have tested this with metrovania module and also with a pickup which increases the time. When the time gets over 999 it rolls over so an addition could be something to normalise the timer to its max but for this tutorial I wanted to keep it simple. Hope this is helpful.
 
Last edited:

Jonny

Well-known member
I've done a bit more work on part 4. of the above tutorial. To use screenFlags to change the length of the time in postScreenLoad.asm...

Code:
    LDA ScreenFlags01   
    AND #%00000010
    BNE ShortTimer
    LDA ScreenFlags01
    AND #%00000001
    BNE MediumTimer
    JMP LongTimer

ShortTimer:   
    LDA #00
    STA temp1
    LDA #05
    STA temp2
    JMP setTimer
    
MediumTimer: 
    LDA #04
    STA temp1
    LDA #09
    STA temp2
    JMP setTimer
    
LongTimer:
    LDA #09
    STA temp1
    LDA #09
    STA temp2
    JMP setTimer

setTimer:
    LDA temp1
    STA myTime+1
    LDA temp2
    STA myTime

Using 2 screen flags to set a short timer (for cutcenes maybe?) or a medium length. Defaults to the long timer if niether screenflag is used.
Also, you can put a check in handleGameTimer.asm to completely skip the timer with another screenflag when its not needed. eg.. for title screens.
 

Jonny

Well-known member
I've just been doing some more work on the above code (for changing the length of the time with screenflags) and realised the end 'setTimer' lines are not needed or using temp1/temp2. Improved code here...
Code:
LDA ScreenFlags01   
    AND #%00000010
    BNE ShortTimer
    LDA ScreenFlags01
    AND #%00000001
    BNE MediumTimer
    JMP LongTimer
ShortTimer:   
    LDA #00
    STA myScore+1
    LDA #05   
    STA myScore
    JMP setTimer
MediumTimer: 
    LDA #01
    STA myScore+1
    LDA #09
    STA myScore
    JMP setTimer
LongTimer:
    LDA #02
    STA myScore+1
    LDA #09
    STA myScore
    JMP setTimer
setTimer:
 
Top Bottom