当前位置: 首页 > 知识库问答 >
问题:

蛇游戏:如何知道自己是否咬了自己

羊舌光赫
2023-03-14

我有一个代码,它在其中移动,生长,生成食物,最后一个问题是,如果蛇咬了自己怎么办。

;Snake Game without Borders
.model small

.data

row db 0FEh dup (?)
col db 0FEh dup (?)

temp_row db ?
temp_col db ?

;variables for food
rand_food_col db ?
rand_food_row db ?


;delay for snake movement
delaytime db 1

;use this delay time to pseudorandom the col and row for food
delay_food db 3


food db 'F' ,'$'


head db '@' ,'$'
snake_length db 5
snake_loop db ?

color db 0Fh
food_color db 0Fh

input db ?
;variables to display text messages:
game_over db 'GAME OVER';
.stack 100h
.code
;///////////////////////////////////////////////////////////////////////////////////////
;clear registers:
clear_reg proc
xor ax,ax
xor bx,bx
xor cx,cx
xor dx,dx
clear_reg endp
;//////////////////////////////////////////////////////////////////////////////////////////////
;food functions 

random_coor_food proc
;random function for col

mov ah, 00h
int 1ah

mov ax,dx
xor dx,dx
mov cx,20
div cx

mov al,dl
mov rand_food_col,al

;random function for row

mov ah, 00h
int 1ah

mov ax,dx
xor dx,dx
mov cx, 71
div cx

mov al,dl
mov rand_food_row,al

ret
random_coor_food endp


print_food proc
mov dl, rand_food_row
mov dh, rand_food_col
xor bh,bh
mov ah, 02h
int 10h

mov dx, offset food
mov ah, 09h
int 21h
ret
print_food endp

;//////////////////////////////////////////////////////////////////////////////////////////////
lefty proc
    mov dl,col[0]
    cmp dl, 0
    je resetposr

    sn:
    dec dl
    jmp leftyie

    resetposr:
    mov dl, 79


    leftyie:
    mov col[0],dl
    ret 
    lefty endp

righty proc
    mov dl,col[0]
    cmp dl,79
    je resetposl

    zero: 
    inc dl
    jmp rightyie


    resetposl:
    mov dl, 0

    rightyie:
    mov col[0],dl
    ret 
righty endp


upy proc
    mov dh,row[0]
    cmp dh,0
    je resetposd

    upzero:
    dec dh
    jmp uptie

    resetposd:
    mov dh,24


    uptie:
    mov row[0],dh
    ret


upy endp

downy proc
mov dh,row[0]
cmp dh,24
je resetposu

gozero:
inc dh
jmp downty

resetposu:
mov dh, 0

downty:
mov row[0],dh
ret 
downy endp

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


clear proc near
    mov al, 03h 
    mov ah, 00h
    int 10h

    mov cx, 3200h  ;stop cursor blinking
    mov ah, 01h
    int 10h
    ret
clear endp

;////////////////////////////////////////////////////////////////////////////////
gameover proc
    call clear
    mov dh, 12 ;row
    mov dl, 35 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

     mov dx, offset game_over
    mov ah, 09h
    int 21h


    mov ax, 4c00h
    int 21h
gameover endp
;this macro will print the whole snake 
; r is for row c is for column char and color: check .data for values
complete_print macro r,c,char,color
    mov dh, r
    mov dl, c
    xor bh, bh
    mov ah, 02h
    int 10h 

    mov al, char
    mov bh, 0
    mov bl, color
    mov cx, 1
    mov ah, 09h
    int 10h 
endm
;function to print the snake body
snake proc

    call delay
    call clear


    mov bl,0
    mov bh,0
    print_snake:

    mov dl,[col+bx]
    mov temp_col,dl

    mov dl,[row+bx]
    mov temp_row,dl

    mov snake_loop,bl

    print_me:
    complete_print temp_row,temp_col,head,color
    inc snake_loop
    mov bl, snake_loop
    mov al, snake_loop
    cmp al, snake_length
    ;continue to print the body of the snake
    jl print_snake


    mov bl, snake_length

    ;transfer the coordinates   
    ;to insert new coordinates

    transfer:
    dec bx
    mov dl ,[col+bx]
    mov [col + bx + 1],dl

    mov dl,[row + bx]
    mov [row+bx+1],dl
    cmp bx,0
    jg transfer



    ret
