Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / simple pong example from hands on dark basic volume 1

Author
Message
dumb newbie
11
Years of Service
User Offline
Joined: 2nd Mar 2013
Location:
Posted: 12th Mar 2013 00:10
Hi guys
Sorry new to this, so I hope I'm not wasting time.
With the simple pong example from the book Hands on Dark Basic,I have some issues. Have checked the forum & while information is given regarding the problems with collision detection. None of this has really helped.
My problem is also with collision detection but not from the start up.
Ball works fine from initial start up but when collision detected between ball and paddle, I get the same result as problem posted. The ball no longer moves the horizontal axis, but moves only the vertical axis.
Hope someone can help.
Here is the code. Written mainly from memory so may look a little funky.

rem pong game program
Rem By Ross Cooper
Rem March-11-2013
// Make variables for players and ball
ballx as integer=screen width()/2
bally as integer=screen height()/2
speedx as integer= -2
speedy as integer=1
px1 as integer=40
px2 as integer=760
py1 as integer=280
py2 as integer=280
score1 as integer=0
score2 as integer=0

// slow down game speed
sync on
sync rate 60

rem main game loop
do
cls 0
`check if control key moves player 1 paddle up
if shiftkey()
dec py1,2
if py1<0 then py1=0
endif
`check if control key moves player 1 paddle down
if controlkey()
inc py1,2
if py1>600-40 then py1=600-40
endif
`check if up key moves player 2 paddle up
if upkey()
dec py2,2
if py2<0 then py2=0
endif
`check if down key moves player 2 paddle down
if downkey()
inc py2,2
if py2>600-40 then py2=600-40
endif
`move the ball
inc ballx,speedx
inc bally,speedy

`bounce ball off top or bottom of screen
if bally<0 or bally>600 then speedy=speedy*-1

`see if player 1 misses the ball
`if so increase player 2 score by 1
if ballx<0
inc score2
ballx=400:bally=300:speedx=2
sleep 100
endif

`see if player 2 misses the ball
`if so increase player score by 1
if ballx>800
inc score1
ballx=400:bally=300:speedx=-2
sleep 100
endif

`draw the ball
circle ballx,bally,10

`draw the paddles
box px1,py1,px1+10,py1+40
box px2,py2,px2+10,py2+40

`see if the ball has hit the paddle
if point(ballx+10,bally+10)>0
speedx=speed*-2
endif

`print the scores
text 0,0,"Score"+str$(score1)
text 750,0,"Score"+str$(score2)

// Draw the screen
sync
loop
luskos
16
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 12th Mar 2013 10:17 Edited at: 12th Mar 2013 10:22
This line: speedx=speed*-2 becomes speedx=speedx*-2 and use "
", just hit code button twice and put your code in the middle.

Coding is My Kung Fu!
And My Kung Fu is better than Yours!
Transcendent
11
Years of Service
User Offline
Joined: 26th Mar 2013
Location:
Posted: 26th Mar 2013 03:30
I can already see a problem in the collision checking.

Holy cow, what was he thinking.



Should be changed to the following:



Since the paddles are white, it will find that the point of ballx/bally is white, and therefore a collision with the ball will occur with the paddle.
luskos
16
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 27th Mar 2013 10:54
Actually this:


Is this:


Coding is My Kung Fu!
And My Kung Fu is better than Yours!

Login to post a reply

Server time is: 2024-04-19 04:01:48
Your offset time is: 2024-04-19 04:01:48