bigbadbrain logo
bbb Home
  Lesson 5 - Make Ball Bounce off Walls
 

SUMMARY

In this lesson we will add code to randomly set the direction of the ball when the game starts.  We will also add code so that the ball bounces off the walls.  We will use concepts learned in previous lessons.

DISCUSSION

When the game begins, the ball starts moving from a random location. To make the game more interesting we should make the ball move in a random direction as well. To keep our code simple, we will have the ball start moving downward and either left or right so that the ball moves diagonally down across the screen.  

If we want the ball to move to the right, we add to the position. For example, if we want the ball to move by 10 pixels our statement is:

    x = x + 10

If we want the ball to move to the left 10 pixels, then our statement is:

    x = x - 10

Notice that the equations are identical with the exception of the + or - operator.  We can write one equation to handle both situations by introducing another variable to keep track of the direction:

    x = x + dirX * 10

In this equation, the variable dirX will be set to 1 to move the ball to the right and to -1 to move the ball to the left. 

This concept is illustrated in the example below.

Now we will discuss the code needed to make the ball bounce off the walls.  Assume an object is moving to the left. We keep moving the object to the left as long as its position is to the right of the wall.  When the object's position equals 0, the location of the left wall, we reverse its direction by changing the sign of the direction variable:

    if(px <=0) dirX = -dirX

We can apply the same concept to the other walls.  The example below illustrates these concepts.

EXAMPLE - Reverse Direction

 Font Size: 10pt 12pt   View Large Version
 Ctrl-C to Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

PRACTICE

1.  Change the above code so that the <div> will start moving to the right approximately 75% of the time.  
 
2.  Modify the code above so that the <div> moves in the vertical direction instead of the horizontal direction. The <div> should bounce between the top of the screen and 200 pixels down the screen.

3.  Create a page with a <div> and a variable called x.  Initialize x to 10. Everytime the user clicks the page, add 1 to x and display x in the <div>.  When x gets to 15, start subtracting 1 from x everytime the user clicks the page.  When x gets to 10, start adding 1 to x everytime the user clicks the page.  The value of x should "bounce" between 10 and 15 indefinitely.

4. Create a page with a small image. When the user clicks on the image , it should move either left, right, up, or down until it reaches a boundary.  You can use the screen as the boundary or locations on the page. 

GAME DEVELOPMENT

1.  Modify your game page so that the ball starts moving diagonally down towards the left or right when the user begins the game.

2. Modify your game so that the ball bounces off the walls. In the next lesson we will learn to make the ball bounce off the paddle.
©dfx interactive, inc. Contact us at bigbadbrain@bigbadbrain.com