Advertisement
Siapran

DCPU-16 snake

Oct 17th, 2012
2,699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;; Snake                   ;;
  3. ;; a game by Siapran       ;;
  4. ;; 16.10.2012 - 18.10.2012 ;;
  5. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  6.  
  7. :start                          ;used to loop the game
  8. set [frontx], 16                ;initialise game
  9. set [fronty], 8
  10. set [frontdir], 2
  11. set [backx], 10
  12. set [backy], 8
  13. set [backdir], 2
  14. set [applex], 0x0
  15. set [appley], 0x0
  16. set [score], 3
  17. set [atelength], 0
  18. jsr clearscreen
  19.  
  20. set a, 0                        ;print greetings message
  21. set b, GreetingsMessage
  22. set c, 0xf000
  23. jsr printstr
  24. set a, 0
  25. :wait4keyloop                   ;wait for user to press a key
  26. add a, 1
  27. ife [0x9000], 0
  28.   set pc, wait4keyloop
  29. set [seed], [0x9000]            ;use key data to make a random seed
  30. add [seed], a
  31. set [0x9000], 0                 ;reinit keyboard buffer
  32. jsr clearscreen                 ;clear screen
  33. set [0x8110], 0xFF02            ;initialise starting snake
  34. set [0x810F], 0xFF02
  35. set [0x810E], 0xFF02
  36. set [0x810D], 0xFF02
  37. set [0x810C], 0xFF02
  38. set [0x810B], 0xFF02
  39. :generatefirstapple             ;generate the first apple
  40.   set a, 0
  41.   set b, 15
  42.   jsr pseudorandom              ;random x
  43.   mul a, 2
  44.   set [applex], a
  45.   set a, 0
  46.   jsr pseudorandom              ;random y
  47.   set [appley], a
  48.   set b, [applex]
  49.   mul b, 32
  50.   add a, b
  51. ifn [0x8000+a], 0               ;check if apple appears in empty space
  52.   set pc, generatefirstapple    ; if not, try again
  53.  
  54. :mainloop                       ;main game loop
  55.   set a, [applex]
  56.   set b, [appley]
  57.   set c, 0xCC00
  58.   jsr printpixel                ;draw apple
  59.   set a, [backx]
  60.   set b, [backy]
  61.   set c, 0
  62.   jsr printpixel                ;erase back of snake
  63.   set a, [frontx]
  64.   set b, [fronty]
  65.   set c, 0xFF00
  66.   jsr printpixel                ;draw front of snake
  67.   set a, 0
  68.   set b, 0
  69.   set [0x9000], 0               ;reinit key buffer
  70.   :getkeyloop                   ;getkey loop (+speed regulation)
  71.     add a, 1
  72.     ifn [0x9000], 0
  73.       set b, [0x9000]           ;keycode stored in B
  74.     ifn a, [gamespeed]
  75.   set pc, getkeyloop
  76.   ifn b, 1                      ;if key=left
  77.     set pc, leftcond
  78.     ife [frontdir], 2           ;and if key!=right
  79.       set pc, leftcond
  80.         set [frontdir], b       ;set direction to left
  81.   :leftcond
  82.   ifn b, 2                      ;if key=right
  83.     set pc, rightcond
  84.     ife [frontdir], 1           ;same
  85.       set pc, rightcond
  86.         set [frontdir], b
  87.   :rightcond
  88.   ifn b, 3                      ;if key=up
  89.     set pc, upcond
  90.     ife [frontdir], 4           ;same
  91.       set pc, upcond
  92.         set [frontdir], b
  93.   :upcond
  94.   ifn b, 4                      ;if key=down
  95.     set pc, downcond
  96.     ife [frontdir], 3           ;same
  97.        set pc, downcond
  98.          set [frontdir], b
  99.   :downcond
  100.   set a, [fronty]
  101.   mul a, 32
  102.   add a, [frontx]
  103.   bor [0x8000+a], [frontdir]    ;record direction on matrix
  104.   set b, [frontdir]             ;move the front of the snake
  105.   ife b, 1
  106.     sub [frontx], 2
  107.   ife b, 2
  108.     add [frontx], 2
  109.   ife b, 3
  110.     sub [fronty], 1
  111.   ife b, 4
  112.     add [fronty], 1
  113.   mod [frontx], 32              ;check collisions with borders of screen
  114.   mod [fronty], 16
  115.   ifn [atelength], 0            ;if nothing is eaten
  116.     set pc, movebackelse        ; then move the back
  117.     set a, [backy]
  118.     mul a, 32
  119.     add a, [backx]
  120.     and [0x8000+a], 0xFF00      ;erase on matrix at pos x,y
  121.     set b, [backdir]            ;same as front
  122.     ife b, 1
  123.       sub [backx], 2
  124.     ife b, 2
  125.       add [backx], 2
  126.     ife b, 3
  127.       sub [backy], 1
  128.     ife b, 4
  129.       add [backy], 1
  130.     mod [backx], 32
  131.     mod [backy], 16
  132.     set a, [backy]
  133.     mul a, 32
  134.     add a, [backx]
  135.     set [backdir], [0x8000+a]   ;grab new diretion left by front on matrix
  136.     and [backdir], 0xFF
  137.   set pc, movebackend
  138.   :movebackelse
  139.     sub [atelength], 1          ; else digest
  140.   :movebackend
  141.   ifn [frontx], [applex]        ;if front on apple
  142.     set pc, eatapplecond
  143.     ifn [fronty], [appley]
  144.       set pc, eatapplecond
  145.         add [atelength], 1      ;eat it
  146.         add [score], 1          ;increment score
  147.         :newapplegen            ;spawn new apple
  148.           set a, 0
  149.           set b, 15
  150.           jsr pseudorandom
  151.           mul a, 2
  152.           set [applex], a
  153.           set a, 0
  154.           jsr pseudorandom
  155.           set [appley], a
  156.           set b, [applex]
  157.           mul b, 32
  158.           add a, b
  159.         ifn [0x8000+a], 0
  160.           set pc, newapplegen
  161.   :eatapplecond
  162.   set a, [fronty]
  163.   mul a, 32
  164.   add a, [frontx]
  165.   set b, [0x8000+a]
  166.   and b, 0xFF
  167. ife b, 0                        ;check for collision
  168.   set pc, mainloop              ;reiterate while snake didn't bite itself
  169. set a, 0                        ;print score
  170. set b, gameover
  171. set c, 0xF000
  172. jsr printstr
  173. set a, [score]                  ;convert score to srt
  174. set b, scorestr
  175. jsr reg2str
  176. set a, 6
  177. set c, 0xF000
  178. jsr printstr
  179. :wait4keyenter                  ;wait for user to press ENTER
  180.   set [0x9000], 0
  181.   :wait4keyenterloop
  182.   ife [0x9000], 0
  183.     set pc, wait4keyenterloop
  184. ifn [0x9000], 0xa
  185.   set pc, wait4keyenter
  186. set pc, start                   ;new game
  187.  
  188. set pc, end                     ;in case of hypothetical crash
  189.  
  190.  
  191.  
  192.  
  193. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  194. ;;           prints a massive 8*8 pixel
  195. ;;  parameters
  196. ;;      a - x
  197. ;;      b - y
  198. ;;      c - color (0xCC00 format)
  199. ;;  returns
  200. ;;      none
  201. :printpixel
  202. mul b, 32
  203. add a, b
  204. and [0x8000+a], 0xFF
  205. bor [0x8000+a], c
  206. add a, 1
  207. and [0x8000+a], 0xFF
  208. bor [0x8000+a], c
  209. set pc, pop
  210.  
  211. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  212. ;;           Generate a random number
  213. ;;  parameters
  214. ;;      A - minimum
  215. ;;      B - maximum
  216. ;;  returns
  217. ;;      A - The random number
  218. ;;  notes
  219. ;;      - Uses [seed] for the random seed and updates
  220. ;;        it with the last un-modulized number generated.
  221. :pseudorandom
  222. set push, x
  223. set push, i
  224. set x, [seed]
  225. mul x, 0xe3d1
  226. add x, 0x2b69
  227. shr x, 3
  228. set [seed], x                    ; new seed
  229. set i, b
  230. sub i, a
  231. mod x, i
  232. add x, a
  233. set a, x                         ; random number
  234. set i, pop
  235. set x, pop
  236. set pc, pop
  237.  
  238. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  239. ;;           Prints a string on the screen
  240. ;;  parameters
  241. ;;      A - relative position on screen (0x8000+A)
  242. ;;      B - pointer to str
  243. ;;      C - color (0xCC00)
  244. ;;  returns
  245. ;;      A - position of cursor on screen
  246. :printstr
  247.   set [0x8000+a], [b]          ;print char on screen
  248.   bor [0x8000+a], c            ;add color
  249.   add a, 1                     ;increment screen position
  250.   add b, 1                     ;increment pointer
  251. ifn [b], 0
  252.   set pc, printstr             ;loopwhile not at the end of the chain
  253. set pc, pop                    ;end of function
  254.  
  255. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  256. ;;           Clears screen
  257. ;;  parameters
  258. ;;      none
  259. ;;  returns
  260. ;;      your porn (good guy)
  261. :clearscreen
  262. set push, a
  263. set a, 0x8000
  264.   :clearscreenloop
  265.     set [a], 0x0
  266.     add a, 1
  267.   ifn a, 0x8200
  268.     set pc, clearscreenloop
  269. set a, pop
  270. set pc, pop
  271.  
  272. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  273. ;;           Register to String
  274. ;;  parameters
  275. ;;      A - register
  276. ;;      B - pointer to new str
  277. ;;  returns
  278. ;;      B - pointer to str
  279. :reg2str
  280.   set push, c
  281.   set push, b
  282.   add b, 5
  283.   :reg2strloop
  284.     set c, a
  285.     mod c, 10
  286.     add c, 0x30
  287.     sub b, 1
  288.     set [b], c
  289.     div a, 10
  290.   ifn b, peek
  291.     set pc, reg2strloop
  292.   set b, pop
  293.   set c, pop
  294.   set pc, pop
  295.  
  296. ;==========================================;
  297. ; Variables                                ;
  298. ;==========================================;
  299. :GreetingsMessage dat "SNAKE            code by SiapranPRESS ANY KEY", 0x0
  300. :gameover dat "SCORE:", 0x0
  301. :seed dat 0x0
  302. :front
  303. :frontx dat 16
  304. :fronty dat 8
  305. :frontdir dat 2
  306. :back dat "ass"
  307. :backx dat 10
  308. :backy dat 8
  309. :backdir dat 2
  310. :applex dat 0x0
  311. :appley dat 0x0
  312. :score dat 3
  313. :atelength dat 0
  314. :gamespeed dat 0x500
  315. :scorestr dat "00000", 0x0
  316.  
  317. :end set PC, end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement