This file contains 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() | |
{ | |
// Set current time (milliseconds): | |
let time_right_now = new Date().getTime() | |
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 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
// Draw score: | |
canvas.fillStyle = text_color; // set color | |
canvas.font = "20px Arial"; // set font and font size | |
canvas.fillText(score, 20, 20); // draw text |
This file contains 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
// Ensure that player is within canvas: | |
player_x = Math.max(0, Math.min(player_x, canvas_element.width)) | |
// Draw player: | |
canvas.beginPath(); // Start drawing | |
canvas.arc(player_x, player_y, player_radius, 0, 2*Math.PI); // Draw 360 degree arc (full circle) | |
canvas.fillStyle = player_color; // Set color | |
canvas.fill(); // Fill in circle | |
canvas.closePath(); // End drawing |
This file contains 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 |
This file contains 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 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 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 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 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 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 |