-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript1.js
193 lines (158 loc) · 5.49 KB
/
script1.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// document.addEventListener('DOMContentLoaded', () => {
// const basket = document.getElementById('basket');
// // Function to move the basket left and right
// function moveBasket(event) {
// const basketLeft = parseInt(getComputedStyle(basket).left);
// const basketWidth = basket.offsetWidth;
// const windowWidth = window.innerWidth;
// if (event.key === 'ArrowLeft' || event.key === 'a') {
// if (basketLeft > 0) {
// basket.style.left = (basketLeft - 10) + 'px';
// }
// } else if (event.key === 'ArrowRight' || event.key === 'd') {
// if (basketLeft < windowWidth - basketWidth) {
// basket.style.left = (basketLeft + 10) + 'px';
// }
// }
// }
// // Event listener for keyboard input
// document.addEventListener('keydown', moveBasket);
// });
document.addEventListener("DOMContentLoaded", () => {
var cookieDisplay = document.getElementById('cookieDisplay');
// Function to get cookie by name
function getCookie(name) {
var cookieArray = document.cookie.split(';');
for (var i = 0; i < cookieArray.length; i++) {
var cookiePair = cookieArray[i].split('=');
if (cookiePair[0].trim() === name) {
return cookiePair[1];
}
}
return null;
}
// Get the value of a specific cookie
var username = getCookie('hscore');
// Display the value of the cookie in the HTML
if (username) {
cookieDisplay.textContent = username;
} else {
cookieDisplay.textContent = 0;
}
const frame = document.getElementById("frame");
const basket = document.getElementById("basket");
scoreValue = document.getElementById("scoreValue");
let score = 0;
// Function to create a new ball
function createBall() {
const ball = document.createElement("div");
ball.classList.add("ball");
ball.style.left = `${Math.random() * 100}%`;
frame.appendChild(ball);
// Animation for the ball
const animation = ball.animate(
[
{ top: "-10%" }, // Start position above the frame
{ top: "110%" }, // End position below the frame
],
{
duration: 3000,
easing: "linear",
}
);
animation.onfinish = () => {
ball.remove();
};
// Select the audio element
const collisionSound = document.getElementById("collisionSound");
const scoreWin = document.getElementById("win");
const scoreWinMore = document.getElementById("winMore");
// Event listener for collision with basket
// animation.onupdate = () => {
// const basketRect = basket.getBoundingClientRect();
// const ballRect = ball.getBoundingClientRect();
// console.log(basketRect)
// console.log(ballRect)
// console.log(score)
// console.log(score)
// if (basketRect.bottom >= ballRect.top &&
// basketRect.top <= ballRect.bottom &&
// basketRect.right >= ballRect.left &&
// basketRect.left <= ballRect.right) {
// score++;
// scoreValue.textContent = score;
// ball.remove();
// // Play collision sound
// collisionSound.play();
// }
// };
function updateGame() {
const basketRect = basket.getBoundingClientRect();
const ballRect = ball.getBoundingClientRect();
if (
basketRect.bottom >= ballRect.top &&
basketRect.top <= ballRect.bottom &&
basketRect.right >= ballRect.left &&
basketRect.left <= ballRect.right
) {
score++;
if(score > getCookie('hscore')){
document.cookie = "hscore="+ score.toString();
cookieDisplay.textContent = score;
}
scoreValue.textContent = score;
ball.remove();
// Play collision sound
collisionSound.play();
}
// Request next animation frame
requestAnimationFrame(updateGame);
}
// Start the game loop
requestAnimationFrame(updateGame);
function playScoreWinSound() {
scoreWin.play();
}
let scoreWinPlayed = false;
let scoreWin1 = false;
if (score % 10 === 0 && score !== 0 && !scoreWinPlayed) {
scoreWin.play();
scoreWinPlayed = true; // Set the flag to true after playing the sound
if (score == 40 && !scoreWin1) {
scoreWinMore.play();
scoreWin1 = true;
}
}
}
// Create a new ball every second
setInterval(createBall, 1000);
// Move basket left and right
document.addEventListener("keydown", (event) => {
const basketLeft = parseInt(getComputedStyle(basket).left);
const basketWidth = basket.offsetWidth;
const frameWidth = frame.offsetWidth;
if (event.key === "ArrowLeft" || event.key === "a") {
if (basketLeft > 0) {
basket.style.left = basketLeft - 10 + "px";
}
} else if (event.key === "ArrowRight" || event.key === "d") {
if (basketLeft < frameWidth - basketWidth) {
basket.style.left = basketLeft + 10 + "px";
}
}
});
// JavaScript functions for drag and drop functionality
});
function allowDrop(event) {
event.preventDefault();
}
function drag(event) {
event.dataTransfer.setData("text", event.target.src);
}
function drop(event) {
console.log(event);
event.preventDefault();
var data = event.dataTransfer.getData("text");
document.getElementById("basket").style.backgroundImage =
"url('" + data + "')";
}