Skip to content

Instantly share code, notes, and snippets.

View bashbaugh's full-sized avatar
🐟
writing cod

Benjamin Ashbaugh bashbaugh

🐟
writing cod
View GitHub Profile
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.
// Draw score:
canvas.fillStyle = text_color; // set color
canvas.font = "20px Arial"; // set font and font size
canvas.fillText(score, 20, 20); // draw text
// 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
// 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
// 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.
}
}
<!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>
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.
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
}
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:
// 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