Help with Bounce

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
samnicole
Newbie
Posts: 1
Joined: Fri Dec 14, 2007 10:43 pm

Help with Bounce

Post by samnicole »

So I am making a game and I need to figure out how to get my ball to bounce off the walls. My wall stops at 450 and I am in screen 9. Any help would be appreciated. I had bx = bx + 5 for the speed and had an if then statement.

if bx <=450 then
bx = bx - 5

and it stops the ball , but i can't quite figure out the code for the bounce.
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

One way to do it is to have a separate variable that will be used for incrementing the ball's position.

Code: Select all

DIM balldirx AS INTEGER
balldirx = 5
... code here ...
bx = bx + balldirx
IF bx> 449 OR bx < 1 THEN balldirx = -balldirx
Essentially, you are using this second variable to help move the ball and also to control its direction...when you use the -balldirx what you're doing is reversing its sign, so if it was 5 it would become -5 and vice versa.

Make sense? :D
Last edited by Nodtveidt on Fri Dec 14, 2007 11:10 pm, edited 1 time in total.
User avatar
coma8coma1
Veteran
Posts: 100
Joined: Sat Dec 08, 2007 5:29 pm
Location: Maryland, USA

Post by coma8coma1 »

i would imagine you could have a horizontal velocity variable. like lets say it's "xvel"

it starts out being 5 and then when the conditions are right it gets set to -5

then each frame you just have bx = bx + xvel and update the screen
User avatar
coma8coma1
Veteran
Posts: 100
Joined: Sat Dec 08, 2007 5:29 pm
Location: Maryland, USA

Post by coma8coma1 »

yeah like nodtveit said lol ;) :oops:
Post Reply