1.

Solve : Need help with pong program in QB64?

Answer»
Need help with pong program

I made a 2d pong program; when the ball goes off the screen for the first time, it resets it back to its original position and changes the score accordingly. When it goes off the second time it gives an error code. I've already put in several 'on error goto...' statements but i cant seem to get it to work.

Here is my source code:



' Set aside enough space to hold the sprite
DIM ball%(30000000)

ponescore = 0
ptwoscore = 0

numb = 38
begin:
SCREEN 12
char = char + 1
IF char.240 THEN char = 1
ball = ASC(CHR$(char))
numb = numb + 38
CLS
' Draw a filled circle for our sprite
CIRCLE (4, 3), 4, 4
PAINT (4, 3), 12, 4
xmin = 10
ymin = 10
xmax = 630
ymax = 470
x = 25
y = 25
dx = 1
dy = 1
curpos = 50
curtpos = 50

LINE (20, curpos)-(20, curpos + 100)

LINE (620, curtpos)-(620, curtpos + 100)
' Get the sprite into the Ball% array
GET (0, 0)-(8, 7), ball%()

DO

PRINT "Player 1 : "; ponescore; "                               Player 2 : "; ptwoscore

IF x > xmax - 10 AND y >= curtpos AND y <= curtpos + 100 THEN dx = -1
ON ERROR GOTO errorthand
IF x < xmin + 15 AND y >= curpos AND y <= curpos + 100 THEN dx = 1
ON ERROR GOTO errorhandle
IF y > ymax - 5 THEN dy = -1
IF y < ymin + 5 THEN dy = 1
' Display the sprite elsewhere on the screen

x = x + dx
y = y + dy

PUT (x, y), ball%()
ON ERROR GOTO errorthand
LINE (20, curpos)-(20, curpos + 100)
LINE (620, curtpos)-(620, curtpos + 100)
FOR something% = 1 TO 10000
FOR ddd = 1 TO 50
NEXT ddd
keypress:
k$ = INKEY$
IF k$ <> "" THEN GOTO paddle
NEXT something%
CLS

LOOP



errorhandle:
IF ERR = 5 THEN GOTO scorechange

paddle:

IF k$ = "w" THEN curpos = curpos - 2
IF k$ = "s" THEN curpos = curpos + 2
IF k$ = "8" THEN curtpos = curtpos - 2
IF k$ = "5" THEN curtpos = curtpos + 2
LINE (20, curpos)-(20, curpos + 100)
LINE (620, curtpos)-(620, curtpos + 100)
IF curpos < 1 THEN curpos = 1
IF curtpos < 1 THEN curtpos = 1

IF curpos > 379 THEN curpos = 379
IF curtpos > 379 THEN curtpos = 379
GOTO keypress

scorechange:
ponescore = ponescore + 1
GOTO begin

errorthand:
IF ERR > 0 THEN
ptwoscore = ptwoscore + 1
GOTO begin
END IF
You use QB64. I love QB. Anyway, I looked at your program for like half and hour and I COULD not figure it out, but I think it has something to do with not resetting a variable after each loop.Fixed version.

The WAY you had it was you were relying on an error occuring with the PUT in order to detect when the ball left the BOUNDS of the screen.

I just added checks to interrogate that directly, add the appropriate score to the player that scored, and then return control to the begin. Problem was in the case of an error your code wasn't always passing control to BEGIN and therefore you would get an untrappable Illegal Function call on the next iteration when the x and y values make the PUT try to place graphics data outside the screen boundaries. Or so it seemed.

I also 'fixed' your unnecessary large declaration of the ball away. you are only storing 72 pixels (9*8) so why do you need 30000000 indices? (could probably be reduced further, I'm not sure how GET Stores the video data)



Code: [Select]' Set aside enough space to hold the sprite
DIM ball%(72)
ponescore = 0
ptwoscore = 0

numb = 38
begin:
SCREEN 12
char = char + 1
IF char.240 THEN char = 1
ball = ASC(CHR$(char))
numb = numb + 38
CLS
' Draw a filled circle for our sprite
CIRCLE (4, 3), 4, 4
PAINT (4, 3), 12, 4
xmin = 10
ymin = 10
xmax = 630
ymax = 470
x = 25
y = 25
dx = 1
dy = 1
curpos = 50
curtpos = 50

LINE (20, curpos)-(20, curpos + 100)

LINE (620, curtpos)-(620, curtpos + 100)
' Get the sprite into the Ball% array
GET (0, 0)-(8, 7), ball%()

DO

    PRINT "Player 1 : "; ponescore; "                               Player 2 : "; ptwoscore
    x = x + dx
    y = y + dy

    IF x > xmax - 10 AND y >= curtpos AND y <= curtpos + 100 THEN dx = -1
    IF x < xmin + 15 AND y >= curpos AND y <= curpos + 100 THEN dx = 1
    IF y > ymax - 5 THEN dy = -1
    IF y < ymin + 5 THEN dy = 1
    IF x > xmax THEN
        ponescore = ponescore + 1
        GOTO begin
    ELSEIF x < xmin THEN
        ptwoscore = ptwoscore + 1
        GOTO begin
    END IF
    ' Display the sprite elsewhere on the screen

    ON ERROR GOTO errorthand
    OPEN "D:\debugqb.txt" FOR APPEND AS #3
    PRINT #3, "X=" + STR$(x) + " Y=" + STR$(y)
    CLOSE #3
    PUT (x, y), ball%()

    ON ERROR GOTO errorthand
    LINE (20, curpos)-(20, curpos + 100)
    LINE (620, curtpos)-(620, curtpos + 100)
    FOR something% = 1 TO 10000
        FOR ddd = 1 TO 50
        NEXT ddd
        keypress:
        k$ = INKEY$
        IF k$ <> "" THEN GOTO paddle
    NEXT something%
    CLS

LOOP



errorhandle:
IF ERR = 5 THEN GOTO scorechange

paddle:

IF k$ = "w" THEN curpos = curpos - 2
IF k$ = "s" THEN curpos = curpos + 2
IF k$ = "8" THEN curtpos = curtpos - 2
IF k$ = "5" THEN curtpos = curtpos + 2
LINE (20, curpos)-(20, curpos + 100)
LINE (620, curtpos)-(620, curtpos + 100)
IF curpos < 1 THEN curpos = 1
IF curtpos < 1 THEN curtpos = 1

IF curpos > 379 THEN curpos = 379
IF curtpos > 379 THEN curtpos = 379
GOTO keypress

scorechange:
ponescore = ponescore + 1
GOTO begin

errorthand:
IF ERR > 0 THEN
    ptwoscore = ptwoscore + 1
    GOTO begin
END IF



Discussion

No Comment Found