Advertisement
Crystal_

snake

Jun 2nd, 2015
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;; PROGRAM
  2. ; tiles
  3.     ; 0x74 eat ; 0x24 snake ; 0x60 black ; 0x7f white
  4.  
  5. ; up to 427 (?) usable bytes from $dd55 (OT party data) through $d42b (?)
  6.     ; content kept through saving the game
  7.     ; content only changes with a trainer battle
  8.  
  9. ;;; PROGRAM BEGIN
  10. ; dd55
  11. ; ~278 bytes
  12.  
  13. ; Place 20 black tiles starting from hl
  14. DrawHorizontalBorder:
  15.     ld bc, $0014 ; screen width (20)
  16.     ld a, $60 ; black tile (border)
  17.     jp $314c ; ByteFill
  18.  
  19. ; Entry point ; dd5d
  20. InitGame:
  21.     ld a, 1
  22.     ldh [$ffd6], a ; hBGMapMode
  23.  
  24. Restart:
  25.     ld hl, $c700 ; $c700 will store the position of the snake in the screen
  26.     ld bc, 100 ; for snake of 50 tiles long max
  27.     xor a
  28.     call $314c ; ByteFill
  29.  
  30. ; Draw BG
  31. InitScreen:
  32.     ld hl, $c3a0 ; tilemap
  33.  
  34. ; Draw top border
  35.     call DrawHorizontalBorder ; top row ;dd55
  36.  
  37. ; Draw background
  38.     ld bc, $0140 ; screen size (20x18) - 2 * screen width (2x20)
  39.     ld a, $7f ; white tile (BG)
  40.     call $314c ; ByteFill
  41.  
  42. ; Draw bottom border
  43.     call DrawHorizontalBorder ; bottom row ;dd55
  44.  
  45. ; Draw vertical borders
  46.     ld hl, $c3b3 ; top right tile
  47.     ld bc, $0013 ; screen width - 1
  48.     ld a, $60 ; black tile (border)
  49.     ld d, $11 ; screen height
  50. .loop
  51.     ld [hli], a ; right border tile
  52.     ld [hl], a ; left border tile
  53.     add hl, bc
  54.     dec d
  55.     jr nz, .loop
  56.    
  57. ; Draw obstacles (two horizontal bars)
  58.     ld hl, $c409
  59.     ld bc, $000A
  60. ;   ld a, $60 ; black tile
  61.     push bc
  62.     call $314c ; ByteFill
  63.     ld hl, $c481
  64.     pop bc
  65.     call $314c ; ByteFill
  66.    
  67.     ldh a, [$ffd0]
  68.     ld a, $10
  69.     ldh [$ffd0], a ; starting movement direction, set to right
  70. ; Draw snake
  71.     ld de, $c448 ; center of screen
  72.    
  73. ; Place the three-tile snake in the screen and save its position and length
  74.     ld b, 3
  75.     ld a, b
  76.     ldh [$ffe6], a ; save snake length 
  77.     ld a, $24 ; snake tile
  78.     ld hl, $c700
  79. .loop2
  80.     ld [de], a ; place snake tile
  81. ; save the snake position in the buffer at $c700   
  82.     ld [hl], d
  83.     inc hl
  84.     ld [hl], e
  85.     inc hl
  86.     dec b
  87.     inc de
  88.     jr nz, .loop2
  89. ; fallthrough
  90.  
  91. ; Draw the object that can be eaten in a random position in the screen
  92. DrawObject:
  93. ; Each random number covers 2 tiles, $ffcf is used to rotate between left and right tile
  94.     ldh a, [$ffcf]
  95.     xor 1
  96.     ldh [$ffcf], a
  97.  
  98. .drawObject
  99.     call $30A2 ; random
  100.     cp $b4 ; screen size / 2
  101.     jr nc, .drawObject ; don't place the object outside the screen
  102.  
  103.     ld b, $0
  104.     ld c, a
  105.     ld hl, $c3a0
  106.     add hl,bc
  107.     add hl,bc
  108.     ldh a, [$ffcf]
  109.     and a
  110.     jr z, .ok
  111.     inc hl
  112. .ok
  113.     ld a, $7f ; white tile
  114.     cp [hl]
  115.     jr nz, .drawObject ; don't place the object in a border or over a snake or obstacle tile
  116.  
  117. ; Place object
  118.     ld a, $74 ; object [x]
  119.     ld [hl], a ; place object
  120. ; fallthrough
  121.  
  122. ; Move snake it the $c700 buffer
  123. ShiftPositions: ; Main Loop
  124. ; $c700 contains snake tail
  125. ; snake head is at $c700 + 2 * snake_length - 2
  126.     ld de, $c700
  127.     ldh a, [$ffe6]
  128.     dec a
  129.     add a
  130.     ld b, a
  131.    
  132. ; If we ate the object in the last move, the snake only increases its length
  133.     ldh a, [$fffe]
  134.     and a
  135.     jr nz, .ateObject
  136.    
  137. ; Remove the snake's tail
  138.     ld a, [de]
  139.     ld h, a
  140.     inc de
  141.     ld a, [de]
  142.     ld l, a
  143.     dec de
  144.     ld a, $7f ; white tile
  145.     ld [hl], a
  146.    
  147. .loop
  148. ; move every snake tile one space
  149.     inc de
  150.     inc de
  151.     ld a, [de]
  152.     dec de
  153.     dec de
  154.     ld [de], a
  155.     inc de
  156.     dec b
  157.     jr nz, .loop
  158.     dec de
  159.     dec de ; de now points to previous snake head
  160.     jr .goOn
  161.  
  162. .ateObject
  163.     xor a
  164.     ldh [$fffe], a
  165.     ldh a, [$ffe6]
  166.     inc a
  167.     ldh [$ffe6], a ; increase snake length
  168.     dec a
  169.     dec a
  170. .loop2
  171.     inc de
  172.     inc de
  173.     dec a
  174.     jr nz, .loop2
  175. ; fallthrough  
  176.  
  177. .goOn
  178. ; de now points to snake head tile, load its content (tile occupied) to hl
  179.     ld a, [de]
  180.     ld h, a
  181.     inc de
  182.     ld a, [de]
  183.     ld l, a
  184.     inc de
  185. ; fallthrough  
  186.    
  187. ; Read user input from $ffa6
  188. ReadUserInput:
  189.     ldh a, [$ffa6] ; joypad register
  190.     and $F0 ; %11110000, R/L/U/D bits
  191.     jr nz, .newMovement ; key was pressed
  192.     ldh a, [$ffd0] ; take the last key pressed, which is also current movement direction
  193. .newMovement
  194.     ldh [$ffd0], a ; save last key pressed out of R/L/U/D
  195.     cp $10
  196.     jr z, MovePositionRight
  197.     cp $20
  198.     jr z, MovePositionLeft
  199.     cp $40
  200.     jr z, MovePositionUp
  201. ;   jr MovePositionDown
  202.  
  203. MovePositionDown: ; +$14
  204.     ld bc, $0014
  205.     jr MovePosition
  206. MovePositionUp: ; - $14
  207.     ld bc, $ffec
  208.     jr MovePosition
  209. MovePositionLeft: ; -1
  210.     ld bc, $ffff
  211.     jr MovePosition
  212. MovePositionRight: ; +1
  213.     ld bc, $0001
  214. ;   jr MovePosition
  215.  
  216. ; Calculate the new snake head and save it in the buffer
  217. MovePosition:
  218.     add hl, bc
  219.  
  220. ; Check if the snake ate the object
  221.     ld a, $74
  222.     cp [hl]
  223.     jr nz, .checkCollision
  224.     ldh [$fffe], a
  225.     jr .didEat
  226.  
  227. .checkCollision
  228. ; Check if the snake collided so the player lost
  229.     ld a, $7f
  230.     cp [hl]
  231.     jp nz, Restart ; dd61
  232.  
  233. .didEat
  234. ; Save the new snake head tile in the buffer and draw the new head
  235.     ld a, $24
  236.     ld [hl], a ; draw head
  237.     ld a, h
  238.     ld [de], a
  239.     inc de
  240.     ld a, l
  241.     ld [de], a
  242. ; fallthrough
  243.  
  244. ; Delay frames (the longer the snake is the faster the game goes)
  245. .delayFrames
  246.     ldh a, [$ffe6]
  247.     ld c, a
  248.     ld a, 40
  249.     sub c
  250.     sub c
  251.     ld c, a
  252.     call $033c ; DelayFrames
  253.    
  254. ; Finished this loop
  255.     ldh a, [$fffe] ; restore whether snake ate or not
  256.     and a
  257.     jp nz, DrawObject ; make sure to place a new object to replace the one just eaten ; ddb9
  258.     jp ShiftPositions ; skip placing an object and go back to the main loop ; dddc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement