0

我正在用汇编语言编写游戏代码,其中恐龙的移动取决于用户按下的键。我已经实现了我的代码,如果用户按下空格键,程序应该终止并且恐龙应该在按下“a”时朝正确的方向移动。但是我面临一个问题,即在第一次按下任何键后,程序不会寻找在显示它一次又一次按下第一个键之后按下的任何其他键。如何解决?

我正在使用 mov ah, 01h 和 int 16h 的函数,它返回 al 寄存器中按下的键的 ascii。在此之后,我将其与所需的 ascii 键进行比较,但这 16h 仅在第一次按下任何键时表现良好。

label2: 
        mov ah, 1h
    int 16h
    mov keyboardkey, al        ;keyboardkey is variable for storing ascii of key pressed

    .IF( keyboardkey == 32 )
        ret
    .ENDIF

    .IF( keyboardkey == 97 )
            mov bx, startingXaxis
            add bx, 10
            mov startingXaxis, bx
            call drawdinosaour
    .ENDIF

    .IF (keyboardkey == 98 )
        mov bx, startingXaxis
        sub bx, 10
        mov startingXaxis, bx
        call drawdinosaur
    .ENDIF  

        call delay              ;this function passes the program for 1/4 second
        jmp label2

我期待每当我按下空格键时,程序都会终止,但它只会选择第一次按下的键,然后它会继续根据第一个键进行操作,并且不会查找之后按下的任何键

4

1 回答 1

3

不会从缓冲区中int 0x16, ah =0x01删除键,并且(如果没有按下键)不会等到用户按下键。

确实会从缓冲区中int 0x16, ah =0x00删除键,并且(如果没有按下任何键)会等到用户按下键。

如果您想从缓冲区中删除键但不想等到按下键;那么你必须使用这两个函数,有点像:

    mov ah,0x01
    int 0x16
    jz .doneKeypress
    mov ah,0x00
    int 0x16
    call handleKeypress
.doneKeypress:

对于你正在做的事情(有延迟,用户可以在你做延迟时按下多个键);你可能想要更多类似的东西:

mainLoop:

;Handle all keypresses

.nextKey:
    mov ah,0x01
    int 0x16
    jz .doneKeypresses
    mov ah,0x00
    int 0x16
    call handleKeypress
    jmp .nextKey
.doneKeypresses:

;Update the screen

     call updateScreen

;Do the delay

    call delay

;Keep doing the loop while the game is running (and stop when the game stops)

    cmp byte [isRunning],0
    je mainLoop
    ret
于 2019-11-12T18:18:19.380 回答