Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; PROGRAM
- ; tiles
- ; 0x74 eat ; 0x24 snake ; 0x60 black ; 0x7f white
- ; up to 427 (?) usable bytes from $dd55 (OT party data) through $d42b (?)
- ; content kept through saving the game
- ; content only changes with a trainer battle
- ;;; PROGRAM BEGIN
- ; dd55
- ; ~278 bytes
- ; Place 20 black tiles starting from hl
- DrawHorizontalBorder:
- ld bc, $0014 ; screen width (20)
- ld a, $60 ; black tile (border)
- jp $314c ; ByteFill
- ; Entry point ; dd5d
- InitGame:
- ld a, 1
- ldh [$ffd6], a ; hBGMapMode
- Restart:
- ld hl, $c700 ; $c700 will store the position of the snake in the screen
- ld bc, 100 ; for snake of 50 tiles long max
- xor a
- call $314c ; ByteFill
- ; Draw BG
- InitScreen:
- ld hl, $c3a0 ; tilemap
- ; Draw top border
- call DrawHorizontalBorder ; top row ;dd55
- ; Draw background
- ld bc, $0140 ; screen size (20x18) - 2 * screen width (2x20)
- ld a, $7f ; white tile (BG)
- call $314c ; ByteFill
- ; Draw bottom border
- call DrawHorizontalBorder ; bottom row ;dd55
- ; Draw vertical borders
- ld hl, $c3b3 ; top right tile
- ld bc, $0013 ; screen width - 1
- ld a, $60 ; black tile (border)
- ld d, $11 ; screen height
- .loop
- ld [hli], a ; right border tile
- ld [hl], a ; left border tile
- add hl, bc
- dec d
- jr nz, .loop
- ; Draw obstacles (two horizontal bars)
- ld hl, $c409
- ld bc, $000A
- ; ld a, $60 ; black tile
- push bc
- call $314c ; ByteFill
- ld hl, $c481
- pop bc
- call $314c ; ByteFill
- ldh a, [$ffd0]
- ld a, $10
- ldh [$ffd0], a ; starting movement direction, set to right
- ; Draw snake
- ld de, $c448 ; center of screen
- ; Place the three-tile snake in the screen and save its position and length
- ld b, 3
- ld a, b
- ldh [$ffe6], a ; save snake length
- ld a, $24 ; snake tile
- ld hl, $c700
- .loop2
- ld [de], a ; place snake tile
- ; save the snake position in the buffer at $c700
- ld [hl], d
- inc hl
- ld [hl], e
- inc hl
- dec b
- inc de
- jr nz, .loop2
- ; fallthrough
- ; Draw the object that can be eaten in a random position in the screen
- DrawObject:
- ; Each random number covers 2 tiles, $ffcf is used to rotate between left and right tile
- ldh a, [$ffcf]
- xor 1
- ldh [$ffcf], a
- .drawObject
- call $30A2 ; random
- cp $b4 ; screen size / 2
- jr nc, .drawObject ; don't place the object outside the screen
- ld b, $0
- ld c, a
- ld hl, $c3a0
- add hl,bc
- add hl,bc
- ldh a, [$ffcf]
- and a
- jr z, .ok
- inc hl
- .ok
- ld a, $7f ; white tile
- cp [hl]
- jr nz, .drawObject ; don't place the object in a border or over a snake or obstacle tile
- ; Place object
- ld a, $74 ; object [x]
- ld [hl], a ; place object
- ; fallthrough
- ; Move snake it the $c700 buffer
- ShiftPositions: ; Main Loop
- ; $c700 contains snake tail
- ; snake head is at $c700 + 2 * snake_length - 2
- ld de, $c700
- ldh a, [$ffe6]
- dec a
- add a
- ld b, a
- ; If we ate the object in the last move, the snake only increases its length
- ldh a, [$fffe]
- and a
- jr nz, .ateObject
- ; Remove the snake's tail
- ld a, [de]
- ld h, a
- inc de
- ld a, [de]
- ld l, a
- dec de
- ld a, $7f ; white tile
- ld [hl], a
- .loop
- ; move every snake tile one space
- inc de
- inc de
- ld a, [de]
- dec de
- dec de
- ld [de], a
- inc de
- dec b
- jr nz, .loop
- dec de
- dec de ; de now points to previous snake head
- jr .goOn
- .ateObject
- xor a
- ldh [$fffe], a
- ldh a, [$ffe6]
- inc a
- ldh [$ffe6], a ; increase snake length
- dec a
- dec a
- .loop2
- inc de
- inc de
- dec a
- jr nz, .loop2
- ; fallthrough
- .goOn
- ; de now points to snake head tile, load its content (tile occupied) to hl
- ld a, [de]
- ld h, a
- inc de
- ld a, [de]
- ld l, a
- inc de
- ; fallthrough
- ; Read user input from $ffa6
- ReadUserInput:
- ldh a, [$ffa6] ; joypad register
- and $F0 ; %11110000, R/L/U/D bits
- jr nz, .newMovement ; key was pressed
- ldh a, [$ffd0] ; take the last key pressed, which is also current movement direction
- .newMovement
- ldh [$ffd0], a ; save last key pressed out of R/L/U/D
- cp $10
- jr z, MovePositionRight
- cp $20
- jr z, MovePositionLeft
- cp $40
- jr z, MovePositionUp
- ; jr MovePositionDown
- MovePositionDown: ; +$14
- ld bc, $0014
- jr MovePosition
- MovePositionUp: ; - $14
- ld bc, $ffec
- jr MovePosition
- MovePositionLeft: ; -1
- ld bc, $ffff
- jr MovePosition
- MovePositionRight: ; +1
- ld bc, $0001
- ; jr MovePosition
- ; Calculate the new snake head and save it in the buffer
- MovePosition:
- add hl, bc
- ; Check if the snake ate the object
- ld a, $74
- cp [hl]
- jr nz, .checkCollision
- ldh [$fffe], a
- jr .didEat
- .checkCollision
- ; Check if the snake collided so the player lost
- ld a, $7f
- cp [hl]
- jp nz, Restart ; dd61
- .didEat
- ; Save the new snake head tile in the buffer and draw the new head
- ld a, $24
- ld [hl], a ; draw head
- ld a, h
- ld [de], a
- inc de
- ld a, l
- ld [de], a
- ; fallthrough
- ; Delay frames (the longer the snake is the faster the game goes)
- .delayFrames
- ldh a, [$ffe6]
- ld c, a
- ld a, 40
- sub c
- sub c
- ld c, a
- call $033c ; DelayFrames
- ; Finished this loop
- ldh a, [$fffe] ; restore whether snake ate or not
- and a
- jp nz, DrawObject ; make sure to place a new object to replace the one just eaten ; ddb9
- jp ShiftPositions ; skip placing an object and go back to the main loop ; dddc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement