## The bouncing ball challenge

Getting started:

The program uses an additional unit called console, which allows you to position the cursor on the screen, read keystrokes, clear the screen and other useful goodies. You can download it from the link below:

The program uses an additional unit called console, which allows you to position the cursor on the screen, read keystrokes, clear the screen and other useful goodies. You can download it from the link below:

console.pas | |

File Size: | 29 kb |

File Type: | pas |

Setting up:

We'll start by creating two variables, x and y. These will store the position of the ball on the screen. By changing these variables, we'll be able to make it move.

The program also needs a loop - the simplest kind of loop, a while loop that never ends. The easy way to do this is to use the line

while True do

Then we position the cursor to our x and y position using the command gotoxy and write a cross.

We'll start by creating two variables, x and y. These will store the position of the ball on the screen. By changing these variables, we'll be able to make it move.

The program also needs a loop - the simplest kind of loop, a while loop that never ends. The easy way to do this is to use the line

while True do

Then we position the cursor to our x and y position using the command gotoxy and write a cross.

uses

SysUtils,console; //make sure you have console.pas in the same folder

var

x,y :integer; //set up some variables

begin

x := 40; //set the starting values in the middle of the console

y := 12;

while True do //loop forever

begin

gotoxy(x,y); //position the cursor

write('X'); //and draw

end;

end.

That's OK, but it doesn't move. Let's make it move by changing the value of x and y

begin

x := x+1; //add one to x

y := y+1; //add one to y

gotoxy(x,y); //position the cursor

write('X'); //and draw

end;

Now we have a screen filled with crosses. It's not really clear what's happening so let's slow it down and clear the screen each time there's a loop, so we can see what's going on.

begin

clrscr; //clear screen

x := x+1; //add one to x

y := y+1; //add one to y

gotoxy(x,y); //position the cursor

write('X'); //and draw

sleep(100); //sleep for 100 milliseconds

end;

That's better. Now the cross is moving, but it gets to the bottom and just slides along. We need to make it bounce.

This requires some thinking. The screen dimensions are 80 across and 25 down. In other words, if we get to position x=80 or y=25, we need to do something to reverse the direction of travel.

The direction of travel is made by the +1 in the x := x+1 and y := y+1. To go backwards we need to make this -1.

So we need to detect whether the ball is at the edge and change the plus one to minus one for either x or y.

When we get to the other edge, where x or y is 0, then we need to change it back to plus one.

The best way seems to be to use another variable, or rather two variables to store the direction. Those variables will either be plus or minus one. We can change the value when we reach the edge.

This requires some thinking. The screen dimensions are 80 across and 25 down. In other words, if we get to position x=80 or y=25, we need to do something to reverse the direction of travel.

The direction of travel is made by the +1 in the x := x+1 and y := y+1. To go backwards we need to make this -1.

So we need to detect whether the ball is at the edge and change the plus one to minus one for either x or y.

When we get to the other edge, where x or y is 0, then we need to change it back to plus one.

The best way seems to be to use another variable, or rather two variables to store the direction. Those variables will either be plus or minus one. We can change the value when we reach the edge.

var

x,y :integer; //set up some variables

dx,dy :integer; //set variables for direction

begin

x := 40; //set the starting values in the middle of the console

y := 12;

dx := 1; //set the initial direction

dy := 1;

while true do //loop forever

begin

clrscr; //clear screen

//================================

if y > 24 then dy := -1; //bounce at the bottom

if y < 1 then dy := 1; //bounce at the top

//=================================

x := x+dx; //add dx to x

y := y+dy; //add dy to x

gotoxy(x,y); //position the cursor

write('X'); //and draw

sleep(100); //sleep for 100 milliseconds

end;

end.

This works for the y value, it will now bounce off the bottom and top of the screen. Can you do the same for the x values?