Qbasicnews.com

Full Version: need hellp
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Im trying to make this 2 player pong sort of game and i've run into a problem.
If player 1 wants to move up and so holds "q"(up), while player 2 wants to move down so at the same time holds "l"(down), then the the first player stops moving.
Like how if you hold down a and then press and hold d then it look like this.
aaaaaaaaaaaaaaaaddddddddddddddddd
I can make it so if player 1 starts moving down and player 2 presses something then player 1 keeps moving.
But i really want the bat to stop moving when you let go of the button.
Can anyone help?
My current program is this:


RANDOMIZE TIMER
13 j = -2 + INT(RND * 5)
IF j = 0 THEN GOTO 13
14 o = -2 + INT(RND * 5)
IF o = 0 THEN GOTO 14
v = 0
n = 0
1 RANDOMIZE TIMER
SCREEN 7, 0, 1, 0
s% = 1
h% = 1
x = 1
y = 100
a = 320
b = 100
l = 90 + RND * 20
m = 150 + RND * 20
DO
IF m < x THEN
v = v + 1
GOTO 1
END IF
IF m > a THEN
n = n + 1
GOTO 1
END IF
CLS
PRINT " p1 score="; n; " p2 score="; v
LINE (x, y)-(x + 5, y + 20), 15, BF
LINE (a, b)-(a - 5, b + 20), 15, BF
CIRCLE (m, l), 4, 1
PAINT (m, l), 1
m = m + j
l = l + o
IF l < 2 THEN o = o + 1
IF l > 195 THEN o = o - 1
IF m < x + 5 AND l > y - 5 AND l < y + 25 THEN
m = x + 5
j = j + 4
END IF
IF m > a - 5 AND l > b - 5 AND l < b + 25 THEN
m = a - 5
j = j - 4
IF j > 2 THEN j = 2
IF j < -2 THEN j = -2
END IF
p1$ = INKEY$

SELECT CASE p1$
CASE "q"
a% = 1
CASE "a"
a% = 2
CASE "p"
b% = 1
CASE "l"
b% = 2
CASE "x"
END
END SELECT

SELECT CASE a%
CASE 2
IF y < 180 THEN y = y + s%
CASE 1
IF y > 0 THEN y = y - s%
END SELECT

SELECT CASE b%
CASE 2
IF b < 180 THEN b = b + h%
CASE 1
IF b > 0 THEN b = b - h%
END SELECT
PCOPY 1, 0
LOOP


Yes, i know it has a lot of useless stuff in it, ive just started with qbasic and im also lazy so if anything is redundant il probably leave it in rather than take it out.

and uh.. should this be in the newbie forum or here?
What the hell are you doing? :evil:
You're going to want to find a library with better keyboard routines than inkey$. inkey$ was not designed for games, and that delay is intentional because it's meant for taking input for typing. Multikey by the late Milo Sedlacek is a great one. All the fancy libraries (dqb, etc) have one. There are also a few I can't remember.

As for getting input with qb, it's tricky because qb can't get or set interrupt vectors. What you can do is kind of fake it by scanning the keyboard port.

keycode = inp(&H80);

the way this works is, every time a key is pressed or released, a scan code for that key is returned. When you press a key, it's between 0 and 127. When you release it, 128 and 255. So you can make an array and keep track of which keys are pressed and released.

Code:
dim keyarray (127) as integer

do 'main game loop
    keybd = inp(&H80)
    if keybd < 128 then keyarray(keybd) = 1
    if keybd > 128 then keyarray(keybd - 128) = 0

    'this clears the keyboard buffer, don't worry about it
    def seg = 0: poke &H41A, peek (&H41C): def seg
loop

Now if you want to check if a key is pressed down, do this test:
Code:
if keybd(scancode) = 1 then 'key is pressed down

if you want a key's scancode, run this code and press that key:
Code:
do
keybd = inp(&H80)
if keybd < 128 then locate 1, 1: print keybd
blah$ = inkey$
loop

The main problem with the pure qb solution is that sometimes it "misses" a keypress, and the key is locked down. This usually only happens with the arrow keys, though. You're best off getting a library.
thanks, thats exactly what i was looking for

but uh...

i tried:

do
keybd = inp(&h80)
print keybd
blah$ = inkey$
loop until blah$ = "x"

that should print the values of the keys im pressing down and then releasing right?

but for some reason it just keeps printing 105, even when im not pressing anything

whats happening?
Superfrog, please only post in one section, that is all that is required, someone will always help you out.

As for the key handler, I would give you a hand but i am going to be late for something if I dont run now.
yeh, sorry , i wasnt sure how things worked around here

i figured out why it wasnt working
i went to device manager and resources and it says the i/o for my printer is 60

so i tried (&h60) instead of (&h80) and it worked...

whys it different?
and if i tried to run my program on another computer would it then not work unless i changed the value?
if so is there anything i can do about that?
It's supposed to be &H60, not &H80...

I've never used 80, and always had good luck with 60...


But liek already said, get a multikey handler.
uh... whats a multikey handler and how do i get one?
It's a routine/function that let's you trap multiple keys at once, allowing you to have up to 3 keys pressed at one time.

Do a search, Milo's keyhandler is one of the more famous ones.
ok
thanks all
Pages: 1 2