-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatch the falling blocks!
72 lines (67 loc) · 2.16 KB
/
Catch the falling blocks!
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Catch the Falling Objects</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
#game-area {
width: 300px;
height: 400px;
border: 2px solid #333;
position: relative;
}
.falling-object {
width: 30px;
height: 30px;
background-color: red;
position: absolute;
top: 0;
left: 0;
border-radius: 50%;
}
</style>
</head>
<body>
<div id="game-area">
</div>
<script>
// Function to generate a random number within a range
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Function to create falling objects
function createFallingObject() {
const object = document.createElement('div');
object.classList.add('falling-object');
object.style.left = random(0, 270) + 'px'; // Random position within game area width
document.getElementById('game-area').appendChild(object);
// Set interval to move the falling object
const fallInterval = setInterval(() => {
const currentTop = parseInt(object.style.top);
if (currentTop >= 370) { // If object reaches bottom
clearInterval(fallInterval);
object.remove();
} else {
object.style.top = (currentTop + 5) + 'px'; // Move object down
}
}, 50);
}
// Function to start the game
function startGame() {
setInterval(createFallingObject, 2000); // Create falling objects every 2 seconds
}
// Call startGame function when the page loads
window.onload = startGame;
</script>
</body>
</html>