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
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)
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")
{
<!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>
// 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
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:
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()
{
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.
<!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>
// 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.
}
}
// 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