The camera's dead zone, the way Mario's is

A level two screens wide and a square to walk across it. Play it, walk right and watch the posts: the picture waits, then starts to slide, then keeps pace. Turn round and the square walks back across the screen before the picture follows. The program is 246 instructions in 551 bytes, built and checked the same way.

Play it here Change it on the desk The cartridge, to play anywhere that plays NES cartridges: camera.nes

On the screen

frame 60
frame 60
frame 150
frame 150
frame 330
frame 330
frame 420
frame 420

The camera waits, then eases in, then holds

Super Mario Bros. does not keep Mario in the middle of the screen. Walking right, he crosses the left part of it with the picture standing still. A little past 80 pixels the camera starts to move, but slower than he does, so he keeps gaining on it until he is at 112; from there it takes every pixel he moves. That stretch where he moves and the camera does not is the dead zone, and the slow start is why the scroll never jerks. Walking left, the game’s camera never moves at all, which is why Mario cannot go back into a level. This cartridge has the game’s right edge exactly, and a left edge of its own at 48 so the camera follows it back.

this cartridgeSuper Mario Bros.
where on the screen the player is when the camera first moves8081
where it holds the player once it keeps pace112112
frames it moved slower than the player, easing in6545
the camera follows the player back to the leftyesno, never
where it holds the player walking left48none
where it starts again after the player turns back right7980

As written

The part of the program this lesson is about. The whole program is a plain text file: camera.s

;; The camera, after the square has moved. Its place on the screen is x
;; less the camera. Walking right past 80, the camera moves a pixel; and
;; if the square is still past 112, the camera is set to keep it at 112.
;; Walking left with the square nearer the edge than 48, the camera is
;; set to keep it at 48. Then the camera is held between 0 and 256, the
;; two ends of the level.
camera:
    LDA $11
    SEC
    SBC $20
    STA $22
    LDA $13
    BEQ settled
    BMI leftward
    LDA $22
    CMP #$51
    BCC settled
    INC $20
    BNE eased
    INC $21
eased:
    LDA $11
    SEC
    SBC $20
    CMP #$71
    BCC hold
    LDA $11
    SEC
    SBC #$70
    STA $20
    LDA $12
    SBC #$00
    STA $21
    JMP hold
leftward:
    LDA $22
    CMP #$30
    BCS settled
    LDA $11
    SEC
    SBC #$30
    STA $20
    LDA $12
    SBC #$00
    STA $21
hold:
    LDA $21
    BPL notbelow
    LDA #$00
    STA $20
    STA $21
    JMP settled
notbelow:
    CMP #$01
    BCC settled
    BNE above
    LDA $20
    BEQ settled
above:
    LDA #$00
    STA $20
    LDA #$01
    STA $21
settled:
    LDA $11
    SEC
    SBC $20
    STA $22
    RTS

Before this one: A picture that scrolls the way Mario's does

Next: Rooms that slide in the way Zelda's do

All the lessons