Advertisement
Guest User

Snack Game Assenbly

a guest
Nov 25th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; this is the screen eating snake game...
  2. ;
  3. ; this game pushes the emulator to its limits,
  4. ; and even with maximum speed it still runs slowly.
  5. ; to enjoy this game it's recommended to run it on real
  6. ; computer, however the emulator can be useful to debug
  7. ; tiny games and other similar programs such as this before
  8. ; they become bug-free and workable.
  9. ;
  10. ; you can control the snake using arrow keys on your keyboard.
  11. ;
  12. ; all other keys will stop the snake.
  13. ;
  14. ; press esc to exit.
  15.  
  16.  
  17. name "snake"
  18.  
  19. org     100h
  20.  
  21. ; jump over data section:
  22. jmp     start
  23.  
  24. ; ------ data section ------
  25.  
  26. s_size  equ     2
  27.  
  28. ; the snake coordinates
  29. ; (from head to tail)
  30. ; low byte is left, high byte
  31. ; is top - [top, left]
  32. snake dw s_size dup(0)
  33.  
  34. tail    dw      ?
  35.  
  36. ; direction constants
  37. ;          (bios key codes):
  38. left    equ     4bh
  39. right   equ     4dh
  40. up      equ     48h
  41. down    equ     50h
  42.  
  43. ; current snake direction:
  44. cur_dir db      right
  45.  
  46. wait_time dw    0
  47.  
  48. ; welcome message
  49. msg     db "==== how to play ====", 0dh,0ah
  50.     db "this game was debugged on emu8086", 0dh,0ah
  51.     db "but it is not designed to run on the emulator", 0dh,0ah
  52.     db "because it requires relatively fast video card and cpu.", 0dh,0ah, 0ah
  53.    
  54.     db "if you want to see how this game really works,", 0dh,0ah
  55.     db "run it on a real computer (click external->run from the menu).", 0dh,0ah, 0ah
  56.    
  57.     db "you can control the snake using arrow keys", 0dh,0ah   
  58.     db "all other keys will stop the snake.", 0dh,0ah, 0ah
  59.    
  60.     db "press esc to exit.", 0dh,0ah
  61.     db "====================", 0dh,0ah, 0ah
  62.     db "press any key to start...$"
  63.  
  64. ; ------ code section ------
  65.  
  66. start:
  67.  
  68. ; print welcome message:
  69. mov dx, offset msg
  70. mov ah, 9
  71. int 21h
  72.  
  73.  
  74. ; wait for any key:
  75. mov ah, 00h
  76. int 16h
  77.  
  78.  
  79. ; hide text cursor:
  80. mov     ah, 1
  81. mov     ch, 2bh
  82. mov     cl, 0bh
  83. int     10h          
  84.  
  85.  
  86. game_loop:
  87.  
  88. ; === select first video page
  89. mov     al, 0  ; page number.
  90. mov     ah, 05h
  91. int     10h
  92.  
  93. ; === show new head:
  94. mov     dx, snake[0]
  95.  
  96. ; set cursor at dl,dh
  97. mov     ah, 02h
  98. int     10h
  99.  
  100. ; print '*' at the location:
  101. mov     al, '*'
  102. mov     ah, 09h
  103. mov     bl, 0eh ; attribute.
  104. mov     cx, 1   ; single char.
  105. int     10h
  106.  
  107. ; === keep the tail:
  108. mov     ax, snake[s_size * 2 - 2]
  109. mov     tail, ax
  110.  
  111. call    move_snake
  112.  
  113.  
  114. ; === hide old tail:
  115. mov     dx, tail
  116.  
  117. ; set cursor at dl,dh
  118. mov     ah, 02h
  119. int     10h
  120.  
  121. ; print ' ' at the location:
  122. mov     al, ' '
  123. mov     ah, 09h
  124. mov     bl, 0eh ; attribute.
  125. mov     cx, 1   ; single char.
  126. int     10h
  127.  
  128.  
  129.  
  130. check_for_key:
  131.  
  132. ; === check for player commands:
  133. mov     ah, 01h
  134. int     16h
  135. jz      no_key
  136.  
  137. mov     ah, 00h
  138. int     16h
  139.  
  140. cmp     al, 1bh    ; esc - key?
  141. je      stop_game  ;
  142.  
  143. mov     cur_dir, ah
  144.  
  145. no_key:
  146.  
  147.  
  148.  
  149. ; === wait a few moments here:
  150. ; get number of clock ticks
  151. ; (about 18 per second)
  152. ; since midnight into cx:dx
  153. mov     ah, 00h
  154. int     1ah
  155. cmp     dx, wait_time
  156. jb      check_for_key
  157. add     dx, 4
  158. mov     wait_time, dx
  159.  
  160.  
  161.  
  162. ; === eternal game loop:
  163. jmp     game_loop
  164.  
  165.  
  166. stop_game:
  167.  
  168. ; show cursor back:
  169. mov     ah, 1
  170. mov     ch, 0bh
  171. mov     cl, 0bh
  172. int     10h
  173.  
  174. ret
  175.  
  176. ; ------ functions section ------
  177.  
  178. ; this procedure creates the
  179. ; animation by moving all snake
  180. ; body parts one step to tail,
  181. ; the old tail goes away:
  182. ; [last part (tail)]-> goes away
  183. ; [part i] -> [part i+1]
  184. ; ....
  185.  
  186. move_snake proc near
  187.  
  188. ; set es to bios info segment:  
  189. mov     ax, 40h
  190. mov     es, ax
  191.  
  192.   ; point di to tail
  193.   mov   di, s_size * 2 - 2
  194.  ; move all body parts
  195.  ; (last one simply goes away)
  196.   mov   cx, s_size-1
  197. move_array:
  198.   mov   ax, snake[di-2]
  199.   mov   snake[di], ax
  200.   sub   di, 2
  201.   loop  move_array
  202.  
  203.  
  204. cmp     cur_dir, left
  205.   je    move_left
  206. cmp     cur_dir, right
  207.   je    move_right
  208. cmp     cur_dir, up
  209.   je    move_up
  210. cmp     cur_dir, down
  211.   je    move_down
  212.  
  213. jmp     stop_move       ; no direction.
  214.  
  215.  
  216. move_left:
  217.   mov   al, b.snake[0]
  218.   dec   al
  219.   mov   b.snake[0], al
  220.   cmp   al, -1
  221.   jne   stop_move      
  222.   mov   al, es:[4ah]    ; col number.
  223.   dec   al
  224.   mov   b.snake[0], al  ; return to right.
  225.   jmp   stop_move
  226.  
  227. move_right:
  228.   mov   al, b.snake[0]
  229.   inc   al
  230.   mov   b.snake[0], al
  231.   cmp   al, es:[4ah]    ; col number.  
  232.   jb    stop_move
  233.   mov   b.snake[0], 0   ; return to left.
  234.   jmp   stop_move
  235.  
  236. move_up:
  237.   mov   al, b.snake[1]
  238.   dec   al
  239.   mov   b.snake[1], al
  240.   cmp   al, -1
  241.   jne   stop_move
  242.   mov   al, es:[84h]    ; row number -1.
  243.   mov   b.snake[1], al  ; return to bottom.
  244.   jmp   stop_move
  245.  
  246. move_down:
  247.   mov   al, b.snake[1]
  248.   inc   al
  249.   mov   b.snake[1], al
  250.   cmp   al, es:[84h]    ; row number -1.
  251.   jbe   stop_move
  252.   mov   b.snake[1], 0   ; return to top.
  253.   jmp   stop_move
  254.  
  255. stop_move:
  256.   ret
  257. move_snake endp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement