Advertisement
TermSpar

Assembly Snake Game

Nov 21st, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; by Ben Bollinger
  2.  
  3. INCLUDE Irvine32.inc
  4.  
  5. .data
  6.  
  7. strWelcome BYTE "Welcome to an x86 Assembly Game by Ben Bollinger (WASD to move, x to exit)",0
  8. strLine BYTE "--------------------------------------------------------------------------",0
  9.  
  10. inputChar BYTE ?
  11.  
  12. xPos BYTE ?
  13. yPos BYTE ?
  14.  
  15. xApplePos BYTE ?
  16. yApplePos BYTE ?
  17.  
  18. isColliding BYTE 'F'
  19. right BYTE 'F'
  20. left BYTE 'F'
  21. up BYTE 'F'
  22. down BYTE 'F'
  23.  
  24. applesEaten DWORD 0
  25. adder DWORD 2
  26.  
  27. .code
  28. main PROC
  29.     ; display welcome message:
  30.     mov  dl,1
  31.     mov  dh,0
  32.     call Gotoxy
  33.     mov edx,OFFSET strWelcome
  34.     call WriteString
  35.     mov  dl,1
  36.     mov  dh,1
  37.     call Gotoxy
  38.     mov edx,0
  39.     mov edx,OFFSET strLine
  40.     call WriteString
  41.  
  42.     call DrawRandomApple
  43.  
  44.     ; set init player position:
  45.     mov xPos,10
  46.     mov yPos,10
  47.  
  48.     call ReadChar
  49.     mov inputChar,al
  50.  
  51.     ; start game loop:
  52.     gameLoop:
  53.         call DrawAppleCont
  54.         ; delay for 80ms
  55.         mov eax,40
  56.         call Delay
  57.  
  58.         ; read keybuffer into al:
  59.         call ReadKey
  60.  
  61.         ;------------------------------------------------------------------------------
  62.         ; make sure keybuffer only goes to inputChar if it's one of the specified keys:
  63.         cmp al,"w"
  64.         jne checkS
  65.         mov inputChar,al
  66.  
  67.         checkS:
  68.         cmp al,"s"
  69.         jne checkA
  70.         mov inputChar,al
  71.  
  72.         checkA:
  73.         cmp al,"a"
  74.         jne checkD
  75.         mov inputChar,al
  76.  
  77.         checkD:
  78.         cmp al,"d"
  79.         jne skip
  80.         mov inputChar,al
  81.  
  82.         skip:
  83.         ;------------------------------------------------------------------------------
  84.  
  85.         ; check for game exit:
  86.         cmp inputChar,"x"
  87.         je exitGame
  88.  
  89.         ; check for 'w' (up):
  90.         cmp inputChar,"w"
  91.         je moveUp
  92.        
  93.         ; check for 's' (down):
  94.         cmp inputChar,"s"
  95.         je moveDown
  96.  
  97.         ; check for 'd' (right):
  98.         cmp inputChar,"d"
  99.         je moveRight
  100.  
  101.         ; check for 'a' (left):
  102.         cmp inputChar,"a"
  103.         je moveLeft
  104.         jmp gameLoop
  105.  
  106.         ; move the player up:
  107.         moveUp:
  108.             ; clear current player location:
  109.             call RefreshPlayer
  110.  
  111.             ; decrement yPos (move up) and draw Player:
  112.             dec yPos
  113.             call DrawPlayer
  114.            
  115.             ; check apple collisions:
  116.             call CheckAppleCollision
  117.             cmp isColliding,'T'
  118.             jne noCollide1
  119.             ; if collision happened:
  120.             inc applesEaten
  121.             call DrawRandomApple
  122.             noCollide1:
  123.  
  124.             mov right,'F'
  125.             mov left,'F'
  126.             mov down,'F'
  127.             mov up,'T'
  128.  
  129.             ; reset input and repeat loop:
  130.             jmp gameLoop
  131.  
  132.         ; move the player down:
  133.         moveDown:
  134.             ; clear current player location:
  135.             call RefreshPlayer
  136.  
  137.             ; increment yPos (move down) and draw Player:
  138.             inc yPos
  139.             call DrawPlayer
  140.  
  141.             ; check apple collisions:
  142.             call CheckAppleCollision
  143.             cmp isColliding,'T'
  144.             jne noCollide2
  145.             ; if collision happened:
  146.             inc applesEaten
  147.             call DrawRandomApple
  148.             noCollide2:
  149.  
  150.             mov right,'F'
  151.             mov left,'F'
  152.             mov down,'T'
  153.             mov up,'F'
  154.  
  155.             ; reset input and repeat loop:
  156.             jmp gameLoop
  157.  
  158.         ; move the player right:
  159.         moveRight:
  160.             ; clear current player location:
  161.             call RefreshPlayer
  162.  
  163.             ; increment yPos (move down) and draw Player:
  164.             inc xPos
  165.             call DrawPlayer
  166.  
  167.             ; check apple collisions:
  168.             call CheckAppleCollision
  169.             cmp isColliding,'T'
  170.             jne noCollide3
  171.             ; if collision happened:
  172.             inc applesEaten
  173.             call DrawRandomApple
  174.             noCollide3:
  175.  
  176.             mov right,'T'
  177.             mov left,'F'
  178.             mov down,'F'
  179.             mov up,'F'
  180.  
  181.             ; reset input and repeat loop:
  182.             jmp gameLoop
  183.  
  184.         ; move the player left:
  185.         moveLeft:
  186.             ; clear current player location:
  187.             call RefreshPlayer
  188.  
  189.             ; increment yPos (move down) and draw Player:
  190.             dec xPos
  191.             call DrawPlayer
  192.  
  193.             ; check apple collisions:
  194.             call CheckAppleCollision
  195.             cmp isColliding,'T'
  196.             jne noCollide4
  197.             ; if collision happened:
  198.             inc applesEaten
  199.             call DrawRandomApple
  200.             noCollide4:
  201.  
  202.             mov right,'F'
  203.             mov left,'T'
  204.             mov down,'F'
  205.             mov up,'F'
  206.  
  207.             ; reset input and repeat loop:
  208.             jmp gameLoop
  209.  
  210.         jmp gameLoop
  211.  
  212.     exitGame:
  213.     exit
  214. main ENDP
  215.  
  216. ; clear current player so to draw at new location:
  217. RefreshPlayer PROC
  218.     ; get the previous location of the player:
  219.     mov  dl,xPos
  220.     mov  dh,yPos
  221.  
  222.     ; as the player eats more apples, cover up less spots behind him:
  223.     cmp applesEaten,0
  224.     je noApples
  225.     mov ecx,applesEaten
  226.     playerLoop:
  227.         cmp right,'T'
  228.         jne goingLeft
  229.         dec dl
  230.  
  231.         goingLeft:
  232.         cmp left,'T'
  233.         jne goingUp
  234.         inc dl
  235.        
  236.         goingUp:
  237.         cmp up,'T'
  238.         jne goingDown
  239.         inc dh
  240.  
  241.         goingDown:
  242.         cmp down,'T'
  243.         jne finishLoop
  244.         dec dh
  245.  
  246.         finishLoop:
  247.     loop playerLoop
  248.  
  249.     call DeletePlayerPixels
  250.  
  251.     cmp up,'T'
  252.         jne checkDown
  253.         call GetAdder
  254.         ; as the body expands, the area of deletion expands proportionally:
  255.         push ecx
  256.         add esi,edi
  257.         mov ecx,esi
  258.         upLoop1:
  259.         dec dl
  260.         call DeletePlayerPixels
  261.         loop upLoop1
  262.         pop ecx
  263.  
  264.         push ecx
  265.         inc edi
  266.         add esi,edi
  267.         mov ecx,esi
  268.         upLoop2:
  269.         inc dl
  270.         call DeletePlayerPixels
  271.         loop upLoop2
  272.         pop ecx
  273.     checkDown:
  274.     cmp down,'T'
  275.     jne checkRight
  276.         push ecx
  277.         add applesEaten,2
  278.         mov ecx,applesEaten
  279.         downLoop1:
  280.         inc dl
  281.         call DeletePlayerPixels
  282.         loop downLoop1
  283.         pop ecx
  284.  
  285.         push ecx
  286.         add applesEaten,3
  287.         mov ecx,applesEaten
  288.         downLoop2:
  289.         dec dl
  290.         call DeletePlayerPixels
  291.         loop downLoop2
  292.         pop ecx
  293.         sub applesEaten,5
  294.     checkRight:
  295.     cmp right,'T'
  296.         jne checkLeft
  297.         push ecx
  298.         add applesEaten,2
  299.         mov ecx,applesEaten
  300.         rightLoop1:
  301.         dec dh
  302.         call DeletePlayerPixels
  303.         loop rightLoop1
  304.         pop ecx
  305.  
  306.         push ecx
  307.         add applesEaten,3
  308.         mov ecx,applesEaten
  309.         rightLoop2:
  310.         inc dh
  311.         call DeletePlayerPixels
  312.         loop rightLoop2
  313.         pop ecx
  314.         sub applesEaten,5
  315.     checkLeft:
  316.     cmp left,'T'
  317.         jne noApples
  318.         push ecx
  319.         add applesEaten,2
  320.         mov ecx,applesEaten
  321.         leftLoop1:
  322.         inc dh
  323.         call DeletePlayerPixels
  324.         loop leftLoop1
  325.         pop ecx
  326.  
  327.         push ecx
  328.         add applesEaten,3
  329.         mov ecx,applesEaten
  330.         leftLoop2:
  331.         dec dh
  332.         call DeletePlayerPixels
  333.         loop leftLoop2
  334.         pop ecx
  335.         sub applesEaten,5
  336.     noApples:
  337. RefreshPlayer ENDP
  338.  
  339. GetAdder PROC
  340.     mov esi,applesEaten
  341.     add esi,adder
  342.     mov edi,esi
  343.     mov esi,applesEaten
  344. GetAdder ENDP
  345.  
  346. DeletePlayerPixels PROC
  347.     call Gotoxy
  348.     push eax
  349.     ; draw a blank space:
  350.     mov eax,black (black * 16)
  351.     call SetTextColor
  352.     pop eax
  353.     mov al," "
  354.     call WriteChar
  355.     ret
  356. DeletePlayerPixels ENDP
  357.  
  358. ; draw player to current location:
  359. DrawPlayer PROC
  360.     push eax
  361.     mov eax,white  (white * 16)
  362.     call SetTextColor
  363.     pop eax
  364.  
  365.     mov  dl,xPos
  366.     mov  dh,yPos
  367.     call Gotoxy
  368.     mov al,"X"
  369.     call WriteChar
  370.     ret
  371. DrawPlayer ENDP
  372.  
  373. DrawAppleCont PROC
  374.     ; draw a red apple:
  375.     push eax
  376.     mov eax,red  (red * 16)
  377.     call SetTextColor
  378.     pop eax
  379.     push edx
  380.     mov dl,xApplePos
  381.     mov dh,yApplePos
  382.     call Gotoxy
  383.     pop edx
  384.     mov al,"X"
  385.     call WriteChar
  386.     ret
  387. DrawAppleCont ENDP
  388.  
  389. DrawRandomApple PROC
  390.     push edx
  391.     ; generate random x position:
  392.     mov eax,30
  393.     call RandomRange
  394.     mov dl,al
  395.     ; generate random y position:
  396.     mov eax,30
  397.     call RandomRange
  398.     mov dh,al
  399.     call Gotoxy
  400.  
  401.     ; log the apple's position:
  402.     mov xApplePos,dl
  403.     mov yApplePos,dh
  404.     pop edx
  405.     ret
  406. DrawRandomApple ENDP
  407.  
  408. CheckAppleCollision PROC
  409.     ; if apple and player positions are equal, set isColliding to 'T'
  410.     mov bl,xPos
  411.     cmp xApplePos,bl
  412.     jne notColliding
  413.  
  414.     mov bl,yPos
  415.     cmp yApplePos,bl
  416.     jne notColliding
  417.     mov isColliding,'T'
  418.     jmp exitCheck
  419.  
  420.     ; otherwise set it to 'F'
  421.     notColliding:
  422.     mov isColliding,'F'
  423.  
  424.     exitCheck:
  425.     ret
  426. CheckAppleCollision ENDP
  427.  
  428. END main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement