-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bffd592
commit 934dc6a
Showing
5 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Sonic Speed</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
|
||
<body> | ||
<h1>Press an arrow key to move Sonic</h1> | ||
<img id="sonic-running" src="./assets/sonic-running.gif"> | ||
|
||
<script src="./script.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Select sonicGif in DOM so we can do things to it with javascript | ||
const stepSize = 100; | ||
const sonicGif = document.getElementById('sonic-running'); | ||
|
||
// Create functions which move Sonic around page using margins | ||
function moveSonicDown() { | ||
sonicGif.style.marginTop = (parseInt(window.getComputedStyle(sonicGif).marginTop) + stepSize) + 'px'; | ||
} | ||
|
||
function moveSonicUp() { | ||
sonicGif.style.marginTop = (parseInt(window.getComputedStyle(sonicGif).marginTop) - stepSize) + 'px'; | ||
} | ||
|
||
function moveSonicRight() { | ||
sonicGif.style.marginLeft = (parseInt(window.getComputedStyle(sonicGif).marginLeft) + stepSize) + 'px'; | ||
} | ||
|
||
function moveSonicLeft() { | ||
sonicGif.style.marginLeft = (parseInt(window.getComputedStyle(sonicGif).marginLeft) - stepSize) + 'px'; | ||
} | ||
|
||
// Add key press event listener. Only do things for arrow keys | ||
document.onkeydown = function(e) { | ||
if (e.key === 'ArrowDown') { | ||
moveSonicDown(); | ||
return; // Once we've found our key let's exit the function with return | ||
} | ||
if (e.key === 'ArrowUp') { | ||
moveSonicUp(); | ||
return; | ||
} | ||
if (e.key === 'ArrowRight') { | ||
moveSonicRight(); | ||
return; | ||
} | ||
if (e.key === 'ArrowLeft') { | ||
moveSonicLeft(); | ||
return; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#sonic-running { | ||
height: 100px; | ||
aspect-ratio: auto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters