Tutorial: How to write games part 2

Go to page << 1 2 3 4 5 6 7 8 9 10 11 >>

Putting it together

That was quite a bit of theory, and not much about space aliens. It must be about time to put it to use on something practical.

But before going back to the space alien game though, we're going to start with a simple bouncing ball program.
This is because we want to demonstrate the inbetween steps for converting a one-object program into a many-object program, and the space alien game has become rather large. We want something smaller so we can show each little step without making the tutorial longer than it already is!

1 bouncing ball

Okay, so firstup we need a bouncing ball program with one ball.
Because we're still using text mode, we will simply draw a letter 'o' for the ball. The ball will simply move diagonally around the screen. Whenever it reaches the edge of the screen it will bounce away.

As before, we need to decide what data the computer needs to store to remember where the ball is, and keep it moving.

We need to store the ball's current position on the screen. So we will use ballx and bally as the current column and row.
Now at any one time, the ball will be moving in a direction, so we need to store that too.
We will use two variables called ballxd and ballyd, and will store the horizontal (left and right) and vertical (up and down) directions of the ball, which we will store as a number. Each time around the main loop we will update the ball's position like this:

ballx = ballx + ballxd
bally = bally + ballyd

So when ballx is 1, it will be the same as doing a ballx = ballx + 1 and the ball will move to the right. And when ballx is -1, it will be doing the same as ballx = ballx + -1 (in other words ballx = ballx - 1) and the ball will move to the left.
Likewise with ballyd. 1 will make the ball move down, and -1 will make it move up.

Here's the first version with 1 ball.

dim ballx, bally
dim ballxd, ballyd

ballx = rnd () % 38 + 1
bally = rnd () % 23 + 1
ballxd = 1: ballyd = 1

TextMode (TEXT_BUFFERED)
SetTextScroll (false)

while true
    ballx = ballx + ballxd
    bally = bally + ballyd
    if ballx <= 0 or ballx >= 39 then 
        ballxd = -ballxd
    endif
    if bally <= 0 or bally >= 24 then
        ballyd = -ballyd
    endif

    cls
    locate ballx, bally: print "o"
    DrawText ()
    
    Sleep (75)
wend

Most of this should look familiar from the space aliens game. We have declared our variables with dim. Then we've stored some initial data in them, using the rnd () function and the remainder operator (%) to generate numbers from 1-38 and 1-23 for the column and row screen position. This makes the ball start from a random position.

In the main loop, we move the ball as we said we would.
The only tricky bit is the if..then instructions:

if ballx <= 0 or ballx >= 39 then

detects when the ball has reached the left hand side of the screen (column 0) or the right hand side (column 39).
If so we reverse the left-right direction with:

ballxd = -ballxd

So if ballxd was 1 (ball moving right), it will be changed to -1 (ball moving left).
And if ballxd was -1 (ball moving left), it will be changed to 1 (ball moving right).

And the ball's up-down direction is handled in the same way.

The rest of the code simply draws the ball on the screen, and delays for 0.075 seconds, to regulate the speed.

Go to page << 1 2 3 4 5 6 7 8 9 10 11 >>