Skip to content

Instantly share code, notes, and snippets.

@dursk
Created May 11, 2017
Embed
What would you like to do?
Exported from Popcode. Click to import: https://popcode.org/?gist=26c83be9f6ed0601547b28f1d6ede534
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div id="game">
<div id="bar"></div>
</div>
</body>
</html>
{"enabledLibraries":["jquery"],"hiddenUIComponents":["editor.html"]}
$("body").keydown(function(event) {
var position = $("#bar").offset().left;
if (event.which === 37 && position > 13) {
$("#bar").css("left", position - 10);
} else if (event.which === 39 && position < 483) {
$("#bar").css("left", position + 10);
} else {
return;
}
});
// STEP 1
// Initialize a variable called currentBlockIds
// and set it to an empty array.
var currentBlockIds;
function generateRandomId() {
return "A" + Math.floor(Math.random() * 10000);
}
function generateRandomOffset() {
return Math.floor(Math.random() * 480);
}
function createBlock() {
// STEP 2
// Use the generateRandomId function to set the id variable
// to what the function returns.
var randomId;
// STEP 3
// Use the generateRandomOffset function to set the leftOffset
// variable to what the function returns.
var randomOffset;
$("#game").append("<div id=" + randomId + " class=block style=left:" + randomOffset + "px></div>");
currentBlockIds.push(randomId);
}
// STEP 4
// Uncomment the code below to see what you've done so far!
//window.setInterval(createBlock, 1200);
#game {
border: 5px solid black;
height: 500px;
width: 500px;
}
#bar {
background-color: black;
height: 8px;
width: 30px;
position: absolute;
margin-top: 480px;
}
.block {
background-color: red;
height: 10px;
width: 10px;
position: absolute;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment