Pong help.

Previous topic - Next topic

cruelcynic

I have worked through the Pong tutorial and the ball won't move. I can't seem to find what is wrong. Any help would be appreciated.
Code (glbasic) Select

// --------------------------------- //
// Project: Pong
// Start: Monday, June 04, 2012
// IDE Version: 10.202


GLOBAL bat_y[];DIM bat_y[2]
GLOBAL bat_x[];DIM bat_x[2]
GLOBAL score[];DIM score[2]
GLOBAL ball_x,ball_y,ball_sx,ball_sy,col

GOSUB Init
MainLoop:
GOSUB MoveAll
GOSUB ShowAll
GOTO MainLoop




// ------------------------------------------------------------- //
// ---  INIT  ---
// ------------------------------------------------------------- //
SUB Init:
GOSUB ResetBall
// Draw playfield, use as background bitmap
BLACKSCREEN
DRAWRECT 0,0,640,16, RGB(255, 255, 255)
DRAWRECT 0,464,640,480, RGB(255,255,255)
DRAWRECT 312,0,16,480,RGB(255,255,255)
USEASBMP

//reset bat y location
bat_y[0]=240; bat_y[1]=240
//reset bat x location
bat_x[0]=16; bat_x[1]=600


ENDSUB // INIT




// ------------------------------------------------------------- //
// ---  RESETBALL  ---
// ------------------------------------------------------------- //
SUB ResetBall:
LOCAL ball_sx,ball_sy
ball_x=320
ball_y=240

IF ball_sx<0
ball_sx=1
ELSE
ball_sx=-1
ENDIF

ball_sy=1


ENDSUB // RESETBALL




// ------------------------------------------------------------- //
// ---  SHOWALL  ---
// ------------------------------------------------------------- //
SUB ShowAll:
//show the bats
FOR num=0 TO 1
DRAWRECT bat_x[num],bat_y[num],16,64,RGB(255,255,255)
PRINT score[num],num*320 + 32,16
NEXT
//draw the ball
DRAWRECT ball_x,ball_y,16,16,RGB(255,255,255)
SHOWSCREEN



ENDSUB // SHOWALL




// ------------------------------------------------------------- //
// ---  MOVEALL  ---
// ------------------------------------------------------------- //
SUB MoveAll:
//paddles
FOR num=0 TO 1
//keys: A, Z
IF KEY(30) THEN bat_y[0]=bat_y[0]-2
IF KEY(44) THEN bat_y[0]=bat_y[0]+2
//keys /\,\/
IF KEY (200) THEN bat_y[1]=bat_y[1]-2
IF KEY (208) THEN bat_y[1]=bat_y[1]+2

// bat at upper/lower border?
IF bat_y[num]<0 THEN bat_y[num]=0
IF bat_y[num]>416 THEN bat_y[num]=416
NEXT
//ball
ball_x=ball_x+ball_sx
ball_y=ball_y+ball_sy
//ball at bottom
IF ball_y>464
ball_y=464
ball_sy= -ball_sy
ENDIF
//ball at ceiling
IF ball_y<0
ball_y=0
ball_sy= -ball_sy
ENDIF
//ball left border score player 1
IF ball_x<0
score[1]=score[1]+1
GOSUB ResetBall
ENDIF
//ball right border score player 0
IF ball_x>624
score[0]=score[0]+1
GOSUB ResetBall
ENDIF
FOR num=0 TO 1
IF (ball_sx<0 AND num=0) OR (ball_sx>0 AND num=1)
col=BOXCOLL (bat_x[num],bat_y[num],16,64,ball_x,ball_y,16,16)
IF col=TRUE
//turn ball speed in x direction
ball_sx=-ball_sx
//speed up ball
ball_sx=ball_sx*1.2
ball_sy=ball_sy*1.05
ENDIF
ENDIF
NEXT


ENDSUB // MOVEALL

Darmakwolf

What do you mean? I just went into the samples folder and compiled "pong" and it worked fine...

cruelcynic

I am talking about the Pong that I built with the written tutorial not the pre-built samples. It does not start.

erico

Your RESETBALL subroutine is re stating ball variables to be local. Like, it is GLOBAL at the beguinning but then it gets LOCAL there.
So, get rid rid of the first line (LOCAL ball_sx,ball_sy).

It should work then.

If you LOCAL a variable, it won´t pass to other parts of your code, hence it won´t move.
If it is GLOBAL, every part of the code will see it.

