bbb Home |
Lesson 3 - Start Game |
SUMMARYIn this lesson we will add code to make the game start when the user presses any key other than the left or right arrow key. DISCUSSIONIn previous lessons, you learned to write an if...else statement to check if a certain condition is true or not. Sometimes we want to check if a condition is one of multiple possibilities. For example, we might want to check if the user clicks the "a" key, or the "b" key, or the "c" key, or any other key. We can use an if...else if...else if...else statement. These statements are written as follows:
if(condition){ EXAMPLE - if...else if...else
PRACTICE1. Write code that prompts the user for a number. Store this number in a variable. Then use an if...else if...else statement to display a message if the number is negative, positive, or 0. Remember to use code similar to var num = parseInt(prompt("Enter a number"))to get the number. 2. Write code that calls a function when the user presses a key. The function should:
GAME DEVELOPMENT
1. Ensure that the message in the <div> on your Pong page is similar to the
message shown in the figure on the Home page. The message should be similar
to: "Click any key to start. Press the arrow keys to control the paddle".
2. Modify your game page so that the game starts when the user presses any key besides the left or right arrow keys. At the moment, nothing much will happen when the game starts except that the message on the screen will change to display "Score: 0". In future lessons we will make the ball move. You should:
|
©dfx interactive, inc. Contact us at bigbadbrain@bigbadbrain.com |