-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.html
157 lines (142 loc) · 5.15 KB
/
serve.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tennis Ball Game</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden;
font-family: Arial, sans-serif;
}
#game {
position: relative;
background-image: url('img/tennis-court.jpg');
width: 100%;
height: 100vh;
background-size: cover;
background-position: center;
}
.control-buttons {
position: absolute;
top: 10px;
left: 10px;
display: flex;
gap: 10px;
}
button {
background-color: #ec600f;
color: white;
border: none;
margin: 30px 0 0 30px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #31bf38;
}
</style>
</head>
<body>
<div id="game">
<div class="control-buttons">
<button onclick="makeOne()">Serve a Ball</button>
<button onclick="removeOne()">Remove a Ball</button>
</div>
</div>
<script>
const ballsArray = ['tennis-ball-1.png', 'tennis-ball-2.png', 'tennis-ball-3.png', 'tennis-ball-4.png'];
const tennisBalls = [];
const originalSize = 50; // Original ball size
function setToRandom(scale) {
return {
x: Math.random() * scale,
y: Math.random() * scale
};
}
function makeBall() {
let velocity = setToRandom(18);
let position = setToRandom(window.innerWidth / 2);
let game = document.getElementById('game');
let newimg = document.createElement('img');
newimg.style.position = 'absolute';
newimg.src = ballsArray[0]; // Start with the first image
newimg.width = originalSize;
newimg.style.left = position.x + 'px';
newimg.style.top = position.y + 'px';
game.appendChild(newimg);
return {
position,
velocity,
newimg
};
}
function update() {
tennisBalls.forEach((item) => {
checkCollisions(item);
item.position.x += item.velocity.x;
item.position.y += item.velocity.y;
item.newimg.style.left = item.position.x + 'px';
item.newimg.style.top = item.position.y + 'px';
});
requestAnimationFrame(update);
}
function checkCollisions(item) {
const squeezeFactor = 0.9;
const bounceBackTime = 150; // Milliseconds
if (item.position.x + item.velocity.x + item.newimg.width > window.innerWidth) {
// Hits right wall
item.velocity.x = -item.velocity.x;
item.newimg.src = ballsArray[1]; // Change to image 2
squeezeBall(item.newimg, 'horizontal', squeezeFactor, bounceBackTime);
}
if (item.position.x + item.velocity.x < 0) {
// Hits left wall
item.velocity.x = -item.velocity.x;
item.newimg.src = ballsArray[2]; // Change to image 3
squeezeBall(item.newimg, 'horizontal', squeezeFactor, bounceBackTime);
}
if (item.position.y + item.velocity.y + item.newimg.height > window.innerHeight) {
// Hits bottom wall
item.velocity.y = -item.velocity.y;
item.newimg.src = ballsArray[3]; // Change to image 4
squeezeBall(item.newimg, 'vertical', squeezeFactor, bounceBackTime);
}
if (item.position.y + item.velocity.y < 0) {
// Hits top wall
item.velocity.y = -item.velocity.y;
item.newimg.src = ballsArray[0]; // Change back to image 1
squeezeBall(item.newimg, 'vertical', squeezeFactor, bounceBackTime);
}
}
function squeezeBall(ball, direction, factor, time) {
if (direction === 'horizontal') {
ball.style.width = ball.width * factor + 'px';
ball.style.height = ball.height / factor + 'px';
} else {
ball.style.width = ball.width / factor + 'px';
ball.style.height = ball.height * factor + 'px';
}
setTimeout(() => {
ball.style.width = originalSize + 'px';
ball.style.height = originalSize + 'px';
}, time);
}
function makeOne() {
tennisBalls.push(makeBall());
}
function removeOne() {
if (tennisBalls.length > 0) {
let ballToRemove = tennisBalls.pop();
ballToRemove.newimg.remove();
}
}
update(); // Start the ball movement
</script>
</body>
</html>