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
// 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
<!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 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")
{
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)
<!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 init()
{
// Draw the background using the function we just created:
draw_background();
// Set the brush color to white:
canvas.fillStyle = "#FFFFFF";
// Set the font size and name:
canvas.font = "25px Arial";
// Draw the text onto the canvas:
canvas.fillText("Press the up arrow to start", 40, 200);
function draw_background()
{
// First we erase everything on the canvas:
canvas.clearRect(0, 0, canvas_element.width, canvas_element.height);
// And then we change the color of our "paintbrush":
canvas.fillStyle = "#1EBA00";
// And then we draw a colored rectangle that covers the entire canvas:
canvas.fillRect(0, 0, canvas_element.width, canvas_element.height);
}
<!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>
// Here are two name variables:
let some_first_name = "Bob";
let some_last_name = "Joe";
// Here we use them in the name printer function:
print_full_name(some_first_name, some_last_name);
// Or else we can change the first name:
print_full_name("Margaret", some_last_name);
// Or else we can make up both names:
print_full_name("Jane", "Smith");
// Remember, this is a comment because it is after two slashes.
// The web browser ignores comments - they are just for the humans.
function print_full_name(first_name, last_name) // Here we declare the function name and arguments
{ // <-- This starts the function
console.log("Here is a full name:"); // This line is run first when the function is called
// And then this line is run,
// which pieces together the first_name argument, a space, and the last_name argument to create a full name:
console.log(first_name + " " + last_name); // Which gets printed to the console.
} // <-- And then this tells the browser that the function is finished.