This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if (happy) // if happy is true (remember, 1 is considered true in javascript and 0 is considered false) | |
| { // <-- then start here | |
| console.log("You are happy"); | |
| } // <-- and stop here | |
| else // else, if happy is NOT true | |
| { //then start here | |
| console.log("You are NOT happy"); | |
| } // and stop here | |
| if (happy && sad) // if happy and (&&) sad is true (will only be true if both are true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function key_pressed(key) //called when a key is pressed | |
| { | |
| // key.key is the name of the key | |
| if (key.key == "ArrowUp") | |
| { | |
| console.log("up"); | |
| } | |
| if (key.key == "ArrowLeft") | |
| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>JavaScript Game</title> | |
| </head> | |
| <body onload="init()"> | |
| <h1>JavaScript Game</h1> | |
| <p>Here is a simple JavaScript game:</p> | |
| <canvas id="c" width="400" height="400"></canvas> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // i += 1 is a shorter way of writing i = i + 1 | |
| for (i = 0; i < 5; i += 1) | |
| { // <-- the code block starts here | |
| // Console.log expects a string, not a number | |
| // so if we are going to log i we need to turn it into a string first | |
| // using the toString function of the number: | |
| let string_i = i.toString() | |
| // Now we can log i: | |
| console.log("Right now, i is " + string_i); | |
| } // <-- the code block ends here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function update() | |
| { | |
| // IF GAME DURATION SO FAR IS LESS THAN game_duration: | |
| // Compare player coordinates to dot coordinates to see whether the player is above a dot. Increment score if so. | |
| // Check distance from highest dot to top of screen. If greater than dot_distance_apart, create a new dot at top. | |
| // ELSE: | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function key_pressed(key) | |
| { | |
| if (key.key == "ArrowUp") | |
| { | |
| console.log("up"); | |
| if (start_time == 0) // Only start if not already started | |
| { | |
| start_time = new Date().getTime(); // Store start time | |
| setInterval(update, 1000 / frames_per_second); // Start looping update function | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function update() | |
| { | |
| let time_right_now = date.getTime() | |
| // If time right now minus start time is less than game time limit: | |
| if (time_right_now - start_time < game_duration) | |
| { | |
| // Compare player coordinates to dot coordinates to see whether the player is above a dot. Increment score if so. | |
| // Check distance from highest dot to top of screen. If greater than dot_distance_apart, create a new dot at top. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>JavaScript Game</title> | |
| </head> | |
| <body onload="init()"> | |
| <h1>JavaScript Game</h1> | |
| <p>Here is a simple JavaScript game:</p> | |
| <canvas id="c" width="400" height="400"></canvas> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // there is one number in dots_x for each dot, so dots_x.length is equal to the number of dots. | |
| // For every dot: | |
| for (i = 0; i < dots_x.length; i++) | |
| { | |
| // if dot's x is same as player's x and dot's y is same as player's y | |
| if (dots_x[i] == player_x && dots_y[i] == player_y) | |
| { | |
| score += 2; // Increment score by two. | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Clear the canvas and draw background: | |
| draw_background(); | |
| // there is one number in dots_x for each dot, so dots_x.length is equal to the number of dots. | |
| // For every dot: | |
| for (i = 0; i < dots_x.length; i++) | |
| { | |
| // Move dot down canvas a little: | |
| dots_y[i] += dot_pixels_per_frame; | |
| canvas.beginPath(); // Start drawing |