【问题标题】:How do I make two codes to run at the same time? (DOS Assembly)如何让两个代码同时运行? (DOS 组装)
【发布时间】:2019-03-31 18:15:52
【问题描述】:

我制作了 2 个方块(我使用的是 DOS 程序集),我试图让一个方块移动,而当用户按下 SPACE 时,另一个方块会跳跃。 问题是用户只能在另一个方块停止移动后按空格键。你能帮帮我吗?

代码:

IDEAL
MODEL small
STACK 100h
DATASEG

X dw ?
Y dw ?
SquareSize dw ?

PlayerSquareX dw 20
PlayerSquareY dw 193

BlockX dw 305
BlockY dw 193

color db 1  ; default color is blue

FloorY dw 194
FloorX dw 0




CODESEG



    proc HalfSecondDelay
    push cx ;Backup
    push dx
    push ax
    
    MOV CX, 1H
    MOV DX, 3H
    MOV AH, 86H
    INT 15H
    
    pop ax ;Backup
    pop dx
    pop cx
    ret
    endp HalfSecondDelay

    
    proc WaitForSpace
    EnterSpace:  ;Wait until user presses Space
    mov ah,0
    int 16h
    cmp al,32
    ;jne EnterSpace
    jne ReturnToMain
    call PlayerJump
    ReturnToMain:
    ret
    endp WaitForSpace
    
    proc ClearKeyboardBuffer
    push ax
    ClearKeyboardBuffer_loop:
    mov ah,01h
    int 16h                     ; is there a key pressed
    jz ClearKeyboardBuffer_ret  ; if not, return
    mov ah,00h
    int 16h                     ; "handle" the key
    jmp ClearKeyboardBuffer_loop

    ClearKeyboardBuffer_ret:
    pop ax
    ret
endp ClearKeyboardBuffer


    proc PlayerJump ;Makes The Square Jump
    push cx
    
    
    mov cx, 10
    MoveUp:
    Call HalfSecondDelay
    
    call RemovePlayerSquare
    sub [PlayerSquareY],4 ; Move Up
    call PaintPlayerSquare
    loop MoveUp
    
    mov cx, 10
    MoveDown:
    Call HalfSecondDelay
    call RemovePlayerSquare
    add [PlayerSquareY], 4 ; Move Down
    Call PaintPlayerSquare
    loop MoveDown

    pop cx
    ret
    endp PlayerJump


proc PaintFloorLine
    push bp
    mov bp,sp
    push cx


    
    mov cx, 320
LoopPaintFloorLine:
    call PaintPixel
    
    inc [X]
    loop loopPaintFloorLine
    

    pop cx
    pop bp
    ret
endp PaintFloorLine

proc PaintFloor ;Paints The Floor
    push bp
    mov bp,sp
    push cx
    
    mov [x], 0
    mov [y], 194
    mov [color], 0Ah
    
    mov cx, 7
    
loopPaintFloor:     
    call PaintFloorLine
    inc [Y]
    mov [x], 0
    loop loopPaintFloor
    
    
    pop cx
    pop bp
    ret
endp PaintFloor
    

proc PaintPixel
    push bp
    mov bp,sp

    push ax
    push bx
    push cx
    push dx 
    

    mov cx,[X]
    mov dx,[Y]
    mov al,[color]
    mov ah,0ch
    int 10h
    
    pop dx
    pop cx
    pop bx
    pop ax
    pop bp
    ret
endp PaintPixel

proc PaintLine
    push bp
    mov bp,sp
    push cx
    
    mov cx, [SquareSize]
    
loopPaintLine:  
    call PaintPixel
    inc [X]
    loop loopPaintLine
    
    mov cx, [SquareSize] ; Return The X
    sub [X], cx
    
    pop cx
    pop bp
    ret
endp PaintLine

    
proc PaintSquare ;Paints A Sqaure
    push bp
    mov bp,sp
    push cx
    
    mov cx, [SquareSize]
    
loopPrinSquare:     
    call PaintLine
    dec [Y]
    loop loopPrinSquare
    
    mov cx, [SquareSize] ; Return The Y
    add [Y], cx
    
    pop cx
    pop bp
    ret
endp PaintSquare

