bbb Home |
Lesson 6 - Make Ball Bounce off Paddle |
SUMMARYIn this lesson we will add code to make the ball bounce off the paddle. DISCUSSIONIn order to determine if the ball should bounce off the paddle, we need to determine if the ball and paddle overlap. When the two objects overlap, they have collided. Open the page and test it. If you test it sufficiently you will notice that the two objects overlap when: right > left AND left < right AND bottom > top AND top < bottom The following example demonstrates collision detection in the x-direction only. Test the code by clicking any key to make the one <div> move. EXAMPLE - Collision
PRACTICE1. What is the purpose of the "30" in line 6?2. How can you modify the code so that div1 will reverse direction when it just touches div2 (without overlapping)? 3. Write code so that div2 is below div1. When the user presses a key, div1 should move downward. When div1 collides with div2, an alert should appear indicating that they have collided. GAME DEVELOPMENT
1. Modify your game page so that the ball bounces off the paddle. We
will simplify the collision algorithm by ending the game if the ball passes the
paddle. By making this game simplification, we will make the coding easier.
|
©dfx interactive, inc. Contact us at bigbadbrain@bigbadbrain.com |