-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
73 lines (53 loc) · 1.62 KB
/
app.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function isTouching(a, b) {
const aRect = a.getBoundingClientRect();
const bRect = b.getBoundingClientRect();
return !(
aRect.top + aRect.height < bRect.top ||
aRect.top > bRect.top + bRect.height ||
aRect.left + aRect.width < bRect.left ||
aRect.left > bRect.left + bRect.width
);
}
const avatar = document.querySelector("#player");
const coin = document.querySelector('#coin');
const score = document.querySelector('#score');
window.addEventListener("keyup", function(e){
if(e.key === "ArrowDown" || e.key === "Down"){
moveVertical(avatar , 50);
}
else if(e.key === "ArrowUp" || e.key === "Up"){
moveVertical(avatar , -50);
}
else if(e.key === "ArrowRight" || e.key === "Right"){
moveHorizontal(avatar ,50);
avatar.style.transform = 'scale(1,1)'
}
else if(e.key === "ArrowLeft" || e.key === "Left"){
moveHorizontal(avatar , -50);
avatar.style.transform = 'scale(-1,1)'
}
if(isTouching(avatar,coin)){
moveCoin();
let scoreNum = parseInt(score.innerText) + 1;
score.innerText = `${scoreNum}`;
}
});
const moveVertical = (element , amount) => {
const currTop = extractPos(avatar.style.top);
avatar.style.top =`${currTop + amount}px`;
};
const moveHorizontal = (element , amount) => {
const currLeft = extractPos(avatar.style.left);
avatar.style.left =`${currLeft + amount}px`;
};
const extractPos = (pos) => {
if (!pos) return 100;
return parseInt(pos.slice(0,-2));
};
const moveCoin = () => {
const x = Math.floor(Math.random() * window.innerWidth);
const y = Math.floor(Math.random() * window.innerHeight);
coin.style.top = `${y}px`;
coin.style.left = `${x}px`;
};
moveCoin();