-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.html
executable file
·56 lines (48 loc) · 1.29 KB
/
game.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
<!DOCTYPE html>
<html>
<head>
<title>Bouncing Bubble</title>
<style>
#bubble {
width: 50px;
height: 50px;
border-radius: 50%;
background-color: red;
position: absolute;
}
</style>
</head>
<body>
<div id="bubble"></div>
<div id="bubble"></div>
<script>
// Get the bubble element
var bubble = document.getElementById('bubble');
// Set initial position and velocity
var x = 0; // initial x position
var y = 0; // initial y position
var vx = 2; // velocity along x-axis
var vy = 2; // velocity along y-axis
// Update the position of the bubble
function updateBubblePosition() {
// Update the position
x += vx;
y += vy;
// Check if the bubble hit the edges
if (x + bubble.offsetWidth >= window.innerWidth || x <= 0) {
vx *= -1; // reverse velocity along x-axis
}
if (y + bubble.offsetHeight >= window.innerHeight || y <= 0) {
vy *= -1; // reverse velocity along y-axis
}
// Set the new position
bubble.style.left = x + 'px';
bubble.style.top = y + 'px';
// Call the updateBubblePosition function again
requestAnimationFrame(updateBubblePosition);
}
// Start the animation
updateBubblePosition();
</script>
</body>
</html>