Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<!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>
<script>
console.log("Starting the script!");
let canvas_element = document.getElementById("c");
let canvas = canvas_element.getContext("2d");
document.onkeydown = key_pressed;
function init()
{
draw_background();
canvas.fillStyle = "#FFFFFF";
canvas.font = "25px Arial";
canvas.fillText("Press the up arrow to start", 40, 200);
}
function draw_background()
{
canvas.clearRect(0, 0, canvas_element.width, canvas_element.height);
canvas.fillStyle = "#1EBA00";
canvas.fillRect(0, 0, canvas_element.width, canvas_element.height);
}
function key_pressed(key)
{
if (key.key == "ArrowUp")
{
console.log("up");
}
if (key.key == "ArrowLeft")
{
console.log("left");
}
if (key.key == "ArrowRight")
{
console.log("right");
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.