snake endp

main proc
;//////////////////////////////////////////////////////////////////////
      mov ax, @data
    mov ds, ax
    call clear
    ;initialize starting body snake :)
    mov row[0],12

    mov row[1],12
    mov row[2],12
    mov row[3],12
    mov row[4],12

    mov col[0],40

    mov col[1],39
    mov col[2],38
    mov col[3],37
    mov col[4],36

    print:  
    call snake
    call random_coor_food
    call print_food

;//////////////////////////////////////////////////////////////////////
; moving the snake
    ;initialize the keyboard input:

    mov ah,00h
    int 16h
    mov input, 'd'

    getinput:
    ;to change direction or to keep going
    ; wait for keystroke
    ; if no key is pressed, value of input will be 
    ;last key pressed.
        mov ah, 01h
        int 16h
        jz key

    ;key is pressed, new value of input
        mov ah,00h
        int 16h
        mov input, al

    key: 
    ;UP
    cmp input, 'w'
    je w 
    ;DOWN
    cmp input, 's'
    je s
    ;LEFT
    cmp input, 'a'
    je a 
    ;RIGHT
    cmp input, 'd'
    je d

    jne getinput

    ;make the snake go up
    w:

    call upy
    jmp rak

    ;make the snake go up
    s:

    call downy
    jmp rak

    ;make the snake go left
    a:

    call lefty
    jmp rak

    ;make the snake go right
    d:

    call righty
    jmp rak



    rak:
    ;////// check first if snake hits the body
    ;if hit, then game over
    mov bh,0
    mov ah,08h ;scan
    int 10h
    cmp al, head
    je end_game




    call snake  
    mov cl,row[0]
    cmp cl,rand_food_col
    jne again

    mov cl,col[0]
    cmp cl,rand_food_row
    jne again


    ;compare the coordinates of head to the coordinates of the food


    increase:
    ;if the head row and col is equal to the coordinates of the food. increase snake_length then generate food again.
    inc snake_length
    call random_coor_food

    jmp rak

    again:
    call print_food
    jmp getinput



    end_game:
    call gameover

    mov ax, 4c00h
    int 21h

main endp
end main

查找RAK:

rak之后有一部分代码扫描光标位置的字符。当蛇的头撞到食物时,它就会发出这个密码。游戏结束了。我想知道为什么,

该代码中的坐标总是存储在数组中,可以检查头坐标是否等于存储在数组中的坐标。循环的极限应该在snake_length附近

mov bl,0
    hit_me_baby:
    mov dl,[col+bx+1]
    cmp col[0],dl
    jne again
    je next
    next:
    mov dl,[row+bx+1]
    cmp row[0],dl
    jne again
    je gameover
    inc bl
    cmp bl,snake_length
    jl hit_me_baby

行db 0FEH dup(?) col db 0FEH dup(?)

temp_row数据库? temp_col数据库?

;食物变量 rand_food_col db? rand_food_row数据库?

头数据库“@”,“$” 正文数据库“#”,“$” snake_length db 5 snake_loop数据库?

颜色db 0EH color_body db 02H food_color db 0fh

输入分贝? ;显示文本消息的变量: game_over数据库“GAME OVER”,“$” start_game db“Snake EATER III”,“$” 消息db“蛇的复仇”,“$” press_key db“按任意键启动......”,“$” rjbc db“rjbc GAMES Inc.”,“$” control db“control Keys:”,“$” go_up DB“W TO GO UP”,“$” go_down数据库要关闭“,”$“ go_left db“A向左”,“$” go_right数据库'd要改‘,'$' go_otherkey数据库“要暂停的其他键”,“$” go_exit db“按Q退出”,“$” .堆栈100H .代码 ;////////////////////////////////////////////////////////////////////////////////////////// ;清除寄存器: clear_reg过程 异或ax,ax 异或bx,bx 异或cx,cx 异或dx,dx clear_reg结束 ;///////////////////////////////////////////////////////////////////////////////////////////////// ;食物功能

