Soron

DCPU Snake game

Apr 7th, 2012
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. ; Note: CLOBBERS REGISTERS
  2. ; Use my v2 (available on this site) if you want to have it play nice
  3.  
  4. ; coded by Soron of http://0x10cforum.com/, for the DCPU
  5. ; hereby released into the public domain
  6. ; (or use the Unlicense, CC0, etc., if your country doesn't have public domain)
  7.  
  8. ; targetted for the DCPU-16 Studio assembler/emulator
  9. ; may or may not work for other emulators, since I/O hasn't been made official
  10.  
  11. ; the snake is drawn on the background, and the food on the foreground
  12.  
  13. ; suggested tweaks:
  14. ; adjust sleep_duration to speed up/slow down the game
  15. ; adjust the upper nybble of food_char to change food color
  16. ; adjust the lower byte of food_char to change food shape
  17. ; adjust prng_dat to start with a different RNG seed
  18. ; if you're not using a 32x16 window, adjust assorted things to not break
  19. ; (things such as dat_width and dat_height, up_dir and down_dir, etc.)
  20.  
  21. ; note: assumes that it'll be run as a standalone program
  22.  
  23. ; this could also probably be made more efficient, but, uh... not a priority,
  24. ; given that we're looping 0x1000 times just to slow things down!
  25.  
  26. ; directions:
  27. ; 0x0001 = right
  28. ; 0x0020 = down (assuming a 32 char-width screen)
  29. ; 0xFFFF = left
  30. ; 0xFFE0 = up
  31. ; up/down WILL need to be changed if you use a different-width screen
  32.  
  33. ; how registers, etc., are used:
  34. ; A = head position
  35. ; B = direction
  36. ; C = food position
  37. ; X = position of thing to draw on screen, etc
  38. ; Y = position of last segment touched
  39. ; Z = temporary data storage, for assorted calculations
  40. ; I = for loops and other iterations
  41. ; J = address of the stack element which stores the pos of start of tail
  42. ; SP = address of the stack element which stores the pos of end of tail
  43. ; O = not used. Why would you use this for Snake? What sort of madman ARE you?
  44.  
  45. :init
  46. JSR clear_screen
  47. SET A, 0x0001 ; head position (add 0x8000 to get screen pos)
  48. SET B, 0x0001 ; direction
  49. JSR gen_food
  50. SET J, SP ; keep track of where OUR stack starts
  51. SET PUSH, 0x0000 ; start of tail position
  52. BOR [0x8000], [tail_color]
  53. BOR [0x8001], [head_color]
  54.  
  55. :game_loop
  56. JSR sleep
  57.  
  58. IFE [0x9000], [up_key]
  59. SET PC, turn_up
  60. IFE [0x9000], [down_key]
  61. SET PC, turn_down
  62. IFE [0x9000], [left_key]
  63. SET PC, turn_left
  64. IFE [0x9000], [right_key]
  65. SET PC, turn_right
  66.  
  67. :move_snake
  68. SET [0x9000], 0
  69. SET Y, A
  70. ADD A, B ; yay overflow ^_^!
  71. SET X, A
  72.  
  73. :check_collision
  74. SET Z, [dat_width]
  75. MUL Z, [dat_height]
  76. IFG X, Z ; went off the screen somehow
  77. SET PC, lose_game
  78.  
  79. SET Z, X
  80. ADD Z, 0x8000
  81. IFE [Z], [tail_color] ; hit the tail!
  82. SET PC, lose_game
  83.  
  84. IFE B, [dir_left]
  85. SET PC, check_edge_collision
  86. IFE B, [dir_right]
  87. SET PC, check_edge_collision
  88. SET PC, draw_snake
  89.  
  90. :check_edge_collision
  91. ;change this next block if you changed screen width
  92. SET X, A
  93. AND X, 0xFFE0
  94. SET Z, Y
  95. AND Z, 0xFFE0
  96. IFN X, Z
  97. SET PC, lose_game ; we have gone off the left/right edge!
  98. SET X, A
  99.  
  100.  
  101. :draw_snake
  102. ADD X, 0x8000
  103. BOR [X], [head_color]
  104.  
  105. SET I, J
  106. :tail_loop
  107. SUB I, 1
  108. IFG SP, I
  109. SET PC, finish_tail
  110. SET Z, [I]
  111. SET [I], Y
  112. SET Y, Z
  113.  
  114. SET X, [I]
  115. ADD X, 0x8000
  116. BOR [X], [tail_color]
  117. SET PC, tail_loop
  118.  
  119. :turn_left
  120. IFN B, [dir_right]
  121. SET B, [dir_left]
  122. SET PC, move_snake
  123.  
  124. :turn_right
  125. IFN B, [dir_left]
  126. SET B, [dir_right]
  127. SET PC, move_snake
  128.  
  129. :turn_up
  130. IFN B, [dir_down]
  131. SET B, [dir_up]
  132. SET PC, move_snake
  133.  
  134. :turn_down
  135. IFN B, [dir_up]
  136. SET B, [dir_down]
  137. SET PC, move_snake
  138.  
  139.  
  140. :finish_tail
  141. IFE A, C
  142. SET PC, grow_tail
  143. SET X, Y
  144. ADD X, 0x8000
  145. AND [X], 0xF0FF
  146. SET PC, game_loop
  147.  
  148. :grow_tail
  149. SET PUSH, Y
  150. SET X, C
  151. ADD X, 0x8000
  152. AND [X], 0x0F00 ; make sure to clear the old food item!
  153. JSR gen_food
  154. SET PC, game_loop
  155.  
  156. :gen_food
  157. JSR rand
  158. SET C, [prng_dat]
  159. SET Z, [dat_width]
  160. MUL Z, [dat_height]
  161. MOD C, Z
  162. SET X, C
  163. ADD X, 0x8000
  164. BOR [X], [food_char]
  165. SET PC, POP
  166.  
  167.  
  168. :lose_game
  169. SET PC, init
  170.  
  171. :sleep
  172. SET I, 0
  173. :sleep_loop
  174. ADD I, 1
  175. IFG I, [sleep_duration]
  176. SET PC, POP
  177. SET PC, sleep_loop
  178.  
  179. :sleep_duration
  180. DAT 0x1000
  181.  
  182.  
  183. :clear_screen
  184. SET A, SP
  185. SET SP, 0x8000
  186. :clear_screen_loop
  187. SET POP, 0
  188. IFG SP, 0x8200
  189. SET PC, clear_screen_exit
  190. SET PC, clear_screen_loop
  191. :clear_screen_exit
  192. SET SP, A
  193. SET PC, POP
  194.  
  195. :rand
  196. ;introduce at least a LITTLE unpredictability
  197. MUL [prng_dat], A
  198. ADD [prng_dat], SP
  199. ;now do the actual work
  200. MUL [prng_dat], 31421
  201. ADD [prng_dat], 6927
  202. SET PC, POP
  203.  
  204. :prng_dat
  205. DAT 0x13AD ; a reasonable start value
  206.  
  207. ;if you change width, you WILL need to double-check edge-of-screen collision
  208. :dat_width
  209. DAT 32
  210. :dat_height
  211. DAT 16
  212.  
  213. :head_color
  214. DAT 0x0700
  215. :tail_color
  216. DAT 0x0F00
  217. :food_char
  218. DAT 0xC040 ; "@" char, for all y'all roguelike fans :)
  219.  
  220. :dir_right
  221. DAT 0x0001
  222. :dir_down
  223. DAT 0x0020 ; relies on screen width
  224. :dir_left
  225. DAT 0xFFFF
  226. :dir_up
  227. DAT 0xFFE0 ; relies on screen width
  228.  
  229. :up_key
  230. DAT 0x0003
  231. :down_key
  232. DAT 0x0004
  233. :left_key
  234. DAT 0x0001
  235. :right_key
  236. DAT 0x0002
Advertisement
Add Comment
Please, Sign In to add comment