Advertisement
Guest User

Snake game

a guest
May 14th, 2014
722
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;**********************************************************;
  2. ;*******Snake game developed by Saitei*********************;
  3. ;**********************************************************;
  4. ;**********************************************************;
  5.  
  6. stsg    segment para    stack   'stack'
  7.     dw  32  dup(?)  ;64 bytes on the stack
  8. stsg    ends
  9.  
  10. MAP_WIDTH = 78
  11. MAP_HEIGHT = 21
  12. START_SNAKE_SIZE = 10
  13. START_PAUSE_STATE = 0  
  14. SLEEP_TIME = 1      ;for limiting FPS      
  15. EXIT_STATE = 0
  16.  
  17. ;***************Dirs enum*****************;
  18. LEFT = 0
  19. UP = 1
  20. RIGHT = 2
  21. DOWN = 3
  22. ;*****************************************;
  23.  
  24. ;***************Colors enum***************;
  25. YELLOW = 1110b
  26. WHITE = 1111b
  27. RED = 1100b
  28. GREEN = 0010b
  29. ;*****************************************;
  30.  
  31. SNAKE   STRUC
  32.     x   db  ?
  33.     y   db  ?
  34. SNAKE   ENDS
  35.  
  36. ;***!!!***;
  37. SNAKE_STRUC_SIZE = 2
  38. ;*********;
  39.  
  40. dssg    segment para    'data'
  41.     snake_elements  SNAKE   255 dup(?)
  42.     snake_size  db  START_SNAKE_SIZE
  43.     snake_dir   db  LEFT
  44.     snake_char  db  254,0
  45.     empty_char  db  ' ',0  
  46.     border_horizont_char    db  196,0
  47.     border_vert_char    db  179,0
  48.     border_ugl_lu       db  218,0
  49.     border_ugl_ru       db  191,0
  50.     border_ugl_ld       db  192,0
  51.     border_ugl_rd       db  217,0
  52.     random_seed_x       dw  MAP_WIDTH
  53.     random_seed_y       dw  MAP_HEIGHT
  54.     food_x      db  ?
  55.     food_y      db  ?
  56.     food_char   db  6,0
  57.     pause       db  START_PAUSE_STATE
  58.     score       dw  0
  59.     exit        db  0  
  60. dssg    ends
  61.  
  62.  
  63. cdsg    segment para    'code'
  64.     assume  cs:cdsg, ds:dssg, ss:stsg
  65.  
  66. cls proc
  67.     pusha
  68.     mov ax,0600h
  69.     mov bh,7
  70.     mov cx,0
  71.     mov dx,184fh
  72.     int 10h
  73.     popa
  74.     ret
  75. endp   
  76.  
  77. PrintColored    macro   ch,_color
  78.     pusha
  79.     mov ah,09
  80.     mov al,ch
  81.     mov bh,00
  82.     mov bx,_color
  83.     mov cx,01
  84.     int 10h
  85.     popa
  86. endm
  87.  
  88. randx proc near
  89. push dx cx
  90. mov ax, random_seed_x
  91. dec ax
  92. xor dx,dx
  93. mov cx,12773
  94. div cx
  95. mov cx,ax
  96. mov ax,16807
  97. mul dx
  98. mov dx,cx
  99. mov cx,ax
  100. mov ax,2836
  101. mul dx
  102. sub cx,ax
  103. xor dx,dx
  104. mov ax,cx
  105. mov random_seed_x, cx
  106. mov cx,10000
  107. div cx
  108. mov ax,dx
  109. inc ax
  110. pop cx dx
  111. ret
  112. randx endp
  113.  
  114. randy proc near
  115. push dx cx
  116. mov ax, random_seed_y
  117. dec ax
  118. xor dx,dx
  119. mov cx,12773
  120. div cx
  121. mov cx,ax
  122. mov ax,16807
  123. mul dx
  124. mov dx,cx
  125. mov cx,ax
  126. mov ax,2836
  127. mul dx
  128. sub cx,ax
  129. xor dx,dx
  130. mov ax,cx
  131. mov random_seed_y, cx
  132. mov cx,10000
  133. div cx
  134. mov ax,dx
  135. inc ax
  136. pop cx dx
  137. ret
  138. randy endp
  139.  
  140. PlaceFood   macro
  141.     push ax
  142.     call    randx
  143.     mov food_x,al
  144.     call    randy
  145.     mov food_y,al
  146.     pop ax
  147. endm
  148.  
  149. DrawFood    macro
  150.     SetCurPos   food_x,food_y
  151.     Print   food_char
  152. endm
  153.  
  154.  
  155.  
  156. DrawBorder  macro
  157.     SetCurPos   0,0
  158.     PrintColored    border_ugl_lu,YELLOW
  159.     SetCurPos   MAP_WIDTH+1,0
  160.     PrintColored    border_ugl_ru,YELLOW
  161.     SetCurPos   0,MAP_HEIGHT+1
  162.     PrintColored    border_ugl_ld,YELLOW
  163.     SetCurPos   MAP_WIDTH+1,MAP_HEIGHT+1
  164.     PrintColored    border_ugl_rd,YELLOW
  165.     mov al,1    ;x
  166.     mov cx,MAP_WIDTH
  167.     UL:
  168.         SetCurPos   al,0
  169.         PrintColored    border_horizont_char,YELLOW
  170.         inc al 
  171.     loop    UL 
  172.     mov al,1    ;x
  173.     mov cx,MAP_WIDTH
  174.     _DL:
  175.         SetCurPos   al,MAP_HEIGHT+1
  176.         PrintColored    border_horizont_char,YELLOW
  177.         inc al
  178.     loop    _DL
  179.     mov al,1    ;y
  180.     mov cx,MAP_HEIGHT
  181.     LL:
  182.         SetCurPos   0,al
  183.         PrintColored    border_vert_char,YELLOW
  184.         inc al 
  185.     loop    LL 
  186.     mov al,1    ;y
  187.     mov cx,MAP_HEIGHT
  188.     RL:
  189.         SetCurPos   MAP_WIDTH+1,al
  190.         PrintColored    border_vert_char,YELLOW
  191.         inc al
  192.     loop    RL
  193. endm
  194.  
  195.  
  196.  
  197. HideBlinkingCursor  macro
  198.     push    cx
  199.     push    ax
  200.     mov ch,32
  201.     mov ah,1
  202.     int 10h
  203.     pop     ax
  204.     pop cx
  205. endm
  206.  
  207. CreateSnake proc
  208.     mov al,11   ;x
  209.     mov ah,4    ;y
  210.     xor cx,cx
  211.     mov cl,snake_size
  212.     xor si,si   ;index for the array of structures snake_elements
  213.     SNAKE_CREATION:
  214.         mov snake_elements[si].x,al
  215.         mov snake_elements[si].y,ah
  216.         inc al
  217.         add si,SNAKE_STRUC_SIZE
  218.     loop    SNAKE_CREATION
  219. ret
  220. endp
  221.  
  222. SetCurPos   macro   x,y
  223.     pusha
  224.     mov bh,0   
  225.     mov dh,y
  226.     mov dl,x
  227.     mov ah,2
  228.     int 10h
  229.     popa
  230. endm
  231.  
  232. Preparation proc
  233.     call    CreateSnake
  234.     ret    
  235. endp
  236.  
  237. MoveSnake   macro
  238.     mov al,snake_size
  239.     mov     bl,SNAKE_STRUC_SIZE
  240.     mul bl
  241.     mov si,ax
  242.     xor     cx,cx
  243.     mov cl,snake_size
  244.     MCC:
  245.         mov al,snake_elements[si-SNAKE_STRUC_SIZE].x
  246.         mov ah,snake_elements[si-SNAKE_STRUC_SIZE].y
  247.         mov snake_elements[si].x,al
  248.         mov snake_elements[si].y,ah
  249.         sub si,SNAKE_STRUC_SIZE
  250.     loop    MCC
  251.     cmp snake_dir,LEFT
  252.     je  MoveLeft
  253.     cmp snake_dir,UP
  254.     je  MoveUp
  255.     cmp snake_dir,RIGHT
  256.     je  MoveRight
  257.     cmp snake_dir,DOWN
  258.     je  MoveDown   
  259.     MoveLeft:
  260.         cmp snake_elements[0].x,1
  261.         je  MLTR
  262.         dec snake_elements[0].x
  263.         jmp MLEXIT
  264.         MLTR:
  265.         mov snake_elements[0].x,MAP_WIDTH
  266.         MLEXIT:    
  267.         jmp SnakeMoveExit
  268.     MoveUp:
  269.         cmp snake_elements[0].y,1
  270.         je  MUTD
  271.         dec snake_elements[0].y
  272.         jmp MUEXIT
  273.         MUTD:
  274.         mov snake_elements[0].y,MAP_HEIGHT
  275.         MUEXIT:
  276.         jmp SnakeMoveExit
  277.     MoveRight:
  278.         cmp snake_elements[0].x,MAP_WIDTH
  279.         je  MRTL
  280.         inc snake_elements[0].x
  281.         jmp MREXIT
  282.         MRTL:
  283.         mov snake_elements[0].x,1      
  284.         MREXIT:
  285.         jmp SnakeMoveExit
  286.     MoveDown:
  287.         cmp snake_elements[0].y,MAP_HEIGHT
  288.         je  MDTU
  289.         inc snake_elements[0].y
  290.         jmp MDEXIT
  291.         MDTU:
  292.         mov snake_elements[0].y,1
  293.         MDEXIT:
  294.         jmp SnakeMoveExit
  295.     SnakeMoveExit:     
  296. endm
  297.  
  298. KeysUpdate  macro
  299.     pusha
  300.     mov ah,0bh
  301.     int 21h
  302.     cmp al,0ffh
  303.     jne KUEXIT
  304.     ClearRegisters
  305.     mov ah,07h
  306.     int 21h
  307.     cmp al,'w'
  308.     je  MUP
  309.     cmp al,'W'
  310.     je  MUP
  311.     cmp al,'a'
  312.     je  MLEFT
  313.     cmp al,'A'
  314.     je  MLEFT
  315.     cmp al,'s'
  316.     je  MDOWN
  317.     cmp al,'S'
  318.     je  MDOWN
  319.     cmp al,'d'
  320.     je  MRIGHT
  321.     cmp al,'D'
  322.     je  MRIGHT
  323.     jmp KUEXIT
  324.     MUP:
  325.         cmp snake_dir,DOWN
  326.         je  KUEXIT
  327.         mov snake_dir,UP
  328.     jmp KUEXIT
  329.     MLEFT:
  330.         cmp snake_dir,RIGHT
  331.         je  KUEXIT
  332.         mov snake_dir,LEFT
  333.     jmp KUEXIT
  334.     MDOWN:
  335.         cmp snake_dir,UP
  336.         je  KUEXIT
  337.         mov snake_dir,DOWN
  338.     jmp KUEXIT
  339.     MRIGHT:
  340.         cmp snake_dir,LEFT
  341.         je  KUEXIT
  342.         mov snake_dir,RIGHT
  343.     jmp KUEXIT
  344.     KUEXIT:    
  345.     popa
  346. endm
  347.  
  348. CheckSnakeSnakeCollision    macro
  349.     pusha
  350.     xor cx,cx
  351.     mov cl,snake_size
  352.     dec cx
  353.     mov al,snake_size
  354.     mov bl,SNAKE_STRUC_SIZE
  355.     mul bl
  356.     mov si,ax
  357.     CSSC:
  358.         mov bl,snake_elements[si].x
  359.         cmp snake_elements[0].x,bl
  360.         je  CheckY
  361.         Back:
  362.     loop    CSSC
  363.     jmp CSSCEXIT
  364.     CheckY:
  365.         mov bl,snake_elements[si].y
  366.         cmp snake_elements[0].y,bl
  367.         jne Back
  368.         call    Restart
  369.     CSSCEXIT:
  370.     popa
  371. endm
  372.  
  373. CheckSnakeFoodCollision macro
  374.     push    ax
  375.     mov al,food_x
  376.     cmp snake_elements[0].x,al
  377.     je  CSFCY
  378.     jmp _Exit
  379.     CSFCY:
  380.     mov al,food_y
  381.     cmp snake_elements[0].y,al
  382.     je  NN
  383.     jmp _Exit
  384.     NN:
  385.     PlaceFood
  386.     _Exit:
  387.     pop ax
  388. endm
  389.    
  390. CheckCollision  macro
  391.     CheckSnakeSnakeCollision
  392.     CheckSnakeFoodCollision
  393. endm
  394.  
  395.  
  396.  
  397. Update  macro
  398.     mov al,snake_size
  399.     mov bl,SNAKE_STRUC_SIZE
  400.     mul bl
  401.     mov si,ax
  402.     SetCurPos   snake_elements[si-SNAKE_STRUC_SIZE].x,snake_elements[si-SNAKE_STRUC_SIZE].y
  403.     Print   empty_char
  404.     KeysUpdate
  405.     MoveSnake
  406.     CheckCollision
  407. endm
  408.  
  409. DrawSnake   macro
  410.     xor si,si
  411.     xor cx,cx
  412.     mov cl,snake_size
  413.     dwc:
  414.         SetCurPos snake_elements[si].x, snake_elements[si].y
  415.         Print   snake_char
  416.         add si,SNAKE_STRUC_SIZE    
  417.     loop    dwc
  418.     DrawFood
  419. endm
  420.  
  421. Print   macro   char
  422.     pusha
  423.     mov ah,02h
  424.     mov dl,char
  425.     int 21h
  426.     popa
  427. endm   
  428.  
  429. Render  macro
  430.     DrawSnake
  431. endm
  432.    
  433. Sleep   macro
  434.     pusha
  435.     xor ax,ax
  436.     xor bx,bx
  437.     xor dx,dx
  438.     int 1ah
  439.     mov bx,dx
  440.     add bx,SLEEP_TIME
  441.     _loop:
  442.         int 1ah
  443.         cmp bx,dx
  444.     jne _loop
  445.     popa   
  446. endm   
  447.  
  448. ClearRegisters  macro
  449.     xor ax,ax
  450.     xor bx,bx
  451.     xor dx,dx
  452.     xor     cx,cx
  453. endm
  454.  
  455. Restart proc
  456.     call    Preparation
  457.     call    cls
  458.     DrawBorder
  459.     mov snake_size,START_SNAKE_SIZE
  460.     mov score,0
  461. ret
  462. endp
  463.  
  464. prg proc    far
  465.     push    ds
  466.     xor ax,ax
  467.     push    ax
  468.     mov ax,dssg
  469.     mov ds,ax
  470.     ;Preparation
  471.     ;cls
  472.     ;DrawBorder
  473.     call    Restart
  474.     HideBlinkingCursor
  475.     PlaceFood
  476.     mov food_x,1
  477.     mov food_y,1
  478.     DrawFood
  479.     GAME_LOOP:
  480.         ;cls
  481.         ClearRegisters
  482.         Update 
  483.         Render
  484.         Sleep
  485.     jmp GAME_LOOP  
  486. ret
  487. prg endp
  488. cdsg    ends
  489. end prg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement