Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;**********************************************************;
- ;*******Snake game developed by Saitei*********************;
- ;**********************************************************;
- ;**********************************************************;
- stsg segment para stack 'stack'
- dw 32 dup(?) ;64 bytes on the stack
- stsg ends
- MAP_WIDTH = 78
- MAP_HEIGHT = 21
- START_SNAKE_SIZE = 10
- START_PAUSE_STATE = 0
- SLEEP_TIME = 1 ;for limiting FPS
- EXIT_STATE = 0
- ;***************Dirs enum*****************;
- LEFT = 0
- UP = 1
- RIGHT = 2
- DOWN = 3
- ;*****************************************;
- ;***************Colors enum***************;
- YELLOW = 1110b
- WHITE = 1111b
- RED = 1100b
- GREEN = 0010b
- ;*****************************************;
- SNAKE STRUC
- x db ?
- y db ?
- SNAKE ENDS
- ;***!!!***;
- SNAKE_STRUC_SIZE = 2
- ;*********;
- dssg segment para 'data'
- snake_elements SNAKE 255 dup(?)
- snake_size db START_SNAKE_SIZE
- snake_dir db LEFT
- snake_char db 254,0
- empty_char db ' ',0
- border_horizont_char db 196,0
- border_vert_char db 179,0
- border_ugl_lu db 218,0
- border_ugl_ru db 191,0
- border_ugl_ld db 192,0
- border_ugl_rd db 217,0
- random_seed_x dw MAP_WIDTH
- random_seed_y dw MAP_HEIGHT
- food_x db ?
- food_y db ?
- food_char db 6,0
- pause db START_PAUSE_STATE
- score dw 0
- exit db 0
- dssg ends
- cdsg segment para 'code'
- assume cs:cdsg, ds:dssg, ss:stsg
- cls proc
- pusha
- mov ax,0600h
- mov bh,7
- mov cx,0
- mov dx,184fh
- int 10h
- popa
- ret
- endp
- PrintColored macro ch,_color
- pusha
- mov ah,09
- mov al,ch
- mov bh,00
- mov bx,_color
- mov cx,01
- int 10h
- popa
- endm
- randx proc near
- push dx cx
- mov ax, random_seed_x
- dec ax
- xor dx,dx
- mov cx,12773
- div cx
- mov cx,ax
- mov ax,16807
- mul dx
- mov dx,cx
- mov cx,ax
- mov ax,2836
- mul dx
- sub cx,ax
- xor dx,dx
- mov ax,cx
- mov random_seed_x, cx
- mov cx,10000
- div cx
- mov ax,dx
- inc ax
- pop cx dx
- ret
- randx endp
- randy proc near
- push dx cx
- mov ax, random_seed_y
- dec ax
- xor dx,dx
- mov cx,12773
- div cx
- mov cx,ax
- mov ax,16807
- mul dx
- mov dx,cx
- mov cx,ax
- mov ax,2836
- mul dx
- sub cx,ax
- xor dx,dx
- mov ax,cx
- mov random_seed_y, cx
- mov cx,10000
- div cx
- mov ax,dx
- inc ax
- pop cx dx
- ret
- randy endp
- PlaceFood macro
- push ax
- call randx
- mov food_x,al
- call randy
- mov food_y,al
- pop ax
- endm
- DrawFood macro
- SetCurPos food_x,food_y
- Print food_char
- endm
- DrawBorder macro
- SetCurPos 0,0
- PrintColored border_ugl_lu,YELLOW
- SetCurPos MAP_WIDTH+1,0
- PrintColored border_ugl_ru,YELLOW
- SetCurPos 0,MAP_HEIGHT+1
- PrintColored border_ugl_ld,YELLOW
- SetCurPos MAP_WIDTH+1,MAP_HEIGHT+1
- PrintColored border_ugl_rd,YELLOW
- mov al,1 ;x
- mov cx,MAP_WIDTH
- UL:
- SetCurPos al,0
- PrintColored border_horizont_char,YELLOW
- inc al
- loop UL
- mov al,1 ;x
- mov cx,MAP_WIDTH
- _DL:
- SetCurPos al,MAP_HEIGHT+1
- PrintColored border_horizont_char,YELLOW
- inc al
- loop _DL
- mov al,1 ;y
- mov cx,MAP_HEIGHT
- LL:
- SetCurPos 0,al
- PrintColored border_vert_char,YELLOW
- inc al
- loop LL
- mov al,1 ;y
- mov cx,MAP_HEIGHT
- RL:
- SetCurPos MAP_WIDTH+1,al
- PrintColored border_vert_char,YELLOW
- inc al
- loop RL
- endm
- HideBlinkingCursor macro
- push cx
- push ax
- mov ch,32
- mov ah,1
- int 10h
- pop ax
- pop cx
- endm
- CreateSnake proc
- mov al,11 ;x
- mov ah,4 ;y
- xor cx,cx
- mov cl,snake_size
- xor si,si ;index for the array of structures snake_elements
- SNAKE_CREATION:
- mov snake_elements[si].x,al
- mov snake_elements[si].y,ah
- inc al
- add si,SNAKE_STRUC_SIZE
- loop SNAKE_CREATION
- ret
- endp
- SetCurPos macro x,y
- pusha
- mov bh,0
- mov dh,y
- mov dl,x
- mov ah,2
- int 10h
- popa
- endm
- Preparation proc
- call CreateSnake
- ret
- endp
- MoveSnake macro
- mov al,snake_size
- mov bl,SNAKE_STRUC_SIZE
- mul bl
- mov si,ax
- xor cx,cx
- mov cl,snake_size
- MCC:
- mov al,snake_elements[si-SNAKE_STRUC_SIZE].x
- mov ah,snake_elements[si-SNAKE_STRUC_SIZE].y
- mov snake_elements[si].x,al
- mov snake_elements[si].y,ah
- sub si,SNAKE_STRUC_SIZE
- loop MCC
- cmp snake_dir,LEFT
- je MoveLeft
- cmp snake_dir,UP
- je MoveUp
- cmp snake_dir,RIGHT
- je MoveRight
- cmp snake_dir,DOWN
- je MoveDown
- MoveLeft:
- cmp snake_elements[0].x,1
- je MLTR
- dec snake_elements[0].x
- jmp MLEXIT
- MLTR:
- mov snake_elements[0].x,MAP_WIDTH
- MLEXIT:
- jmp SnakeMoveExit
- MoveUp:
- cmp snake_elements[0].y,1
- je MUTD
- dec snake_elements[0].y
- jmp MUEXIT
- MUTD:
- mov snake_elements[0].y,MAP_HEIGHT
- MUEXIT:
- jmp SnakeMoveExit
- MoveRight:
- cmp snake_elements[0].x,MAP_WIDTH
- je MRTL
- inc snake_elements[0].x
- jmp MREXIT
- MRTL:
- mov snake_elements[0].x,1
- MREXIT:
- jmp SnakeMoveExit
- MoveDown:
- cmp snake_elements[0].y,MAP_HEIGHT
- je MDTU
- inc snake_elements[0].y
- jmp MDEXIT
- MDTU:
- mov snake_elements[0].y,1
- MDEXIT:
- jmp SnakeMoveExit
- SnakeMoveExit:
- endm
- KeysUpdate macro
- pusha
- mov ah,0bh
- int 21h
- cmp al,0ffh
- jne KUEXIT
- ClearRegisters
- mov ah,07h
- int 21h
- cmp al,'w'
- je MUP
- cmp al,'W'
- je MUP
- cmp al,'a'
- je MLEFT
- cmp al,'A'
- je MLEFT
- cmp al,'s'
- je MDOWN
- cmp al,'S'
- je MDOWN
- cmp al,'d'
- je MRIGHT
- cmp al,'D'
- je MRIGHT
- jmp KUEXIT
- MUP:
- cmp snake_dir,DOWN
- je KUEXIT
- mov snake_dir,UP
- jmp KUEXIT
- MLEFT:
- cmp snake_dir,RIGHT
- je KUEXIT
- mov snake_dir,LEFT
- jmp KUEXIT
- MDOWN:
- cmp snake_dir,UP
- je KUEXIT
- mov snake_dir,DOWN
- jmp KUEXIT
- MRIGHT:
- cmp snake_dir,LEFT
- je KUEXIT
- mov snake_dir,RIGHT
- jmp KUEXIT
- KUEXIT:
- popa
- endm
- CheckSnakeSnakeCollision macro
- pusha
- xor cx,cx
- mov cl,snake_size
- dec cx
- mov al,snake_size
- mov bl,SNAKE_STRUC_SIZE
- mul bl
- mov si,ax
- CSSC:
- mov bl,snake_elements[si].x
- cmp snake_elements[0].x,bl
- je CheckY
- Back:
- loop CSSC
- jmp CSSCEXIT
- CheckY:
- mov bl,snake_elements[si].y
- cmp snake_elements[0].y,bl
- jne Back
- call Restart
- CSSCEXIT:
- popa
- endm
- CheckSnakeFoodCollision macro
- push ax
- mov al,food_x
- cmp snake_elements[0].x,al
- je CSFCY
- jmp _Exit
- CSFCY:
- mov al,food_y
- cmp snake_elements[0].y,al
- je NN
- jmp _Exit
- NN:
- PlaceFood
- _Exit:
- pop ax
- endm
- CheckCollision macro
- CheckSnakeSnakeCollision
- CheckSnakeFoodCollision
- endm
- Update macro
- mov al,snake_size
- mov bl,SNAKE_STRUC_SIZE
- mul bl
- mov si,ax
- SetCurPos snake_elements[si-SNAKE_STRUC_SIZE].x,snake_elements[si-SNAKE_STRUC_SIZE].y
- Print empty_char
- KeysUpdate
- MoveSnake
- CheckCollision
- endm
- DrawSnake macro
- xor si,si
- xor cx,cx
- mov cl,snake_size
- dwc:
- SetCurPos snake_elements[si].x, snake_elements[si].y
- Print snake_char
- add si,SNAKE_STRUC_SIZE
- loop dwc
- DrawFood
- endm
- Print macro char
- pusha
- mov ah,02h
- mov dl,char
- int 21h
- popa
- endm
- Render macro
- DrawSnake
- endm
- Sleep macro
- pusha
- xor ax,ax
- xor bx,bx
- xor dx,dx
- int 1ah
- mov bx,dx
- add bx,SLEEP_TIME
- _loop:
- int 1ah
- cmp bx,dx
- jne _loop
- popa
- endm
- ClearRegisters macro
- xor ax,ax
- xor bx,bx
- xor dx,dx
- xor cx,cx
- endm
- Restart proc
- call Preparation
- call cls
- DrawBorder
- mov snake_size,START_SNAKE_SIZE
- mov score,0
- ret
- endp
- prg proc far
- push ds
- xor ax,ax
- push ax
- mov ax,dssg
- mov ds,ax
- ;Preparation
- ;cls
- ;DrawBorder
- call Restart
- HideBlinkingCursor
- PlaceFood
- mov food_x,1
- mov food_y,1
- DrawFood
- GAME_LOOP:
- ;cls
- ClearRegisters
- Update
- Render
- Sleep
- jmp GAME_LOOP
- ret
- prg endp
- cdsg ends
- end prg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement