.model small .data ;variables needed for snake body head db '@', '$' ;head and body symbols tail db ' ', '$' ;tail part set color to black snake_length db 3 ; 1-2 body 3 tail copy_snake_length db ? ;for loop sEnd db ? head_col db 40 ;starting col for snake head_row db 12 ;starting row for snake ; snake movement algorithm ; always copy the coordinates of the symbol in front. copy_col db ? copy_row db ? delaytime db 1 ;variable for control keys input db ? .stack 100h .code ;to delay time delay proc mov ah, 00 int 1Ah mov bx, dx jmp_delay: int 1Ah sub dx, bx cmp dl, delaytime jl jmp_delay ret delay endp ;make snake head go to the right righty proc cmp head_col,79 je resetposl zero: inc head_col jmp rightyie resetposl: mov head_col, 0 rightyie: mov dl,head_col mov dh,head_row xor bh, bh mov ah, 02h int 10h ret righty endp startgame proc mov dh, 12 ;row mov dl, 40 ;column xor bh, bh mov ah, 02h int 10h mov dx, offset head mov ah, 09h int 21h ret startgame endp mov cl, head_row mov copy_row,cl mov cl, head_col mov copy_col, cl ;print head mov dh, head_row mov dl, head_col xor bh, bh mov ah, 02h int 10h mov dx, offset head mov ah, 09h int 21h main proc mov ax, @data mov ds, ax ;set video mode mov al, 03h mov ah, 00h int 10h ;clear screen ;only need it once (reason no need to use function) mov ax, 0600h mov bh, 07h xor cx, cx mov dx, 184fh int 10h mov cx, 3200h ;stop cursor blinking mov ah, 01h int 10h ;set start head snake in the middle of the screen call startgame ;control mov ah,00h int 16h mov input, 'd' ;to change direction or to keep on going getinput: mov ah, 01h int 16h jz key mov ah,00h int 16h mov input,al ;control keys key: ;cmp input, 'w' ;je w ;cmp input, 's' ;je s ;cmp input, 'a' ;je a cmp input, 'd' je d jne rak d: mov cl, head_row mov copy_row,cl mov cl, head_col mov copy_col, cl mov dh, head_row mov dl, head_col xor bh, bh mov ah, 02h int 10h mov al, tail mov bh, 0 mov bl, 000h mov cx, 1 mov ah, 09h int 10h mov cl, snake_length mov copy_snake_length, cl dec copy_snake_length mov bl,0 printbody: mov al, head_row mov copy_row,al mov al, head_col mov copy_col, al call righty ;print head ;coordinates mov dh, head_row mov dl, head_col xor bh, bh mov ah, 02h int 10h ;printing "@" mov dx, offset head mov ah, 09h int 21h ; inc copy_col to update the head. inc copy_col mov al,copy_col mov head_col,al inc bl ; now loop to print other characters cmp bl,copy_snake_length jl printbody dec head_col jmp rak rak: call delay jmp getinput mov ax, 4c00h int 21h main endp end main
如果您想尝试一下代码,请按d移动字符。蛇组装8086:无法正确移动
的问题是,当它到达最后一栏,我用右手的功能想改变head_col为0,这样它看起来像它到达屏幕的结束。不幸的是,它忽略了在最后一个字符山坳80然后继续打印,但下一排。
然后,当它到达第13行的末尾时,它不会在col 0处留下字符,但仍然沿着另一个行向下,并且在3继续打印后,似乎它会回到原始行,然后当它达到了起点,它不再打印任何东西。
如果你的“蛇”是多于一个字符长,你将不得不assing屏幕坐标(x,y)的每个字符。一个是X坐标和另一个用于Y坐标,和文字本身的第三阵列:你可以用两个数组做。 – 2015-04-06 19:46:58