Very Basic help needed

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!
Post Reply
Kyle_Katarn
Newbie
Posts: 1
Joined: Thu Jul 20, 2006 8:20 pm

Very Basic help needed

Post by Kyle_Katarn »

Hey, newbie here.

I'm doing a PONG game for my software design class, and whatever I do, I cannot get my bat to work, it's driving me nuts.

Can someone please have a look at it?

Code: Select all

CLS
REM Demo Only
REM Initialisation
mbatx = 1
mbaty = 12
pbatx1 = 79
pbaty1 = 12
pbatx2 = 79
pbaty2 = 13
pbatx3 = 79
pbaty3 = 14
xpos = 2
ypos = 13
xposscore = 2
xdir = 1
ydir = 1
xincrement = 1
yincrement = .25
REM Introductory Screen
PRINT
PRINT TAB(6); "This is PONG the first ever computer game."
PRINT
PRINT TAB(6); "Machine is on the left."
PRINT TAB(20); "You are on the right."
PRINT
PRINT TAB(6); "Move the paddle with the up and down arrows."
PRINT
PRINT TAB(6); "Best out of 3."
PRINT TAB(6); "For every round you win, the speed increases."
PRINT
PRINT
INPUT "Enter the speed you prefer [1/9] slowest/fastest"; speed
CLS

DO WHILE playscore = 0 AND machscore = 0
REM User Score Interface
PRINT "Player", , , , "Com"
PRINT playscore, , , , machscore

REM Draw Machine Bat
DO WHILE mbaty = 1 OR mbaty <= 22
mbaty = ypos - 1
LOCATE mbaty, mbatx
PRINT "|"
PRINT "|"
PRINT "|"


REM Draw Player Bat
LOCATE pbaty1, pbatx1
PRINT "|"
LOCATE pbaty2, pbatx2
PRINT "|"
LOCATE pbaty3, pbatx3
PRINT "|"
IF pbaty < 22 AND INKEY$ = CHR$(80) THEN pbaty = pbaty - 1
IF pbaty > 2 AND INKEY$ = CHR$(72) THEN pbaty = pbaty + 1

REM Motion Part
        xpos = xpos + (xdir * xincrement)
        ypos = ypos + (ydir * yincrement)
        LOCATE ypos, xpos
        PRINT CHR$(15)
                REM Delay loop
                FOR d = 1 TO (35000 - speed * 3000)
                NEXT d

REM Move Player Bat
IF INKEY$ = CHR$(24) THEN
pbaty1 = pbaty1 - 1
pbaty2 = pbaty2 - 1
pbaty3 = pbaty3 - 1
END IF

REM Move Machine Bat

REM Bounce off Walls
       IF xpos = 79 THEN
           IF ABS(ypos - pbaty - 1) < 1.5 THEN xdir = xdir * -1
       END IF
       IF xpos = 2 THEN
           IF ABS(ypos - mbaty - 1) < 1.5 THEN xdir = xdir * -1
       END IF
      
       IF xpos < 2 THEN xdir = xdir * -1
       IF ypos > 22 THEN ydir = ydir * -1
       IF ypos < 2 THEN ydir = ydir * -1
REM Refresh the screen
       IF xpos = 80 THEN xdir = xdir * -1
       CLS
REM Scoring System
       IF xposscore > 79 THEN playscore = playscore + 1
       IF xposscore < 2 THEN machscore = machscore + 1
      
LOOP
LOOP
END

Thanks in advance.
~Kyle Out
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Kyle,

I tried your program. t runs, but ignores the up/down arrows because you forgot that they are expanded ASCII codes.
Down Arrow is chr$(0)+chr$(80)
and
Up Arrow is chr$(0)+chr$(72)

You also have an INKEY looking for a chr$(24). I don't know what that should be.

Try these and see.

Regards..... Moneo
nkk_kan
Veteran
Posts: 57
Joined: Thu Jun 01, 2006 10:45 am
Location: Gujrat,India,Asia
Contact:

Post by nkk_kan »

moneo

i think she was trying to catch the "up" keypress by chr$(24)
you understood...Didn't you? :D
User avatar
BadMrBox
Veteran
Posts: 86
Joined: Tue Feb 28, 2006 12:19 pm

Post by BadMrBox »

"This is PONG the first ever computer game."
Pong wasn't the first computer game...

Anyhow, looking through the code I'm starting to wonder what pbaty is for?
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

nkk_kan wrote:moneo

i think she was trying to catch the "up" keypress by chr$(24)
you understood...Didn't you? :D
I suspected that, but I wasn't sure.
The ASCII character 24 in decimal is CAN (cancel), although if you do a
PRINT CHR$(24)
it prints out a character that looks like an up-arrow. But it's not.

An up-arrow is read in by the INKEY$ function as an extended character (2 bytes) containing Chr$0) and Chr$(72) as we said before.

The usage of INKEY$ and the extended characters can be very confusing, especially when some characters like TAB (09), CARRIAGE-RETURN (13), and ESCAPE (27) are NOT read in by INKEY$ as extended characters.

BTW, what made you refer to Kyle as "she"?

*****
Post Reply