random_coor_food过程 col随机函数

mov ah,00h int 1Ah

mov ax、dx 异或dx,dx mov cx,20 分区cx

mov al,dl mov rand_food_row,al

可再生能源 random_coor_food endp

print_food过程 mov dl,rand_food_row mov dh,rand_food_col 异或bh,bh mov ah,02h int 10H

sn:
dec dl
jmp leftyie

resetposr:
mov dl, 79


leftyie:
mov col[0],dl
ret 
lefty endp
zero: 
inc dl
jmp rightyie


resetposl:
mov dl, 0

rightyie:
mov col[0],dl
ret 

右端

upy过程 mov dh,行[0] cmp dh,0 乙脑重新安置d

upzero:
dec dh
jmp uptie

resetposd:
mov dh,24


uptie:
mov row[0],dh
ret

upy endp

延迟过程 mov ah,00 int 1Ah mov bx、dx

jmp_delay: int 1Ah 子dx、bx cmp dl,延迟时间 jl jmp_delay

ret

延迟endp

mov cx, 3200h  ;stop cursor blinking
mov ah, 01h
int 10h
ret
     mov dx, offset game_over
    mov ah, 09h
    int 21h


    mov ax, 4c00h
    int 21h

    ret

start_snake_game过程 呼叫清除 mov dh,8;世界其他地区 mov dl,31;柱 异或bh,bh mov ah,02h int 10H

     mov dx, offset start_game
    mov ah, 09h
    int 21h

    mov dh, 9 ;row
    mov dl, 29 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

     mov dx, offset message
    mov ah, 09h
    int 21h


    mov dh, 22 ;row
    mov dl, 27 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

         mov dx, offset press_key
    mov ah, 09h
    int 21h


    mov dh, 24 ;row
    mov dl, 31 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

         mov dx, offset rjbc
    mov ah, 09h
    int 21h


    mov dh, 12 ;row
    mov dl, 31 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

     mov dx, offset control
    mov ah, 09h
    int 21h

    mov dh, 13;row
    mov dl, 31 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

     mov dx, offset go_up
    mov ah, 09h
    int 21h

    mov dh, 14;row
    mov dl, 31 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

     mov dx, offset go_down
    mov ah, 09h
    int 21h


    mov dh, 15;row
    mov dl, 31 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

     mov dx, offset go_left
    mov ah, 09h
    int 21h

    mov dh, 16;row
    mov dl, 31 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

     mov dx, offset go_right
    mov ah, 09h
    int 21h

    mov dh, 17;row
    mov dl, 31 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

     mov dx, offset go_otherkey
    mov ah, 09h
    int 21h

    mov dh, 18;row
    mov dl, 31 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

     mov dx, offset go_exit
    mov ah, 09h
    int 21h
    ret

start_snake_game结束 ;此宏将打印整个蛇 ;r代表行c代表列char和color:check.data代表值 complete_print宏r,c,char,color mov dh,r mov dl,c 异或bh,bh mov ah,02h int 10H

mov al, char
mov bh, 0
mov bl, color
mov cx, 1
mov ah, 09h
int 10h 

ENDM ;打印蛇体的函数 snake工艺

call delay
call clear





mov bl,0
mov bh,0
print_snake:

mov dl,[col+bx]
mov temp_col,dl

mov dl,[row+bx]
mov temp_row,dl



mov snake_loop,bl
cmp snake_loop, 0
je printhead
jne printbody

printhead:
complete_print temp_row,temp_col,head,color
jmp finish

printbody:
complete_print temp_row,temp_col,body,color_body

