-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgame.html
More file actions
111 lines (104 loc) · 2.7 KB
/
game.html
File metadata and controls
111 lines (104 loc) · 2.7 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Dino Mini Game</title>
<style>
body {
background: #f4f4f4;
font-family: sans-serif;
overflow: hidden;
margin: 0;
text-align: center;
}
.game {
position: relative;
width: 100%;
height: 300px;
background: #fff;
border-bottom: 3px solid #333;
overflow: hidden;
}
#dino {
width: 60px;
height: 60px;
background: url('https://files.catbox.moe/5fesaz.webp') no-repeat center;
background-size: contain;
position: absolute;
bottom: 0;
left: 50px;
}
.obstacle {
width: 30px;
height: 50px;
background: url('https://files.catbox.moe/n48qeh.jpg') no-repeat center;
background-size: contain;
position: absolute;
bottom: 0;
right: -40px;
animation: move 2s linear infinite;
}
@keyframes move {
0% { right: -40px; }
100% { right: 100%; }
}
.jump {
animation: jump 0.5s ease-out;
}
@keyframes jump {
0% { bottom: 0; }
50% { bottom: 100px; }
100% { bottom: 0; }
}
button {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
background: #2196f3;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background: #1976d2;
}
</style>
</head>
<body>
<div class="game">
<div id="dino"></div>
<div class="obstacle" id="obstacle"></div>
</div>
<button onclick="jump()">Lompat</button>
<script>
const dino = document.getElementById("dino");
document.addEventListener("keydown", function (event) {
if (event.code === "Space" || event.code === "ArrowUp") {
jump();
}
});
function jump() {
if (!dino.classList.contains("jump")) {
dino.classList.add("jump");
setTimeout(() => {
dino.classList.remove("jump");
}, 500);
}
}
setInterval(() => {
const dinoBottom = parseInt(window.getComputedStyle(dino).getPropertyValue("bottom"));
const obstacle = document.getElementById("obstacle");
const obsRight = parseInt(window.getComputedStyle(obstacle).getPropertyValue("right"));
const obsWidth = parseInt(window.getComputedStyle(obstacle).getPropertyValue("width"));
const gameWidth = document.querySelector(".game").offsetWidth;
const obsLeft = gameWidth - obsRight - obsWidth;
if (obsLeft >= 50 && obsLeft <= 90 && dinoBottom <= 40) {
alert("Game Over!");
location.reload();
}
}, 10);
</script>
</body>
</html>