Skip to content

Instantly share code, notes, and snippets.

@dursk
Created May 10, 2017
Embed
What would you like to do?
Exported from Popcode. Click to import: https://popcode.org/?gist=2785d0af77c69875c7bac41583fb3225
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div id="game">
<div id="bar"></div>
</div>
</body>
</html>
{"enabledLibraries":["jquery"]}
$("body").keydown(function(event) {
// STEP 1
// Store the current left offset of the bar
// in the position variable below.
var position;
// STEP 2
// For the left key, adjust the if statement so that
// it is only true if the position of the bar is greater than 13.
if (event.which === 37) {
$("#bar").css("left", position - 10);
// STEP 3
// For the right key, adjust the if statement so that
// is is only true if the position of the bar is less than 483.
} else if (event.which === 39) {
$("#bar").css("left", position + 10);
} else {
return;
}
});
#game {
border: 5px solid black;
height: 500px;
width: 500px;
}
#bar {
background-color: black;
height: 8px;
width: 30px;
position: absolute;
margin-top: 480px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment