Qbasicnews.com

Full Version: can you help me understand this?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hey everyone,
I am reading Vic's Qbasic toutorials and i am on 3.
I get how to make a pixel move with the user pressing keys like w s a d. and i get clipping but then he talks about making a ball bounce around a screen. I'll post the code and mabye you can help me understand what it means ive been re reading this for a week i just dont understand it. Heres the code he uses

-----------------------------------------
Code:
SCREEN 13
x = 50: y = 50
xadj = 1: yadj = 1
DO
press$ = INKEY$
WAIT &H3DA, 8
LINE (x - 8, y - 8)-(x + 8, y + 8), 0, BF
CIRCLE (x, y), 7
PAINT (x, y), 4, 15
IF y <= 20 THEN yadj = 1
IF y >= 180 THEN yadj = -1
IF x >= 300 THEN xadj = -1
IF x <= 20 THEN xadj = 1
x = x + xadj
y = y + yadj
LOOP UNTIL press$ = "q"

so i dont know what WAIT &H3DA, 8 does but in the tout. he says it just makes it look better. ok so the part REALLY REALLY REALLY dont get is the


Code:
IF y <= 20 THEN yadj = 1
IF y >= 180 THEN yadj = -1
IF x >= 300 THEN xadj = -1
IF x <= 20 THEN xadj = 1
x = x + xadj
y = y + yadj

it would be awsome if you could explain it thanks.
WAIT pauses the program until it receives a signal from a specified hardware port.

In the case of:
WAIT &H3DA, 8

The program waits until the monitor has finished plotting the screen.
Your program clears the screen before drawing the next frame. So for a split second the screen is blank. This is where flicker comes from. If you wait for the hardware to finish drawing the screen before clearing it, the screen is cleared and next frame is drawn before the screen is updated on the monitor.

This isn't foolproof, but it reduces flickers a bit.

EDIT: the other part

The ball has a position that is constantly chaging. To keep track of this, you have two "variables", symbols that store number.

x = 4 ' Now, whenever we refer to x, "4" is returned.

print x ' This will print '4'
y = x ' Now y has the value of x

Now, "x" and "y" in this program store the ball's horizontal and vertical position. So if you do this:

x = 50
y = 75
circle(x, y)

this will draw a circle at (50, 75).

Now, you need this ball to move. To do that, you will need to adjust x and y.

xadj, and yadj, store the velocity of the ball. For instance, when you want the ball to move 1 pixel down, 1 pixel right *every frame*,

xadj = 1
yadj = 1

and to actually move the ball

x = x + xadj
y = y + yadj

Now, the ball's position has been updated, and when the ball is drawn, it will be drawn at new coordinates.

So now that you have a moving ball, you want it to bounce when it hits a wall.

Let's say the ball hits leftmost wall. You want the ball to move in the right direction after it hits. So do you this, you first need to check where the ball is to see if it's hit the leftmost boundary of the screen. For this, we use IF

IF test THEN action

so IF y <= 20 THEN yadj = 1

This means that if the y location is less or equal to than 20 pixels from the top of the screen, then its y velocity should point down.

IF y >= 180 THEN yadj = -1

If the y position is greater than or equal to 180 pixels (20 pixels from the bottom of the screen), then yadj points up (-1).

This is repeated for the x position:
IF x >= 300 THEN xadj = -1
IF x <= 20 THEN xadj = 1

Now, every single frame in the do loop in your program will:

1) add the ball's velocity to its position, moving the ball
2) check the ball to see if it has hit a border, and change the ball's velocity if it has
3) draw the ball at the position specified.

And viola, you have yourself a bouncing ball demo.
OHHHHH. that is much more understandable thank you sooo much for your fast reply and clear explanation. this makes sense thanks agian