finish:
inc snake_loop
mov bl, snake_loop
mov al, snake_loop
cmp al, snake_length
;continue to print the body of the snake
jl print_snake



mov bl, snake_length

;transfer the coordinates   
;to insert new coordinates

transfer:
dec bx
mov dl ,[col+bx]
mov [col + bx + 1],dl

mov dl,[row + bx]
mov [row+bx+1],dl
cmp bx,0
jg transfer
jmp return_me


return_me:
ret
mov ah,00h
int 16h
mov input, 'd'
call clear
;initialize starting body snake :)
mov row[0],12

mov row[1],12
mov row[2],12
mov row[3],12
mov row[4],12

mov col[0],40

mov col[1],39
mov col[2],38
mov col[3],37
mov col[4],36

print:  
call snake
call random_coor_food
call print_food
getinput:
;to change direction or to keep going
; wait for keystroke
; if no key is pressed, value of input will be 
;last key pressed.
    mov ah, 01h
    int 16h
    jz key

;key is pressed, new value of input
    mov ah,00h
    int 16h
    mov input, al

key: 
;UP
cmp input, 'w'
je w 
;DOWN
cmp input, 's'
je s
;LEFT
cmp input, 'a'
je a 
;RIGHT
cmp input, 'd'
je d

cmp input, 'q'
je end_game

jne getinput

;make the snake go up
w:

call upy
jmp rak

;make the snake go up
s:

call downy
jmp rak

;make the snake go left
a:

call lefty
jmp rak

;make the snake go right
d:

call righty
jmp rak



rak:


 comment @
 mov bh,0
mov ah,08h ;scan
int 10h
cmp al, body
je end_game
cmp al, food
je increase
@


call snake  


;compare the coordinates of head to the coordinates of the food
;kinda made a mistake over here. 
;#houston, we got a problem 
mov cl,row[0]
cmp cl,rand_food_col
jne again

mov cl,col[0]
cmp cl,rand_food_row
jne again

;//////首先检查蛇是否击中身体 ;如果击中,则游戏结束 评论@ mov bl,0 hit_me_baby: mov dl,[COL+BX+1] cmp Col[0],dl 又是jne mov dl,[行+BX+1] cmp行[0],dl 又是jne je结束游戏 inc bl cmp bl,snake_length jl hit_me_baby

主端

共有1个答案

宗乐池
2023-03-14

这里是你的代码的最后一个编辑版本,我做了两个小小的改动:第一,在标签rak:,我添加了另一个跳转到increase:,第二,在标签increase:,我注释了跳转到rak,这里是:

;Snake Game without Borders
.model small

.data

row db 0FEh dup (?)
col db 0FEh dup (?)

temp_row db ?
temp_col db ?

;variables for food
rand_food_col db ?
rand_food_row db ?


;delay for snake movement
delaytime db 1

;use this delay time to pseudorandom the col and row for food
delay_food db 3


food db 'F' ,'$'


head db '@' ,'$'
snake_length db 5
snake_loop db ?

color db 0Fh
food_color db 0Fh

input db ?
;variables to display text messages:
game_over db 'GAME OVER';
.stack 100h
.code
;///////////////////////////////////////////////////////////////////////////////////////
;clear registers:
clear_reg proc
xor ax,ax
xor bx,bx
xor cx,cx
xor dx,dx
clear_reg endp
;//////////////////////////////////////////////////////////////////////////////////////////////
;food functions 

random_coor_food proc
;random function for col

mov ah, 00h
int 1ah

mov ax,dx
xor dx,dx
mov cx,20
div cx

mov al,dl
mov rand_food_col,al

;random function for row

mov ah, 00h
int 1ah

mov ax,dx
xor dx,dx
mov cx, 71
div cx

mov al,dl
mov rand_food_row,al

ret
random_coor_food endp


print_food proc
mov dl, rand_food_row
mov dh, rand_food_col
xor bh,bh
mov ah, 02h
int 10h

