我正在尝试为自己的汇编语言练习制作一个小型乒乓球游戏,因为我是该领域的初学者。
我在我的 x86 64 位 Windows 电脑上运行代码。我正在使用 DOSBox 模拟器来运行我的游戏并测试它。
我遇到的问题是:-
在尝试为我的乒乓球游戏绘制球时,模拟器显示一条矩形水平线,我无法将其修复为正方形。
这是我到目前为止写的代码
STACK SEGMENT PARA STACK
DB 64 DUP (' ')
STACK ENDS
DATA SEGMENT PARA 'DATA'
BALL_X DW 0Ah ;current X position (column) of the ball
BALL_Y DW 0Ah ;current Y position (line) of the ball
BALL_SIZE DW 04h ;size of the ball (how many pixels does the ball have in width and height)
DATA ENDS
CODE SEGMENT PARA 'CODE'
MAIN PROC FAR
ASSUME CS:CODE,DS:DATA,SS:STACK ;assume as code, data and stack segments the respective registers
PUSH DS ; Push the DS segment to the stack
SUB AX, AX ; Clean the AX register
PUSH AX ; Push AX to the stack
MOV AX, DATA ; Load the DATA segment into AX
MOV DS, AX ; Set DS to the DATA segment
POP AX ; Release top item from stack
POP AX ; Release top item from stack
MOV AH, 00h ; Set the video mode configuration
MOV AL, 13h ; Choose the video mode (320x200 256-color VGA mode)
INT 10h ; Execute the configuration
MOV AH, 0Bh ; Set the background color
MOV BH, 00h ; Page number (usually 0)
MOV BL, 00h ; Choose black as the background color
INT 10h ; Execute the configuration
CALL DRAW_BALL
RET
MAIN ENDP
DRAW_BALL PROC NEAR
MOV CX,BALL_X ;set the coloumn (X)
MOV DX,BALL_Y ;set the line (Y)
DRAW_BALL_HORIZONTAL:
MOV AH,0Ch ;set configuration to writing a pixel
MOV AL,0Fh ;set pixel color white
MOV BH,00h ;set the page number
INT 10h ;exec the config
INC CX ;CX = CX+1
MOV AX,CX ;CX - BALL_X > BALL_SIZE (Y-> We go to the next line, N-> We continue to the next column)
SUB AX,BALL_X
CMP AX,BALL_SIZE
JNG DRAW_BALL_HORIZONTAL
MOV CX,BALL_X ;the CX register goes back to initial column
INC DX ;DX = DX + 1
MOV AX,DX ;DX - BALL_Y > BALL_SIZE (Y-> We got to the next line, N-> We continue to the next column)
SUB DX,BALL_Y
CMP AX,BALL_SIZE
JNG DRAW_BALL_HORIZONTAL
RET
DRAW_BALL ENDP
CODE ENDS
球的代码写在DRAW_BALL_HORIZONTAL中。
我尝试了不同的迭代来修复它以表示方块,但我仍然无法做到。
这里似乎有什么问题?我该如何解决它?
我将
draw_ball proc
的最后一部分更改为以下代码:
mov ax,dx
mov bx,BALL_Y
add bx,10
cmp dx,bx
jng DRAW_BALL_HORIZONTAL
并且成功了。因为
ball_y
是代码开始绘制球的第一行,并且绘制应在 ball_y + 10
处结束。