-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJumpGame.html
135 lines (122 loc) · 3.73 KB
/
JumpGame.html
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
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html lang="en" onclick="jump()" onkeydown="checkKey(event)">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background-color: #f0f0f0;
}
* {
padding: 0;
margin: 0;
}
#game {
width: 500px;
height: 200px;
border: 1px solid black;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#character {
width: 20px;
height: 50px;
position: relative;
top: 150px;
background-color: blue;
}
.animate {
animation: jump 500ms;
}
#block {
width: 20px;
height: 20px;
background-color: black;
position: relative;
top: 130px;
left: 580px;
animation: block 1.5s infinite linear;
}
#point {
font-size: 24px;
text-align: center;
margin-bottom: 20px;
}
button {
display: block;
margin: 0 auto 20px auto;
font-size: 24px;
padding: 10px 20px;
}
@keyframes jump {
0% { top: 150px; }
30% { top: 100px; }
70% { top: 100px; }
100% { top: 150px; }
}
@keyframes block {
0% { left: 480px; }
100% { left: -40px; }
}
</style>
<title>Basic Game</title>
</head>
<body>
<div id="game">
<div id="character"> </div>
<div id="block"> </div>
</div>
<button onclick="togglePause()">Pause</button>
<div id="point"><p>Points: 0</p></div>
<script>
var character = document.getElementById("character");
var block = document.getElementById("block");
var point = document.getElementById("point").getElementsByTagName("p")[0];
var points = 0;
var isPaused = false;
function jump() {
if (!isPaused && character.classList != "animate") {
character.classList.add("animate");
}
setTimeout(function() {
character.classList.remove("animate");
}, 500);
}
function checkKey(event) {
if (event.keyCode === 9) { // Tab key
jump();
event.preventDefault();
} else if (event.keyCode === 80) { // 'P' key to pause
togglePause();
}
}
function togglePause() {
isPaused = !isPaused;
if (isPaused) {
block.style.animationPlayState = "paused";
} else {
block.style.animationPlayState = "running";
}
}
setInterval(function() {
if (!isPaused) {
var characterTop = parseInt(window.getComputedStyle(character).getPropertyValue("top"));
var blockLeft = parseInt(window.getComputedStyle(block).getPropertyValue("left"));
if (blockLeft < 20 && blockLeft > 0 && characterTop >= 130) {
alert("Game Over");
character.style.display = "none";
block.style.display = "none";
setTimeout(function() {
location.reload();
}, 2000);
} else if (blockLeft < 20 && blockLeft > 0 && characterTop < 130) {
points += 5;
point.textContent = "Points: " + points;
}
}
}, 10);
</script>
</body>
</html>