Computer is fixed. Time to get back to blogging about NES homebrew development. In my last post I talked about monster sequences. Monster sequences are sets of monster portraits displayed near the top of the screen. These portraits tell the player which monsters to whack.
At the time of that post I hadn’t programmed any monsters into the game. That’s changed now. I have my first dummy monsters in the game. I also have a timer and a death sequence and music. It’s almost bedtime so I’m going to keep it simple and just talk about the bomb timer for now.
Bomb Timer
The premise of Ramses Game is that an earthling named Ramses is kidnapped by space pirates and sold at a slave auction to an evil overlord named Bonehammer. Later, Ramses wakes up in a cold room and finds a bomb strapped to his chest, and it’s ticking.
So I need a timer and I need it to cause a GAME OVER when it reaches zero. Pretty standard. Super Mario Brothers 1 had a timer.
For my timer I reserve a byte of RAM for each digit, tens and ones (that’s two bytes). One byte of RAM would be enough to handle the maximum timer value of 99, but it’s easier to draw to the screen if it’s stored in RAM exactly how I want to display it. No hex to decimal conversion.
I also need a frame counter variable that counts frames to determine when one second has passed. The 6502 runs at about 60 frames per second, but I want my timer to count down a little faster, so I define a second as 50 frames:
SECOND_LENGTH = 50 ;constant value defining how many frames are in one "second"
.segment "ZP": zeropage
sec_counter: .res 1 ;frame counter for determining when one "second" has passed
bomb_timer_tens: .res 1 ;tens digit for the bomb timer
bomb_timer_ones: .res 1 ;ones digit for the bomb timer
And the subroutine that is called each frame to run the bomb timer:
.proc do_bombtimer
dec sec_counter ;decrement the frame counter. If it is zero, a second has passed
bne @end ;else nonzero, so quit
lda #SECOND_LENGTH ;reload the framecounter to 50.
sta sec_counter
dec bomb_timer_ones ;subtract 1
bmi @dec_tens ;if 0->FF, decrement the tens place and stick a 9 in the ones
bne @draw ;else if N->nonzero, done
lda bomb_timer_tens ;else if 1->0, check if tens is 0 too, if so die
beq @die
bne @draw ;not 00, so we are still alive
@dec_tens:
dec bomb_timer_tens
lda #9
sta bomb_timer_ones
@draw:
jsr draw_bombtimer
@end:
rts
@die:
jsr draw_bombtimer
lda #BOMB_DEATH ;set gamestate to BOMB_DEATH gamestate
jsr gamestate::set_gamestate
;game over
rts
.endproc
Using two variables for the timer complicates the math ever so slightly, but for me the tradeoff is worth it.
Adding to the timer
In Ramses Game, when the player whacks all the monsters in a sequence, a new sequence is displayed and a little bit of time is added to the timer as a reward. This makes the game more exciting. Instead of giving you 60 seconds to clear 8 sequences, I’ll give you 20. The timer will always hover close to zero, giving the player a sense of urgency for every sequence. Adding to the timer is easy:
;-----------------------------
; add_A_to_timer will add the value in the Accumulator to the bomb timer
.proc add_A_to_timer
clc
adc bomb_timer_ones ;add value in A to the ones digit
@loop:
cmp #10 ;if result is 10 or more, loop to subtract 10 and
;increment the tens until ones digit is less than 10
bcc @store
sec
sbc #10
inc bomb_timer_tens
bne @loop
@store:
sta bomb_timer_ones ;store the result, then check to make sure the
;tens digit didn't overflow. Max time = 99
lda bomb_timer_tens
cmp #10
bcc @end
lda #9
sta bomb_timer_tens
@end:
jsr draw_bombtimer
rts
.endproc
Conclusion
That’s it for the bomb timer. At least for now. There are a couple features I still want to add to the bomb timer. One is a visual cue of how much time is added to the timer upon completion of a sequence. Like a sprite that says “+8″ that floats from the sequence indicator panel to the timer. Another is palette cycling the timer display when it gets close to zero, to highlight the urgency and add to the tension.
If you have any ideas or comments, leave a comment!

