Losing a life and starting again, the way Mario does
The walkers’ level with three lives. Play it and walk into a walker: the round stops, the square hops and falls, a screen says how many lives are left, and the level begins again. Lose all three to see the end. The program is 566 instructions in 1,456 bytes, built and checked the same way.
Play it here Change it on the desk The cartridge, to play anywhere that plays NES cartridges: lives.nes
On the screen
A life lost is a short scene of its own
When Mario touches a Goomba in Super Mario Bros., the game does not go straight to the next try. Everything stops, and Mario stays frozen where he was touched for a moment. Then he hops up and falls straight down off the screen, through the ground as if it were not there. Only when the fall is over is the life taken. The picture is then cleared for a plain screen showing the lives left, and after a while the level starts again from its beginning. This cartridge plays the same scene with its own timings, a smaller hop and its own words.
| this cartridge | Super Mario Bros. | |
|---|---|---|
| frames everything holds still at the touch | 21 | 16 |
| pixels the hop rises | 24 | 53 |
| frames from the touch until the life is taken | 55 | 208 |
| frames from that until the picture is cleared | 0 | 2 |
| frames until play starts again | 121 | 168 |
| the level begins again from its start | yes | yes |
As written
The part of the program this lesson is about. The whole program is a plain text file: lives.s
;; The square and each walker: closer than 14 pixels across and 14 up
;; and down (the walkers stand on the ground, their top at 192) is a touch,
;; and the round stops there: the square hangs for 20 frames with the hop
;; already in it, 4 pixels a frame upward.
touch:
LDX #$01
near:
LDA $11
SEC
SBC $40,X
BCS across1
EOR #$FF
ADC #$01
across1:
CMP #$0E
BCS apart
LDA $15
SEC
SBC #$C0
BCS down1
EOR #$FF
ADC #$01
down1:
CMP #$0E
BCS apart
LDA #$01
STA $50
LDA #$14
STA $51
LDA #$00
STA $16
LDA #$FC
STA $17
RTS
apart:
DEX
BPL near
RTS
;; The fall: the speed moves the square and pulls 96/256 a frame, up to 4
;; pixels a frame, through anything. Past the bottom of the screen a life
;; is taken and the screen for what is left comes up.
drop:
CLC
LDA $14
ADC $16
STA $14
LDA $15
ADC $17
STA $15
CLC
LDA $16
ADC #$60
STA $16
LDA $17
ADC #$00
STA $17
BMI dropping
CMP #$04
BCC low
LDA #$00
STA $16
LDA #$04
STA $17
low:
LDA $15
CMP #$F0
BCC dropping
DEC $52
JSR lives
dropping:
RTS
;; The screen between rounds, drawn with the picture off: both tables
;; empty, every sprite out of sight, and a line of words in the middle,
;; LIVES and the count, or GAME OVER with none left; it stays 120 frames.
lives:
LDA #$00
STA $2001
LDA #$20
STA $2006
LDA #$00
STA $2006
TAX
LDY #$04
wipe2:
STA $2007
INX
BNE wipe2
DEY
BNE wipe2
LDA #$FF
hide2:
STA $0200,X
INX
BNE hide2
LDA #$21
STA $2006
LDA #$CC
STA $2006
LDA $52
BEQ gameover
LDX #$00
word1:
LDA livesword,X
STA $2007
INX
CPX #$06
BNE word1
LDA $52
CLC
ADC #$2A
STA $2007
LDA #$03
STA $50
JMP shown
gameover:
LDX #$00
word2:
LDA overword,X
STA $2007
INX
CPX #$09
BNE word2
LDA #$04
STA $50
shown:
LDA #$78
STA $51
LDA #$00
STA $2005
STA $2005
LDA #$1E
STA $2001
RTSBefore this one: A pause that holds the world still, the way Mario's does
Next: An item screen the picture slides away to show, the way Zelda's does