proc PaintPlayerSquare ; Paints The Player Square
    push bp
    mov bp,sp
    
    mov ax, [PlayerSquareX]
    mov [x], ax
    
    mov ax, [PlayerSquareY]
    mov [Y], ax
    
    mov [SquareSize], 25
    mov  [color], 1 ; blue color
    call PaintSquare ;  
    
    pop bp
    ret 
endp PaintPlayerSquare

proc RemovePlayerSquare ;Removes The Player Square
    push bp
    mov bp,sp
    
    push ax
    push bx
    push cx
    push dx 
    
    mov ax, [PlayerSquareX]
    mov [x], ax
    
    mov ax, [PlayerSquareY]
    mov [Y], ax
    
    mov [SquareSize], 25
    mov  [color], 0 ; black color
    call PaintSquare ;  
    
    pop dx
    pop cx
    pop bx
    pop ax
    
    pop bp
    ret 
endp RemovePlayerSquare


proc PaintBlockSquare ; Paints The Block Square
    push bp
    mov bp,sp
    
    
    mov ax, [BlockX]
    mov [x], ax
    
    mov ax, [BlockY]
    mov [Y], ax
    
    mov [SquareSize], 15
    mov  [color], 4 ; red color
    call PaintSquare ;  
    
    pop bp
    ret 
endp PaintBlockSquare

proc RemoveBlockSquare ;Removes The block Square
    push bp
    mov bp,sp
    
    push ax
    push bx
    push cx
    push dx 
    
    
    mov ax, [BlockX]
    mov [x], ax
    
    mov ax, [BlockY]
    mov [Y], ax
    
    mov [SquareSize], 15
    mov  [color], 0 ; black color
    call PaintSquare ;  
    
    pop dx
    pop cx
    pop bx
    pop ax
    
    pop bp
    ret 
endp RemoveBlockSquare

proc MoveBlockLeft ;Makes The Square Jump
    push cx
    
    
    mov cx, 77
    loopMoveBlockLeft:
    Call HalfSecondDelay
    
    call RemoveBlockSquare
    sub [BlockX], 4 ; Move Up
    call PaintBlockSquare
    loop loopMoveBlockLeft
    


    pop cx
    ret
endp MoveBlockLeft



proc GraphicsScreen
    mov al, 13h
    mov ah, 0
    int 10h
    ret
endp GraphicsScreen

proc ResetBlockLocation
    mov [BlockX], 305
    mov [BlockY], 193
    ret
endp ResetBlockLocation

    
start:
    mov ax, @data
    mov ds, ax
    
    Call GraphicsScreen
    Call PaintFloor
    Call PaintPlayerSquare
    
    
    call PaintBlockSquare
    call MoveBlockLeft
    call RemoveBlockSquare
    call ResetBlockLocation
    call ClearKeyboardBuffer
    call WaitForSpace
    

    

exit:
    mov ax, 4c00h
    int 21h
END start

【问题讨论】:

  • 请不要从您的问题中删除代码 sn-ps!你的新的、简短的、单行的问题并不是这个论坛的真正目的。人们来这里学习,您的代码为他们提供了这样做的机会。我敢肯定你里面没有任何秘密,是吗?
  • 您可以随时改写问题和标题,但始终保持其原意,以免使给出的答案无效。

标签: assembly dos x86-16 tasm


【解决方案1】:

一种方法是实现game loop,或类似的概念 - event loop

这个想法是将您的动画分割成一系列帧,并且一次只更新一帧。假设你想做 30 FPS,你的移动速度是每秒 90px。这意味着您需要在 1 帧中将对象移动大约 3px,然后暂停 0.033 秒(33 ms)。

在每一帧之后检查输入和处理动作。

它的运行方式是你开始第一个动画,记住你的动画状态,更新到下一帧,然后检查输入,更新到下一帧,然后再次检查输入,也许开始第二个动画(现在您一次处理 2 个动画状态),将两者都更新到下一帧,再次检查输入等。

【讨论】:

  • 您的代码中有一堆循环,我假设您理解它们。因此,您需要添加另一个循环并更改动画的工作方式。究竟是什么不清楚?你读过链接吗?
猜你喜欢
  • 1970-01-01
  • 2022-10-05
  • 2020-10-12
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
相关资源
最近更新 更多