As the ball moves around, it's X and Y screen position are stored in BallposX and BallposY.
The Y co-ordinates run from 0 at the top to 600 at the bottom (assuming you are in 800x600 screen mode).
To bounce of the top wall, all that is needed is a check to see if the ball's Y position (BallposY) tries to get below a certain value. In your program, you are checking to see if it goes less than 5.
At this point, you need to tell the ball to start moving down and keep moving down
until told otherwise. Yours only tells it to move down once - at the moment it gets lower than 5.
The direction the ball moves is stored in BallDirY - initially set to 2 which makes it move down. You'll notice I've changed the stating value to -2 so it initially moves up and hits the top wall first.
What you therefore need to do is alter the value of
BallDirY when you get below 5. (The values used for the right and bottom wall depend on the width and height of your sprite).
If a value of BallDirY=2 makes it move up, then to make it move down you set BallDirY to -2 (minus 2). To negate any value in a variable you use
VariableName = 0-Variablename which in your program gives you:
If BallposY<5
BallposY=0-BallposY
Endif
Using this method, the exact same line will work for reversing the Y direction at both the top and bottom of the screen. You also do not have to alter the line
BallposY=BallposY+BallDirY
as that still works regardless of whether BallDirY is 2 or -2. (BTW you could have used
Inc BallposY,BallDirY instead).
Here's your program modified so you can work on it:
`This Pong Was Made By Robert Lee
`With Major Help From TDK And His Tutorials
`It is also my first full game
`(If i ever complete it!)
Set Display Mode 800,600,16
sync on: Sync Rate 60: CLS 0
`Variables
BallposX=500
BallposY=300
BallDirX=-2
BallDirY=-2
`Load images
Load image "pongball.bmp",1
Load image "bat.bmp",2
`sprites
Sprite 1,BallposX,BallposY,1
`Start Main loop
do
`renew variables
Inc BallposX,BallDirX
Inc BallposY,BallDirY
`move sprite
Sprite 1,BallposX,BallposY,1
`bounce on walls
if BallposY<5
BallDirY=0-BallDirY
endif
If BallposY>580
BallDirY=0-BallDirY
endif
If BallposX<5
BallDirX=0-BallDirX
Endif
If BallposX>780
BallDirX=0-BallDirX
endif
`End loop
sync
loop
TDK_Man