From colliding to dying is a small step. Once the player collides with an enemy we kill him by removing the player object. A "Press Fire to Restart" message is displayed and a press on the button will revive the player object.
We add the function RemoveObject which simply removes the object from the SPRITE_ACTIVE table and disables its sprite. While we wait for the player to press the button all the rest of the game moves on.
First of all we add the getting killed part in our old routine "CheckCollisions". Nothing ground breaking, a call to the text display function follows by removing the object and resetting the button released flag.
.PlayerCollidedWithEnemy
;display text
lda #<TEXT_PRESS_FIRE
sta ZEROPAGE_POINTER_1
lda #>TEXT_PRESS_FIRE
sta ZEROPAGE_POINTER_1 + 1
lda #10
sta PARAM1
lda #23
sta PARAM2
jsr DisplayText
ldx #0
stx BUTTON_PRESSED
stx BUTTON_RELEASED
jsr RemoveObject
rts
A new call is added to the main game loop which controls behaviour when the player is dead:
GameLoop
jsr WaitFrame
jsr DeadControl
jsr ObjectControl
jsr CheckCollisions
jmp GameLoop
Surprisingly easy. We check if the player is really dead, if he isn't, bail out. Then we check for the joystick button being pressed, but only allow to go on, if the button has been released before. If all that happened, we simply force the player object back into life (for now with hard coded values).
!zone DeadControl
DeadControl
lda SPRITE_ACTIVE
beq .PlayerIsDead
rts
.PlayerIsDead
lda #$10
bit $dc00
bne .ButtonNotPressed
;button pushed
lda BUTTON_RELEASED
bne .Restart
rts
.ButtonNotPressed
lda #1
sta BUTTON_RELEASED
rts
.Restart
lda #5
sta PARAM1
lda #4
sta PARAM2
;type
lda #TYPE_PLAYER
sta PARAM3
ldx #0
lda PARAM3
sta SPRITE_ACTIVE,x
;PARAM1 and PARAM2 hold x,y already
jsr CalcSpritePosFromCharPos
;enable sprite
lda BIT_TABLE,x
ora VIC_SPRITE_ENABLE
sta VIC_SPRITE_ENABLE
;initialise enemy values
lda #SPRITE_PLAYER
sta SPRITE_POINTER_BASE,x
;look right per default
lda #0
sta SPRITE_DIRECTION,x
rts