Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; this is the screen eating snake game...
- ;
- ; this game pushes the emulator to its limits,
- ; and even with maximum speed it still runs slowly.
- ; to enjoy this game it's recommended to run it on real
- ; computer, however the emulator can be useful to debug
- ; tiny games and other similar programs such as this before
- ; they become bug-free and workable.
- ;
- ; you can control the snake using arrow keys on your keyboard.
- ;
- ; all other keys will stop the snake.
- ;
- ; press esc to exit.
- name "snake"
- org 100h
- .data
- newline db 10,13,0
- ; jump over data section:
- ;jmp start
- ; ------ data section ------
- s_size equ 20
- arr_size equ 35
- arr2_size equ 200
- flag_size equ 35
- ; the snake coordinates
- ; (from head to tail)
- ; low byte is left, high byte
- ; is top - [top, left]
- snake dw s_size dup(0)
- x_array db arr_size dup(?)
- y_array db arr_size dup(?)
- X db arr2_size dup(?)
- Y db arr2_size dup(?)
- flag db flag_size dup(?)
- tail dw ?
- hour db ?
- min db ?
- sec db ?
- mili_sec db ?
- ; direction constants
- ; (bios key codes):
- left equ 4bh
- right equ 4dh
- up equ 48h
- down equ 50h
- ; current snake direction:
- cur_dir db right
- wait_time dw 0
- just_stop db 0
- ; welcome message
- msg db "==== how to play ====", 0dh,0ah
- db "this game was debugged on emu8086", 0dh,0ah
- db "but it is not designed to run on the emulator", 0dh,0ah
- db "because it requires relatively fast video card and cpu.", 0dh,0ah, 0ah
- db "if you want to see how this game really works,", 0dh,0ah
- db "run it on a real computer (click external->run from the menu).", 0dh,0ah, 0ah
- db "you can control the snake using arrow keys", 0dh,0ah
- db "all other keys will stop the snake.", 0dh,0ah, 0ah
- db "press esc to exit.", 0dh,0ah
- db "====================", 0dh,0ah, 0ah
- db "press any key to start...$"
- ; ------ code section ------
- .code
- include 'emu8086.inc'
- main proc
- start:
- ; print welcome message:
- ;mov dx, offset msg
- ;mov ah, 9
- ;int 21h
- ; wait for any key:
- mov ah, 00h
- int 16h
- ; hide text cursor:
- mov ah, 1
- mov ch, 2bh
- mov cl, 0bh
- int 10h
- ;1. print '#' at the location:
- mov dh,5
- mov y_array[7],dh
- mov dl,10
- mov x_array[7],dl
- mov flag[7],1
- mov cx,10
- mov ah,2
- int 10h
- loop1:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop1
- ;1. print '#' at the location:
- mov dh,5
- mov y_array[8],dh
- mov dl,45
- mov x_array[8],dl
- mov flag[8],1
- mov cx,10
- mov ah,2
- int 10h
- loop2:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop2
- ;1. print '#' at the location:
- mov dh,5
- mov y_array[9],dh
- mov dl,65
- mov x_array[9],dl
- mov flag[9],1
- mov cx,10
- mov ah,2
- int 10h
- loop3:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop3
- ;1. print '#' at the location:
- mov dh,0
- mov y_array[10],dh
- mov dl,55
- mov x_array[10],dl
- mov flag[10],1
- mov cx,10
- mov ah,2
- int 10h
- loop4:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop4
- ;1. print '#' at the location:
- mov dh,15
- mov y_array[11],dh
- mov dl,55
- mov x_array[11],dl
- mov flag[11],1
- mov cx,10
- mov ah,2
- int 10h
- loop5:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop5
- ;1. print '#' at the location:
- mov dh,5
- mov y_array[12],dh
- mov dl,80
- mov x_array[12],dl
- mov flag[12],1
- mov cx,10
- mov ah,2
- int 10h
- loop6:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop6
- ;1. print '#' at the location:
- mov dh,15
- mov y_array[13],dh
- mov dl,75
- mov x_array[13],dl
- mov flag[13],1
- mov cx,10
- mov ah,2
- int 10h
- loop7:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop7
- ;1. print '#' at the location:
- mov dh,0
- mov y_array[14],dh
- mov dl,75
- mov x_array[14],dl
- mov flag[14],1
- mov cx,10
- mov ah,2
- int 10h
- loop8:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop8
- ;1. print '#' at the location:
- mov dh,5
- mov y_array[15],dh
- mov dl,5
- mov x_array[15],dl
- mov flag[15],1
- mov cx,10
- mov ah,2
- int 10h
- loop9:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop9
- ;1. print '#' at the location:
- mov dh,5
- mov y_array[16],dh
- mov dl,30
- mov x_array[16],dl
- mov flag[16],1
- mov cx,10
- mov ah,2
- int 10h
- loop10:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop10
- ;1. print '#' at the location:
- mov dh,15
- mov y_array[17],dh
- mov dl,25
- mov x_array[17],dl
- mov flag[17],1
- mov cx,10
- mov ah,2
- int 10h
- loop11:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop11
- ;1. print '#' at the location:
- mov dh,0
- mov y_array[18],dh
- mov dl,15
- mov x_array[18],dl
- mov flag[18],1
- mov cx,10
- mov ah,2
- int 10h
- loop12:
- push dx
- mov dl,'#'
- int 21h
- pop dx
- inc dh
- int 10h
- loop loop12
- ;1.set cursor pos:
- mov dh,20
- mov y_array[21],dh
- mov dl,70
- mov x_array[21],dl
- mov flag[21],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;1.set cursor pos:
- mov dh,10
- mov y_array[22],dh
- mov dl,70
- mov x_array[22],dl
- mov flag[22],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;1.set cursor pos:
- mov dh,20
- mov y_array[23],dh
- mov dl,55
- mov x_array[23],dl
- mov flag[23],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;1.set cursor pos:
- mov dh,20
- mov y_array[24],dh
- mov dl,40
- mov x_array[24],dl
- mov flag[24],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;1.set cursor pos:
- mov dh,25
- mov y_array[25],dh
- mov dl,15
- mov x_array[25],dl
- mov flag[25],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;1.set cursor pos:
- mov dh,25
- mov y_array[26],dh
- mov dl,0
- mov x_array[26],dl
- mov flag[26],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;1.set cursor pos:
- mov dh,25
- mov y_array[27],dh
- mov dl,30
- mov x_array[27],dl
- mov flag[27],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;1.set cursor pos:
- mov dh,10
- mov y_array[0],dh
- mov dl,20
- mov x_array[0],dl
- mov flag[0],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;1.set cursor pos:
- mov dh,15
- mov y_array[19],dh
- mov dl,0
- mov x_array[19],dl
- mov flag[19],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;1.set cursor pos:
- mov dh,13
- mov y_array[20],dh
- mov dl,10
- mov x_array[20],dl
- mov flag[20],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;3.set cursor pos:
- mov dh,15
- mov y_array[1],dh
- mov dl,30
- mov x_array[1],dl
- mov flag[1],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;5.set cursor pos:
- mov dh,5
- mov y_array[2],dh
- mov dl,30
- mov x_array[2],dl
- mov flag[2],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;7.set cursor pos:
- mov dh,20
- mov y_array[3],dh
- mov dl,10
- mov x_array[3],dl
- mov flag[3],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;11.set cursor pos:
- mov dh,15
- mov y_array[4],dh
- mov dl,15
- mov x_array[4],dl
- mov flag[4],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;11.set cursor pos:
- mov dh,15
- mov y_array[5],dh
- mov dl,60
- mov x_array[5],dl
- mov flag[5],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;11.set cursor pos:
- mov dh,25
- mov y_array[6],dh
- mov dl,65
- mov x_array[6],dl
- mov flag[6],0
- mov ah,2
- int 10h
- ; print '#' at the location:
- mov al, '#'
- mov ah, 09h
- mov bl, 0ah ; attribute.
- mov cx, 10 ; single char.
- int 10h
- ;set cursor pos:
- ;mov dh,15
- ;mov dl,0
- ;mov ah,2
- ;int 10h
- game_loop:
- push ax
- mov ah, 0
- mov al, just_stop
- cmp al, 1
- je gtfo
- pop ax
- mov ah,2ch
- int 21h
- mov hour,ch
- mov min,cl
- mov sec,dh
- mov mili_sec,dl
- ; === select first video page
- mov al, 0 ; page number.
- mov ah, 05h
- int 10h
- ; === show new head:
- mov dx, snake[0]
- ; set cursor at dl,dh
- mov ah, 02h
- int 10h
- ; print '*' at the location:
- mov al, '*'
- mov ah, 09h
- mov bl, 0eh ; attribute.
- mov cx, 1 ; single char.
- int 10h
- ; === keep the tail:
- mov ax, snake[s_size * 2 - 2]
- mov tail, ax
- call move_snake
- ; === hide old tail:
- mov dx, tail
- ; set cursor at dl,dh
- mov ah, 02h
- int 10h
- ; print ' ' at the location:
- mov al, ' '
- mov ah, 09h
- mov bl, 0eh ; attribute.
- mov cx, 1 ; single char.
- int 10h
- check_for_key:
- ; === check for player commands:
- mov ah, 01h
- int 16h
- jz no_key
- mov ah, 00h
- int 16h
- cmp al, 1bh ; esc - key?
- je stop_game ;
- mov cur_dir, ah
- no_key:
- ; === wait a few moments here:
- ; get number of clock ticks
- ; (about 18 per second)
- ; since midnight into cx:dx
- mov ah, 00h
- int 1ah
- add dx,200
- cmp dx, wait_time
- jb check_for_key
- add dx, 4
- mov wait_time, dx
- ; === eternal game loop:
- jmp game_loop
- stop_game:
- ; show cursor back:
- mov ah, 1
- mov ch, 0bh
- mov cl, 0bh
- int 10h
- main endp
- ret
- DEFINE_PRINT_STRING
- DEFINE_SCAN_NUM
- DEFINE_PRINT_NUM
- DEFINE_PRINT_NUM_UNS
- ; ------ functions section ------
- ; this procedure creates the
- ; animation by moving all snake
- ; body parts one step to tail,
- ; the old tail goes away:
- ; [last part (tail)]-> goes away
- ; [part i] -> [part i+1]
- ; ....
- move_snake proc near
- ; set es to bios info segment:
- mov ax, 40h
- mov es, ax
- ; point di to tail
- mov di, s_size * 2 - 2
- ; move all body parts
- ; (last one simply goes away)
- mov cx, s_size-1
- move_array:
- mov ax, snake[di-2]
- mov snake[di], ax
- sub di, 2
- loop move_array
- cmp cur_dir, left
- je move_left
- cmp cur_dir, right
- je move_right
- cmp cur_dir, up
- je move_up
- cmp cur_dir, down
- je move_down
- jmp stop_move ; no direction.
- move_left:
- mov al, b.snake[0]
- dec al
- mov b.snake[0], al
- cmp al, -1
- jne lool
- mov al, es:[4ah] ; col number.
- dec al
- mov b.snake[0], al ; return to right.
- lool:
- push cx
- mov cx,28
- push si
- push di
- push bp
- mov si, offset x_array
- mov di, offset y_array
- pool1:
- push cx
- mov cx, 10
- current_x db 0
- push ax
- mov ah, 0
- mov al, [si]
- mov current_x, al
- current_y db 0
- mov ah, 0
- mov al, [di]
- mov current_y, al
- pop ax
- push bp
- mov bp, offset flag
- nested_loop:
- push ax
- mov ah, current_x
- mov al, b.snake[0]
- cmp ah, al
- jne skip14
- push bx
- mov bh, current_y
- mov bl, b.snake[1]
- cmp bh, bl
- jne skip16
- pop bx
- mov just_stop, 1
- jmp skip17
- skip16:
- pop bx
- skip17:
- skip14:
- pop ax
- push ax
- mov ah, 0
- mov al, [bp]
- cmp al, 1
- jne skip156
- dec current_y
- jmp skip666
- skip156:
- inc current_x
- skip666:
- inc bp
- pop ax
- loop nested_loop
- pop bp
- pop cx
- inc si
- inc di
- loop pool1
- pop di
- pop si
- pop cx
- jmp stop_move
- move_right:
- mov al, b.snake[0]
- inc al
- mov b.snake[0], al
- cmp al, es:[4ah] ; col number.
- jb stop_move
- mov b.snake[0], 0 ; return to left.
- jmp stop_move
- move_up:
- mov al, b.snake[1]
- dec al
- mov b.snake[1], al
- cmp al, -1
- jne stop_move
- mov al, es:[84h] ; row number -1.
- mov b.snake[1], al ; return to bottom.
- jmp stop_move
- move_down:
- mov al, b.snake[1]
- inc al
- mov b.snake[1], al
- cmp al, es:[84h] ; row number -1.
- jbe stop_move
- mov b.snake[1], 0 ; return to top.
- jmp stop_move
- stop_move:
- ret
- move_snake endp
- gtfo:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement