This repository was archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
51 lines (50 loc) · 1.38 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
let character=document.getElementById("character")
let interval
let both=0
function moveLeft(){
let left=parseInt(window.getComputedStyle(character).getPropertyValue("left"))
if(left<380){
character.style.left=(left+2+Math.random())+"px"
}else{
character.style.left=left
}
}
function moveRight(){
let left=parseInt(window.getComputedStyle(character).getPropertyValue("left"))
if(left>0){
character.style.left=(left-2+Math.random())+"px"
}else{
character.style.left=left
}
}
function moveUp(){
let top=parseInt(window.getComputedStyle(character).getPropertyValue("top"))
if(top<480){
character.style.top=(top+2+Math.random())+"px"}
}
function moveDown(){
let top=parseInt(window.getComputedStyle(character).getPropertyValue("top"))
if(top>0){
character.style.top=(top-2+Math.random())+"px"}
}
document.addEventListener("keydown", event=>{
if(both==0){
both++
if(event.key==="ArrowLeft"){
interval=setInterval(moveLeft,1)
}
if(event.key==="ArrowRight"){
interval=setInterval(moveRight,1)
}
if(event.key==="ArrowUp"){
interval=setInterval(moveUp,1)
}
if(event.key==="ArrowDown"){
interval=setInterval(moveDown,1)
}
}
})
document.addEventListener("keyup",event=>{
clearInterval(interval)
both=0
})