贪吃蛇

assume cs:code,ds:data,ss:stack

data segment
    food dw 0
    dir  dw 0
    site db -1,0,1,0,0,-1,0,1
    top  dw 0
    body dw 400 dup(0)
data ends

stack segment
    dw 20 dup(0)
stack ends

code segment
start:
        mov ax,data
        mov ds,ax
        
        mov ax, 0b800h
        mov es, ax
        
        mov ax, stack
        mov ss,ax
        mov sp,20
    
        mov ds:[dir],3    ;方向
        
        mov ds:[top],3        ;初始蛇身
        mov cx, 4
        mov ax, 0A03h
        mov si,0
init:
        
        mov ds:[body+si], ax
        add si, 2
        add al, 1
        
loop init
        
        call clear 
        call set_food 
        call snake
        call walls
        
game:
        call update
        mov cx, 0Fh
        sss:
            push cx
            mov cx, 05FFh
                ssss:
                    push cx
                    call getInput
                    pop cx
                loop ssss
            pop cx
        loop sss
        
jmp game

gameend:

        mov ax,4c00h
        int 21h
        
update:

        mov si,ds:[top]
        
        
        mov cx,si
        dec cx
        mov di,si
        
        
        add di,di
        mov ax,ds:[body+di]
        
        
        mov di,0
judge:

        mov bx,ds:[body+di]
        cmp ax,bx
        je gameend
        add di,2
        
loop judge        


        cmp ah,1
        je gameend
        
        cmp ah,25
        je gameend
        
        cmp al,2
        je gameend
        
        cmp al,79
        je gameend
        
        mov cx,si
        mov di,2
        
    updata_change:

            mov ax,ds:[body+di]
            
            mov bx, ds:[body+di-2]
            mov dh,0
            mov dl,' '
            call print_char
            
            mov ds:[body+di-2],ax
            add di,2
        
    loop updata_change

        mov si,ds:[dir]
        add si,si
        
        mov dh,ah
        mov ah,ds:[site+si]
        add dh,ah
        
        mov dl,al
        mov al,ds:[site+si+1]
        add dl,al

        mov si,ds:[top]
        add si,si
        mov ds:[body+si],dx
        
        cmp dx,ds:[food]
        je addsnakebody
        
        call snake
ret

addsnakebody:

        mov si,ds:[top]
        inc si
        mov ds:[top],si
        add si,si
        
        mov byte ptr ds:[body+si],dl
        mov byte ptr ds:[body+si+1],dh
        
        call snake
        call set_food

ret


getInput:

    mov al, 0
    mov ah, 1
    int 16h
    
    cmp ah, 1
    je getInputEnd

    mov al, 0
    mov ah, 0
    int 16h;
    mov cx, ax;

    cmp cx, 4800h
    je get_up

    cmp cx, 5000h
    je get_down

    cmp cx, 4b00h
    je get_left

    cmp cx, 4d00h
    je get_right

    jmp getInputEnd

    get_up:
        mov ax, ds:[dir]
        cmp ax, 1
        je getInputEnd
        mov ds:[dir], 0
        jmp getInputEnd

    get_down:
        mov ax, ds:[dir]
        cmp ax, 0
        je getInputEnd
        mov ds:[dir], 1
        jmp getInputEnd

    get_left:
        mov ax, ds:[dir]
        cmp ax, 3
        je getInputEnd
        mov ds:[dir], 2
        jmp getInputEnd

    get_right:
        mov ax, ds:[dir]
        cmp ax, 2
        je getInputEnd
        mov ds:[dir], 3
        jmp getInputEnd

getInputEnd:

        ret

                
set_food:
        call getRand
        
        mov dh,40h
        mov dl,' '
        call print_char
ret

getRand:

    mov ax, 0h
    out 43h, al
    in al, 40h
    in al, 40h
    in al, 40h

    mov bl, 23
    div bl 

    mov bh, ah
    add bh,2
    
    
    
    mov ax, 0h
    out 43h, al
    in al, 40h
    in al, 40h
    in al, 40h

    mov bl, 76
    div bl 

    mov bl, ah
    add bl,3
    
    mov ds:[food],bx
ret    
    
snake:
        mov si,ds:[top]
        mov cx,si
        add si,si
        
        mov bx,ds:[body+si]
        mov dh,10h
        mov dl,' '
        call print_char

        sub si,2
        
    snake_print:
    
        mov bx,ds:[body+si]
        mov dh,20h
        mov dl, ' '
        call print_char
        sub si,2
        
    loop snake_print

ret    
        
                
walls:
        mov dh, 71h
        mov dl,' '
        
        mov cx, 80
        mov bl, 1
        
    walls_row:        

            mov bh, 1
            call print_char
            mov bh, 25
            call print_char
        
            inc bl
        
    loop walls_row

            mov dh, 71h
            mov dl,' '
        
            mov cx, 25
            mov bh, 1

    walls_col:        

            mov bl,1
            call print_char
            
            mov bl,2
            call print_char
            
            mov bl,79
            call print_char
            
            mov bl,80
            call print_char
            
            
            inc bh
        
    loop walls_col

ret


;bh-行 bl-列 dx-值和颜色
print_char:

        push bx
        push dx
        push ax
        push si
        
        dec bh
        dec bl
        
        mov al, 160
        mul bh
        
        mov bh, 0
        add bl, bl
        
        add ax,bx
        mov si,ax
        
        mov  es:[si],dl
        mov  es:[si+1],dh
        ;mov  es:[si+2],dl
        ;mov  es:[si+3],dh
        pop si
        pop ax
        pop dx
        pop bx
ret

clear:

        push ax

        mov ax, 3h
        int 10h

        pop ax
        
ret

code ends
end start

不够完美,随便写写。

原文地址:https://www.cnblogs.com/VividBinGo/p/11867609.html