mov dx, offset food
mov ah, 09h
int 21h
ret
print_food endp

;//////////////////////////////////////////////////////////////////////////////////////////////
lefty proc
    mov dl,col[0]
    cmp dl, 0
    je resetposr

    sn:
    dec dl
    jmp leftyie

    resetposr:
    mov dl, 79


    leftyie:
    mov col[0],dl
    ret 
    lefty endp

righty proc
    mov dl,col[0]
    cmp dl,79
    je resetposl

    zero: 
    inc dl
    jmp rightyie


    resetposl:
    mov dl, 0

    rightyie:
    mov col[0],dl
    ret 
righty endp


upy proc
    mov dh,row[0]
    cmp dh,0
    je resetposd

    upzero:
    dec dh
    jmp uptie

    resetposd:
    mov dh,24


    uptie:
    mov row[0],dh
    ret


upy endp

downy proc
mov dh,row[0]
cmp dh,24
je resetposu

gozero:
inc dh
jmp downty

resetposu:
mov dh, 0

downty:
mov row[0],dh
ret 
downy endp

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


clear proc near
    mov al, 03h 
    mov ah, 00h
    int 10h

    mov cx, 3200h  ;stop cursor blinking
    mov ah, 01h
    int 10h
    ret
clear endp

;////////////////////////////////////////////////////////////////////////////////
gameover proc
    call clear
    mov dh, 12 ;row
    mov dl, 35 ;column
    xor bh, bh
    mov ah, 02h
    int 10h 

     mov dx, offset game_over
    mov ah, 09h
    int 21h


    mov ax, 4c00h
    int 21h
gameover endp
;this macro will print the whole snake 
; r is for row c is for column char and color: check .data for values
complete_print macro r,c,char,color
    mov dh, r
    mov dl, c
    xor bh, bh
    mov ah, 02h
    int 10h 

    mov al, char
    mov bh, 0
    mov bl, color
    mov cx, 1
    mov ah, 09h
    int 10h 
endm
;function to print the snake body
snake proc

    call delay
    call clear


    mov bl,0
    mov bh,0
    print_snake:

    mov dl,[col+bx]
    mov temp_col,dl

    mov dl,[row+bx]
    mov temp_row,dl

    mov snake_loop,bl

    print_me:
    complete_print temp_row,temp_col,head,color
    inc snake_loop
    mov bl, snake_loop
    mov al, snake_loop
    cmp al, snake_length
    ;continue to print the body of the snake
    jl print_snake


    mov bl, snake_length

    ;transfer the coordinates   
    ;to insert new coordinates

    transfer:
    dec bx
    mov dl ,[col+bx]
    mov [col + bx + 1],dl

    mov dl,[row + bx]
    mov [row+bx+1],dl
    cmp bx,0
    jg transfer



    ret
snake endp

main proc
;//////////////////////////////////////////////////////////////////////
      mov ax, @data
    mov ds, ax
    call clear
    ;initialize starting body snake :)
    mov row[0],12

    mov row[1],12
    mov row[2],12
    mov row[3],12
    mov row[4],12

    mov col[0],40

    mov col[1],39
    mov col[2],38
    mov col[3],37
    mov col[4],36

    print:  
    call snake
    call random_coor_food
    call print_food