I´m not sure I explained the best of ways :-[

fuzzy70

I just copied the full source from the helpful & had 2 errors.  Replace the BLACKSCREEN command with CLEARSCREAN. unless you want to go through the entire code & work out what is a global variable & what's local then select "options" under the project menu & make sure "Explicit declarations" is unticked.

After I made those 2 changes it compiled & ran fine.

Lee

Sent from my GT-I5700 using Tapatalk 2
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

erico

@fuzzy70 , you mean the code posted here or the help files?

I tried the one here, but  BLACKSCREEN didn´t report an error on my side.

Slydog

I found the major issue, as reported by erico. (Global vs Local)

There is a minor problem in the MoveAll sub, where the paddles keys are checked twice, since it is in a loop. (fixed below)
I fixed / replaced the 'BLACKSCREEN' command too, since it's deprecated.
I reformatted it for proper indenting (makes it easier to read IMO).
Finally, I converted it to use FUNCTIONs, and not SUBs and GOTOs.

Code (glbasic) Select
// --------------------------------- //
// Project: Pong
// Start: Monday, June 04, 2012
// IDE Version: 10.202

GLOBAL bat_y[];DIM bat_y[2]
GLOBAL bat_x[];DIM bat_x[2]
GLOBAL score[];DIM score[2]
GLOBAL ball_x,ball_y,ball_sx,ball_sy,col
GLOBAL ball_sx,ball_sy

Init()

WHILE TRUE
MoveAll()
ShowAll()
WEND


// ------------------------------------------------------------- //
// ---  INIT  ---
// ------------------------------------------------------------- //
FUNCTION Init:
ResetBall()

// Draw playfield, use as background bitmap
CLEARSCREEN
DRAWRECT 0,0,640,16, RGB(255, 255, 255)
DRAWRECT 0,464,640,480, RGB(255,255,255)
DRAWRECT 312,0,16,480,RGB(255,255,255)
USEASBMP

//reset bat y location
bat_y[0]=240; bat_y[1]=240
//reset bat x location
bat_x[0]=16; bat_x[1]=600
ENDFUNCTION // INIT


// ------------------------------------------------------------- //
// ---  RESETBALL  ---
// ------------------------------------------------------------- //
FUNCTION ResetBall:
ball_x=320
ball_y=240

IF ball_sx<0
ball_sx=1
ELSE
ball_sx=-1
ENDIF

ball_sy=1
ENDFUNCTION // RESETBALL


// ------------------------------------------------------------- //
// ---  SHOWALL  ---
// ------------------------------------------------------------- //
FUNCTION ShowAll:
//show the bats
FOR num=0 TO 1
DRAWRECT bat_x[num],bat_y[num],16,64,RGB(255,255,255)
PRINT score[num],num*320 + 32,16
NEXT

//draw the ball
DRAWRECT ball_x,ball_y,16,16,RGB(255,255,255)
SHOWSCREEN
ENDFUNCTION // SHOWALL


// ------------------------------------------------------------- //
// ---  MOVEALL  ---
// ------------------------------------------------------------- //
FUNCTION MoveAll:
// PADDLES

//keys: A, Z
IF KEY(30) THEN bat_y[0]=bat_y[0]-2
IF KEY(44) THEN bat_y[0]=bat_y[0]+2

//keys /\,\/
IF KEY (200) THEN bat_y[1]=bat_y[1]-2
IF KEY (208) THEN bat_y[1]=bat_y[1]+2

FOR num = 0 TO 1
// bat at upper/lower border?
IF bat_y[num]<0 THEN bat_y[num]=0
IF bat_y[num]>416 THEN bat_y[num]=416
NEXT

// BALL
ball_x=ball_x+ball_sx
ball_y=ball_y+ball_sy

//ball at bottom
IF ball_y>464
ball_y=464
ball_sy= -ball_sy
ENDIF

//ball at ceiling
IF ball_y<0
ball_y=0
ball_sy= -ball_sy
ENDIF

//ball left border score player 1
IF ball_x<0
score[1]=score[1]+1
ResetBall()
ENDIF

//ball right border score player 0
IF ball_x>624
score[0]=score[0]+1
ResetBall()
ENDIF

FOR num=0 TO 1
IF (ball_sx<0 AND num=0) OR (ball_sx>0 AND num=1)
col=BOXCOLL (bat_x[num],bat_y[num],16,64,ball_x,ball_y,16,16)
IF col=TRUE
//turn ball speed in x direction
ball_sx=-ball_sx
//speed up ball
ball_sx=ball_sx*1.2
ball_sy=ball_sy*1.05
ENDIF
ENDIF
NEXT
ENDFUNCTION // MOVEALL
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

fuzzy70

Quote from: erico on 2012-Jun-04
@fuzzy70 , you mean the code posted here or the help files?

I tried the one here, but  BLACKSCREEN didn´t report an error on my side.
I meant the code on the helpfile sorry erico, my bad for not being to clear on that.

My internet is down at the moment hence why I used the helpfile code as this tapatalk app on my phone does weird things with posted code, like seemingly random smilies lol.

With regards to the BLACKSCREEN command I get a "this command is depreciated use CLEARSCREEN instead" compiler error. Am currently using 10.283 of GLBasic

Lee

Sent from my GT-I5700 using Tapatalk 2
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

erico

@ Slydog, I will have to study the code a bit, I kind of fear functions, so that will help me out understanding it a bit better. I´m a veeery simple/noob coder, the kind that uses a lot of IFs for almost everything. Heck, If I made 1 cent out of every IF on my own codes I would be a millionaire by now. =D

@fuzzy70, same version here. You are right, I see the warning on the console window now. :-[

cruelcynic

Wow, lots of replys. Thanks.
@erico That local was exactly the problem I was looking for! Everything works fine now.

@Slydog I will look at those changes and see what I can learn. Thank you.

erico

Don´t forget fuzzy70´s recommendations on option-> Explicit declarations.

I now personally prefer to declare all my variables, and that was because of on old discussion on this issue on another thread.
So it is something some people go one way or the other.

fuzzy70

As a rule with my programs I have explicit declaration turned on, however if I'm grabbing something from the helpfile etc I turn it off while I work out how/why the code works.

A lot of the command examples in the helpfile have no declarations so doing the above just makes it quicker & easier for me :)

Lee

Sent from my GT-I5700 using Tapatalk 2
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)