Just a short post today. In the arcade Galaga, the 1UP text flashes while player one is playing.
We can’t use the
FLASH attribute bit for this, because that would alternate between red and black backgrounds. Instead, let’s use the same technique we applied to make the ship shimmer:
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
; sprites.asm ld a, (FRAMES) ; Read the LSB of the ROM frame counter (0.255) ld b, a ; Save the value for the 1UP animation and %00000011 ; Take the lowest 2 bits (effectively FRAMES modulus 4), ret nz ; and return 3 out of every 4 frames. ld a, (MovePlayer.AnimOffset) ; For every 4th frame, read player's tile offset, xor %00000100 ; alternate between (0 => 4 => 0 => 4 => etc), ld (MovePlayer.AnimOffset), a ; SMC> then save it back. ld a, b ; Restore the frame counter and %0010000 ; 16 out of every 32 frames will be zero, jp z, OneUp ; which is already the attribute value for black, ld a, BrightRedBlackP ; otherwise bright red for the other 16 frames. OneUp: ld (AttributeAddress), a ; Set this colour ld (AttributeAddress+1), a ; for the three 1UP ld (AttributeAddress+2), a ; attributes.
We’re already reading the frame counter in
AnimateDemo, and using it to do something every four frames. Let’s save the value (which is quicker than reading it again), then mask out everything but the fifth bit. This gives us 16 frames with the value 0, and 16 frames with the value 16.
Actually, we only hit this piece of code for one in every four frames, because the earlier code returns three out of every four frames. This doesn’t matter though, because the points where the colours change are aligned with the frames that are let through.
Zero is handily already the colour of black, so whenever it’s non-zero we’ll set it to bright red. Then we’ll set the first three attributes to whatever colour we calculated.
Simple! More next time 🙂