;//////////////////////////////////////////////////////////////////////
; moving the snake
    ;initialize the keyboard input:

    mov ah,00h
    int 16h
    mov input, 'd'

    getinput:
    ;to change direction or to keep going
    ; wait for keystroke
    ; if no key is pressed, value of input will be 
    ;last key pressed.
        mov ah, 01h
        int 16h
        jz key

    ;key is pressed, new value of input
        mov ah,00h
        int 16h
        mov input, al

    key: 
    ;UP
    cmp input, 'w'
    je w 
    ;DOWN
    cmp input, 's'
    je s
    ;LEFT
    cmp input, 'a'
    je a 
    ;RIGHT
    cmp input, 'd'
    je d

    jne getinput

    ;make the snake go up
    w:

    call upy
    jmp rak

    ;make the snake go up
    s:

    call downy
    jmp rak

    ;make the snake go left
    a:

    call lefty
    jmp rak

    ;make the snake go right
    d:

    call righty
    jmp rak



    rak:
    ;////// check first if snake hits the body
    ;if hit, then game over
    mov bh,0
    mov ah,08h ;scan
    int 10h
    cmp al, head
    je end_game
    cmp al, food
    je increase




    call snake  
    mov cl,row[0]
    cmp cl,rand_food_col
    jne again

    mov cl,col[0]
    cmp cl,rand_food_row
    jne again


    ;compare the coordinates of head to the coordinates of the food


    increase:       
    ;if the head row and col is equal to the coordinates of the food. increase snake_length then generate food again.
    inc snake_length
    call random_coor_food

;    jmp rak

    again:
    call print_food
    jmp getinput



    end_game:
    call gameover

    mov ax, 4c00h
    int 21h

main endp
end main

说明:在increase:,当它跳回到rak时,光标位置的char是“@”,所以,我们要做的不是跳到rak而是getinput。

 类似资料:
  • 问题内容: 我希望我的班级实现保存和加载功能,这些功能只是对班级进行Pickle。但是显然您不能以以下方式使用“自我”。你该怎么做? 问题答案: 这就是我最终要做的。更新方法是,我们保留我添加到类中的所有新成员变量,并仅更新上次腌制对象时存在的成员变量。在类本身内部维护保存和加载代码时,这似乎是最简单的,因此调用代码仅执行object.save()。

  • 本文向大家介绍分享自己用JS做的扫雷小游戏,包括了分享自己用JS做的扫雷小游戏的使用技巧和注意事项,需要的朋友参考一下 引用了jQuery,节省了很多鼠标点击上的判断。界面显然都是照搬Windows的扫雷啦,详细的内容注释里都有,我就不啰嗦啦~ 先上截图~ 引用了jQuery,节省了很多鼠标点击上的判断 界面显然都是照搬Windows的扫雷啦 详细的内容注释里都有,我就不啰嗦啦~ JS部分 HTM

  • 文档中的这段代码完全让我感到困惑: 我明白了,摩基托很奇怪而且几乎不在Java。令人困惑的是必须充分评估才能知道它是否被包装在或其他文件中。第一个方法为什么不调用实际对象,而后面的方法却调用了?

  • 帖子底部的实际问题! 首先,我想解释我的问题。 我正在写一个基本的蛇游戏,我让蛇自动移动。当您执行代码时,它会自动移动到窗口的右侧,就像预期的那样。然而,我不能按我想要的方式控制我的蛇,它根本不会改变方向。 为了避免混淆,是类的一个实例。 为了解释蛇的运动: 对象有一个属性,它是一个包含对象的数组列表。每个对象都有和属性。使用此ArrayList,蛇通过在画布的y轴和x轴上使用和属性在画布上绘制小

  • 我正在尝试用libgdx开发一个简单的蛇游戏。我的问题是,每次我想要繁殖一些苹果(纹理,20px宽X 20px高),它总是与蛇的身体重叠。我试图避免这种情况,但它在比赛中不断发生。 snake由多个部分组成-每个部分都是一个20px宽X 20px高的纹理(屏幕宽度是480px宽X 800px高) 以下是我迄今为止所做的尝试: 代码很容易解释。每时每刻,屏幕上都有3个不同的苹果。这段代码试图抽奖x-

  • 问题内容: 即使文件在远程http服务器上不存在,它也会以静默方式返回,它只是将html页面保存到命名文件中。例如: 即使abc.jpg在google.com服务器上不存在,它只是默默返回,生成的不是有效的jpg文件,它实际上是html页面。我猜返回的标头(一个httplib.HTTPMessage实例)可以用来实际上告诉检索是否成功,但是我找不到的任何文档。 有人可以提供有关此问题的一些信息吗?