-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
89 lines (77 loc) · 1.88 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import F9Launching from './assets/f9-launching.png';
import F9Landing from './assets/f9-landing.png';
import F9Landed from './assets/f9-landed.png';
import F9RUD from './assets/f9-rud.png';
let groundY = 0;
let groundHeight = 82;
let stepX = 10;
let stepY = 10;
const f9 = document.getElementById('falcon9');
const f9Images = {
launching: F9Launching,
landing: F9Landing,
landed: F9Landed,
exploding: F9RUD,
};
f9.src = f9Images.landed;
document.addEventListener('keydown', ev => {
if (ev.keyCode === 39) {
// right
goRight();
} else if (ev.keyCode === 37) {
// left
goLeft();
} else if (ev.keyCode == 38) {
// up
goUp();
} else if (ev.keyCode === 40) {
// down
goDown();
}
});
let exploded = false;
f9.addEventListener('click', () => {
exploded = true;
f9.src = f9Images.exploding;
});
const goRight = () => {
if (canGoRight() && !exploded) {
const left = parseInt(f9.style.left) || 0;
f9.style.left = left + stepX + 'px';
}
};
const goLeft = () => {
if (canGoLeft() && !exploded) {
const left = parseInt(f9.style.left) || 0;
f9.style.left = left - stepX + 'px';
}
};
const goUp = () => {
if (exploded) {
return;
}
const bottom = parseInt(f9.style.bottom) || groundY;
f9.style.bottom = bottom + stepY + 'px';
f9.src = f9Images.launching;
};
const goDown = () => {
if (!canGoDown() || exploded) {
return;
}
const bottom = parseInt(f9.style.bottom) || groundY;
f9.style.bottom = bottom - stepY + 'px';
if (parseInt(f9.style.bottom) === 0) {
f9.src = f9Images.landed;
} else {
f9.src = f9Images.landing;
}
};
const canGoRight = () => {
return (parseInt(f9.style.left) || 0) + parseInt(f9.width) + stepX <= window.innerWidth;
};
const canGoLeft = () => {
return parseInt(f9.style.left) || 0 - stepX >= 0;
};
const canGoDown = () => {
return parseInt(f9.style.bottom) || 0 - stepY >= 0;
};