Skip to content

Sprite Movement with Bounds

Move sprites with joystick input and play area boundary checks. Foundation for player control.

Code status: illustrative code. Adapt the listing to your program and check it on your target machine. This page links no assembly or execution record for the listing itself.

Overview

Amiga sprites move by updating their control words each frame. Read the joystick, apply movement with boundary checks, then recalculate the sprite’s position registers. The 68000’s signed comparisons make bounds checking straightforward.

Calling contract

Include the maintained joystick reader once and sample it before move_frog. It takes decoded held controls in D0.W, changes D1 and condition codes, and preserves D0 and the address registers. frog_x and frog_y are writable signed words, initially inside their inclusive ranges. With speed 2 these small coordinates cannot overflow a word.

This version rejects a step that crosses an edge; it does not saturate to the edge. From X=279, right proposes 281 and stays 279. Choose saturation if reaching 280 matters. Directions are processed up, down, left, right; opposing inputs can behave differently at a boundary. These are game rules to choose deliberately.

The loop below is glue, not a standalone executable: supply the frame wait and sprite control-word writer, its allocated Chip RAM block, display setup and lifetime. Use one coordinate convention throughout; these example limits are beam coordinates, not automatically a 320×256 screen-relative rectangle.

Code

68000 assembly
; =============================================================================
; SPRITE MOVEMENT WITH BOUNDS - AMIGA
; Move sprite; reject a step that would cross a boundary
; Taught: Game 1 (Signal), Unit 2
; Cost depends on the assembled caller and memory placement.
; =============================================================================

MOVE_SPEED      equ 2           ; Pixels per frame

; Play area boundaries
MIN_X           equ 48          ; Left edge
MAX_X           equ 280         ; Right edge
MIN_Y           equ 44          ; Top edge
MAX_Y           equ 196         ; Bottom edge

; Move sprite based on decoded joystick input
; Input: D0 = decoded joystick (from read_controls)
;        Bit 8 = up, Bit 0 = down
;        Bit 9 = left, Bit 1 = right
move_frog:
            ; === Check Up ===
            btst    #8,d0
            beq.s   .no_up
            move.w  frog_y,d1
            sub.w   #MOVE_SPEED,d1
            cmp.w   #MIN_Y,d1
            blt.s   .no_up          ; Past top boundary
            move.w  d1,frog_y
.no_up:
            ; === Check Down ===
            btst    #0,d0
            beq.s   .no_down
            move.w  frog_y,d1
            add.w   #MOVE_SPEED,d1
            cmp.w   #MAX_Y,d1
            bgt.s   .no_down        ; Past bottom boundary
            move.w  d1,frog_y
.no_down:
            ; === Check Left ===
            btst    #9,d0
            beq.s   .no_left
            move.w  frog_x,d1
            sub.w   #MOVE_SPEED,d1
            cmp.w   #MIN_X,d1
            blt.s   .no_left        ; Past left boundary
            move.w  d1,frog_x
.no_left:
            ; === Check Right ===
            btst    #1,d0
            beq.s   .no_right
            move.w  frog_x,d1
            add.w   #MOVE_SPEED,d1
            cmp.w   #MAX_X,d1
            bgt.s   .no_right       ; Past right boundary
            move.w  d1,frog_x
.no_right:
            rts

frog_x:     dc.w    160             ; Current X position
frog_y:     dc.w    180             ; Current Y position

Trade-offs

Aspect Cost
CPU Not measured for this illustrative listing; include the caller and hardware scheduling costs
Memory Assemble the complete caller to obtain its size
Limitation Must call update_sprite after to see changes

When to use: Any game with joystick-controlled sprites.

When to avoid: Mouse-controlled games - read mouse deltas instead.

How Signed Comparisons Work

The 68000’s CMP and Bcc instructions handle signed values naturally:

Condition Branch Use Case
BLT Less than (signed) Check against minimum
BGT Greater than (signed) Check against maximum
BLE Less or equal Candidate at or below a maximum
BGE Greater or equal Candidate at or above a minimum

On a 6502, unsigned small screen coordinates can use carry-based comparisons; signed values need an explicit signed comparison. Choose the representation before choosing the branch.

Complete Movement Loop

68000 assembly
mainloop:
            bsr     wait_vblank
            bsr     read_controls   ; D0.W = maintained held-control word
            bsr     move_frog       ; Update position
            bsr     update_sprite   ; Recalculate control words
            bra     mainloop

Adjusting Boundaries

For different play areas or sprite sizes:

68000 assembly
SPRITE_WIDTH    equ 16
SPRITE_HEIGHT   equ 16

; Account for sprite size in boundaries
MAX_X           equ 320-SPRITE_WIDTH
MAX_Y           equ 256-SPRITE_HEIGHT

Patterns: Joystick Reading, Sprite Control Words

Vault: Commodore Amiga