-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjeu.js
122 lines (117 loc) · 3.61 KB
/
jeu.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const personage = document.getElementById("personage");
let a = 0;
document.addEventListener("keydown", (event) => {
console.log(event.key);
if (parseInt(personage.style.left) + 50 < document.body.clientWidth) {
if (event.key === "ArrowRight") {
personage.style.left = parseInt(personage.style.left) + 100 + "px";
}
}
if (parseInt(personage.style.left) > 0) {
if (event.key === "ArrowLeft") {
personage.style.left = parseInt(personage.style.left) - 100 + "px";
}
}
console.log(obstacle);
perdu();
if (event.key === "ArrowUp" && a < 1) {
// setTimeout(() => {
// personage.style.top = parseInt(personage.style.top) - 100 + "px";
// perdu();
// }, 40);
// setTimeout(() => {
// personage.style.top = parseInt(personage.style.top) - 80 + "px";
// perdu();
// }, 80);
// setTimeout(() => {
// personage.style.top = parseInt(personage.style.top) - 60 + "px";
// perdu();
// }, 120);
// setTimeout(() => {
// personage.style.top = parseInt(personage.style.top) - 40 + "px";
// perdu();
// }, 160);
// setTimeout(() => {
// personage.style.top = parseInt(personage.style.top) - 20 + "px";
// perdu();
// }, 200);
// setTimeout(() => {
// personage.style.top = parseInt(personage.style.top) + 20 + "px";
// perdu();
// }, 240);
// setTimeout(() => {
// personage.style.top = parseInt(personage.style.top) + 40 + "px";
// perdu();
// }, 280);
// setTimeout(() => {
// personage.style.top = parseInt(personage.style.top) + 60 + "px";
// perdu();
// }, 320);
// setTimeout(() => {
// personage.style.top = parseInt(personage.style.top) + 80 + "px";
// perdu();
// }, 360);
// setTimeout(() => {
// personage.style.top = parseInt(personage.style.top) + 100 + "px";
// perdu();
// }, 400);
// a = a + 1;
// setTimeout(() => {
// a = a - 1;
// perdu();
// }, 500);
// }
// perdu();
saut(-100);
// a = a + 1;
// setTimeout(() => {
// a = a - 1;
// perdu();
// }, 500);
}
});
let isJumping = false;
function saut(pas) {
if (
((isJumping && pas !== -100) || (!isJumping && pas === -100)) &&
pas !== 120
) {
isJumping = true;
setTimeout(() => {
personage.style.top = parseInt(personage.style.top) + pas + "px";
perdu();
saut(pas + 20);
}, 50);
} else if (pas === 120) isJumping = false;
}
function perdu() {
let pl = parseInt(personage.style.left);
let pw = parseInt(personage.style.width);
let pt = parseInt(personage.style.top);
let ph = parseInt(personage.style.height);
const collision = obstacle.some(
({ top: et, left: el, width: ew, height: eh }) =>
(((el <= pl + pw && pl + pw <= el + ew) || (el <= pl && pl <= el + ew)) &&
((et <= pt + ph && pt + ph <= et + eh) ||
(et <= pt && pt <= et + eh))) ||
(((pl <= el + ew && el + ew <= pl + pw) || (pl <= el && el <= pl + pw)) &&
((pt <= et + eh && et + eh <= pt + ph) || (pt <= et && et <= pt + ph)))
);
if (collision === true) {
if (isJumping)
setTimeout(() => {
personage.style.left = 20 + "px";
personage.style.top = 640 + "px";
}, 560);
setTimeout(() => {
console.log("perdu");
if (isJumping)
setTimeout(() => {
personage.style.left = 20 + "px";
personage.style.top = 640 + "px";
}, 610);
personage.style.left = 20 + "px";
personage.style.top = 640 + "px";
}, 50);
}
}