Hi again,
Now before we stop the ball from moving off screen we need to understand why it is moving in the first place.
We put the ball at 100,100 then we created variables (ball_x and ball_y) to represent these values. We then added.....
ball_x#=ball_x#+0.75
ball_y#=ball_y#+0.75
to our code. This is adding 0.75 to the balls x position and 0.75 to the balls y position every loop.
If we want to make our ball change direction we need to change the speed from 0.75 to -0.75, of course, if we want to change it we will need to swap it for a variable, add.....
ball_x_speed#=0.75
ball_y_speed#=0.75
to the top of your code, then change.....
ball_x#=ball_x#+0.75
ball_y#=ball_y#+0.75
to
ball_x#=ball_x#+ball_x_speed#
ball_y#=ball_y#+ball_y_speed#
ball_x#=ball_x#+ball_x_speed#, ball_y#=ball_y#+ball_y_speed#
Here we've just done a simple swap of values, we have kept exactly the same co-ordinates as before but we can now change the value associated with the balls direction.
We now need to tell the computer that IF the ball hits the wall we want it to change its direction, so we use an IF command, add......
if sprite hit (6,4) then ball_y_speed#=-0.75
to your code.
if sprite hit (6,4) then ball_y_speed#=-0.75
We are now saying that IF two sprites (numbers 6 and 4) hit each other, we want to change the ball_y_speed# from 0.75 to -0.75, so we are actually taking away 0.75 from the balls y position, which makes it appear to bounce off the wall.
Your complete code should now look something like this.....
set display mode 800,600,32
backdrop on
color backdrop rgb(0,0,0)
player_x=50
player_y=240
ball_x#=100
ball_y#=100
ball_x_speed#=0.75
ball_y_speed#=0.75
load image "player_bat.bmp",1
sprite 1,player_x,player_y,1
load image "enemy_bat.bmp",2
sprite 2,750,240,2
load image "horizontal_wall_top.bmp",3
sprite 3,0,30,3
load image "horizontal_wall_bottom.bmp",4
sprite 4,0,570,4
load image "vertical_wall.bmp",5
sprite 5,400,30,5
load image "ball.bmp",6
sprite 6,ball_x#,ball_y#,6
rem start loop
do
ball_x#=ball_x#+ball_x_speed#
ball_y#=ball_y#+ball_y_speed#
rem move bat up when upkey is pressed
if upkey()=1 and player_y>0 then player_y=player_y-1
rem move bat down when downkey is pressed
if downkey()=1 and player_y<580 then player_y=player_y+1
if sprite hit (6,4) then ball_y_speed#=-0.75
rem remind computer that player_y belongs to sprite 1
sprite 1,player_x,player_y,1
sprite 6,ball_x#,ball_y#,6
rem end loop
loop
If you run your code you should see the ball bounce off the bottom wall, I'll post again later telling you how to make it bounce off the others, any problems, just say.
Formely code2kill
To tell you the truth, I'm just glad I DON'T enjoy playng The Sims.
the
play2kill fan club