Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ti83plus.inc"
- #define progStart $9D95
- .org progStart-2
- .db $BB,$6D
- init:
- bcall(_ClrLCDFull)
- bcall(_HomeUp)
- call setApple
- jp loop
- ;--- GameLoop
- loop:
- call input
- call move
- call collision
- call draw
- jp loop
- ;--- adds a Segment to the body
- addSegment:
- ld ix, var_body ;TableAddr
- ld bc, $0001 ;steps
- call getLength
- ld (var_length), a
- ;ix = addr + a
- ld ix, var_body
- ld b, $00
- sub $02
- ld c, a
- add ix, bc
- ld a, (ix)
- ld (ix+2), a
- ld a, (ix+1)
- ld (ix+3), a
- ret
- ;--- calculates a random number between the given value
- ;Input a = length
- ;Output a = random number
- random:
- ld a, $06
- ret
- ;--- sets the location of the apple
- setApple:
- ld a, $0F
- call random
- ld (var_apple), a
- ld a, $07
- call random
- ld (var_apple+1), a
- ret
- ;--- Check the Collision between the body and the borders
- collision:
- ld hl, var_mode
- ld a, (hl)
- cp $00
- jp nz, collision_mode2
- ;Collision with the wall
- collision_mode1:
- jp collision_body
- ;Without wall collision
- collision_mode2:
- ld hl, var_head
- ld a, (hl) ;a = pos_x
- collision_mode2_left:
- ;if x < 0
- cp $FF
- jp nz, collision_mode2_right
- ld a, $0F ;16
- ld (hl), a
- jp collision_body
- collision_mode2_right:
- ;if x > 16
- cp $10
- jp nz, collision_mode2_top
- ld a, $00
- ld (hl), a
- jp collision_body
- collision_mode2_top:
- ld hl, var_head+1
- ld a, (hl) ;a = pos_y
- ;if y < 0
- cp $FF
- jp nz, collision_mode2_bottom
- ld a, $07 ;16
- ld (hl), a
- jp collision_body
- collision_mode2_bottom:
- ;if y > 8
- cp $08
- jp nz, collision_body
- ld a, $00 ;16
- ld (hl), a
- collision_body:
- ld a, (var_head)
- ld b, a
- ld a, (hl)
- cp b ;TODO
- collision_body_loop:
- collision_apple_x:
- ld a, (var_head)
- ld b, a
- ld a, (var_apple)
- cp b
- jp nz, collision_end
- collision_apple_y:
- ld a, (var_head+1)
- ld b, a
- ld a, (var_apple+1)
- cp b
- jp nz, collision_end
- call addSegment
- call setApple
- collision_end:
- ret
- ;--- Move Snake
- move:
- ;shift segments
- ;for(i=bodyLength-1; i>0; i--)
- ; body(i) = body(i-1)
- ; body(0) = head
- ld ix, var_body ;TableAddr
- ld bc, $0002 ;steps
- call getLength
- add a, a
- sub $03 ;a = i
- ld b,0
- ld c, a
- ld ix, var_body
- add ix, bc ;ix = addr + i
- inc a ;a = counter
- ;maybe sub 2?
- move_loop:
- ;body(i+1) = body(i)
- ld b, (ix)
- ld (ix+2), b
- dec ix
- dec a
- jp nz, move_loop
- ;move first body segment
- ld a, (var_head)
- ld (var_body), a
- ld a, (var_head+1)
- ld (var_body+1), a
- ;move head
- ;switch(var_dir){
- ;case 1: (var_dir)--
- ld a, (var_dir)
- move_case1:
- cp $01
- jp nz, move_case2
- ld a, (var_head)
- dec a
- ld (var_head), a
- ret
- move_case2:
- cp $02
- jp nz, move_case3
- ld a, (var_head)
- inc a
- ld (var_head), a
- ret
- move_case3:
- cp $03
- jp nz, move_case4
- ld a, (var_head+1)
- dec a
- ld (var_head+1), a
- ret
- move_case4:
- ld a, (var_head+1)
- inc a
- ld (var_head+1), a
- ret
- ;--- Get the Length of an Array
- ;Input ix = Array Address
- ; bc = Steps
- ;Output a = Length
- getLength:
- ld d, $00 ;counter
- getLength_loop:
- ld a, (ix)
- cp $FF
- getLength_mod: ;For modifying the value to check for
- jp z, getLength_end
- inc d
- ;ix = ix + a
- add ix, bc
- jp getLength_loop
- getLength_end:
- ld a, d
- ret
- ;--- Draw Segments
- draw:
- bcall(_ClrLCDFull)
- ;draw Apple
- ld a, (var_apple)
- ld (curCol), a
- ld a, (var_apple+1)
- ld (curRow), a
- ld a, 'a'
- bcall(_PutC)
- ;draw Head
- ld a, (var_head)
- ld (curCol), a
- ld a, (var_head+1)
- ld (curRow), a
- ld a, '0'
- bcall(_PutC)
- ld ix, var_body ;ix = bodyAddr
- draw_loop:
- ld a,(ix)
- cp $FF
- ret z
- ;x
- ld (curCol), a
- inc ix
- ;y
- ld a, (ix)
- ld (curRow), a
- ld a, '0'
- bcall(_PutC)
- inc ix
- jp draw_loop
- ;--- GameOver
- exit:
- bcall(_ClrLCDFull)
- bcall(_HomeUp)
- ld hl, str_gameOver
- bcall(_PutS)
- ld hl, curCol
- ld (hl), $00
- ld hl, curRow
- ld (hl), $02
- ld hl, str_pressKey
- bcall(_PutS)
- exit_loop:
- bcall(_GetKey)
- cp $00
- jp z, exit_loop
- ret
- ;--- Input
- input:
- bcall(_GetKey)
- input_case1:
- cp $01
- jp nz, input_case2
- ld b, $02
- jp input_end
- input_case2:
- cp $02
- jp nz, input_case3
- ld b, $01
- jp input_end
- input_case3:
- cp $03
- jp nz, input_case4
- ld b, $03
- jp input_end
- input_case4:
- cp $04
- jp nz, input_default
- ld b, $04
- jp input_end
- input_default:
- pop bc
- jp exit
- input_end:
- ld hl, var_dir
- ld (hl), b
- ret
- ;--- VARS
- var_body:
- ;0...20
- .db $02,$03, $01,$03
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- ;20...40
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- .db $FF,$FF, $FF,$FF
- var_length:
- .db $04
- var_head:
- .db $03,$03
- var_apple:
- .db $00,$00
- var_dir:
- .db $02
- var_mode:
- .db $01
- ;--- Strings
- str_gameOver:
- .db " Game Over",$00
- str_pressKey:
- .db "Press Key!",$00
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement