Reputation: 11
I am building pong and I want to get input from two users to move the rackets but when I am using int 16h ah1 (and ah0 to detect if a key was pressed wt all) it move one character at a time.
I found some solutions but becuase I am not a native engilsh speaker I couldn't understand them.
Upvotes: 1
Views: 1087
Reputation: 44066
Int 16/AH=01h is used to check for a keystroke.
While Int 16/AH=00h is used to get the keystroke.
Note that you got them the other way around.
Here a very simple program that polls for keystrokes A, D and Q.
A increments player 1 blue character on row 1.
D increments player 2 red character on row 2.
Q quits the program.
If you press A and D together, both characters get incremented.
It uses a simple Lookup table to dispatch a key to its handler in order to avoid Spaghetti code when the program has a lot of functions.
Use the dispatching method that more suits your needs, giving a general efficient solution is out of the scope of this question.
BITS 16
ORG 100h
mov ax, 0003h
int 10h ;Set a known screen mode
mov ax, 0b800h
mov es, ax ;Set ES to access textual video buffer
;Write two 'A's (Blue and red)
mov WORD [es:0000h], 0941h
mov WORD [es:160], 0c41h
;We need BH zero for later
xor bx, bx
_poll_key:
;CHECK FOR A KEY
mov ah, 01h
int 16h
jz _poll_key
;REMOVE THE KEY FROM THE BUFFER
xor ah, ah
int 16h
mov bl, al
call dispatch_key
jmp _poll_key
;
; D I S P A T C H E R
;
;BX = Key to dispatch
dispatch_key:
push bx
sub bx, 'a'
jb _dk_end
cmp bx, 26
jae _dk_end
shl bx, 1
mov bx, WORD [dispatch_table + bx]
test bx, bx
jz _dk_end
call bx
_dk_end:
pop bx
ret
dispatch_table:
;A
dw _key_a
;B
dw 0
;C
dw 0
;D
dw _key_d
;E
dw 0
;F
dw 0
;G
dw 0
;H
dw 0
;I
dw 0
;J
dw 0
;K
dw 0
;L
dw 0
;M
dw 0
;N
dw 0
;O
dw 0
;P
dw 0
;Q
dw _key_q
;R
dw 0
;S
dw 0
;T
dw 0
;U
dw 0
;V
dw 0
;W
dw 0
;X
dw 0
;Y
dw 0
;Z
dw 0
;
; K E Y H A N D L E R S
;
;EXIT
_key_q:
mov ax, 4c00h
int 21h
;INCREMENT PLAYER 2 LETTER
_key_d:
inc BYTE [es:160]
ret
;INCREMENT PLAYER 1 LETTER
_key_a:
inc BYTE [es:0000h]
ret
Code is for NASM, to produce a COM file.
Upvotes: 1