-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
83 lines (70 loc) · 2.24 KB
/
script.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
const target = document.getElementById('target');
const scoreDisplay = document.getElementById('score');
const gameContainer = document.querySelector('.game-container');
const audio = document.getElementById('audio'); // Ses öğesi
const bgAudio = document.getElementById('bgAudio');
let score = 0;
let timeLeft = 60; // 1 dakika (60 saniye)
function updateScore() {
scoreDisplay.textContent = `Skor: ${score} - Süre: ${formatTime(timeLeft)}`;
}
function generateRandomPosition() {
const maxX = gameContainer.clientWidth - target.clientWidth;
const maxY = gameContainer.clientHeight - target.clientHeight;
const randomX = Math.floor(Math.random() * maxX);
const randomY = Math.floor(Math.random() * maxY);
return { x: randomX, y: randomY };
}
function moveTargetRandomly() {
const newPosition = generateRandomPosition();
target.style.left = `${newPosition.x}px`;
target.style.top = `${newPosition.y}px`;
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60).toString().padStart(2, '0');
const remainingSeconds = (seconds % 60).toString().padStart(2, '0');
return `${minutes}:${remainingSeconds}`;
}
function updateTime() {
timeLeft--;
updateScore();
if (timeLeft <= 0) {
clearInterval(gameInterval);
target.removeEventListener('click', handleTargetClick);
scoreDisplay.textContent = `Oyun Bitti! Skor: ${score} - Süre: 0:00`;
}
}
// Müziği çal/durdur
function toggleBackgroundMusic() {
if (bgAudio.paused) {
bgAudio.play();
} else {
bgAudio.pause();
}
}
function handleTargetClick() {
score++;
moveTargetRandomly();
updateScore();
playSound();
}
function playSound() {
audio.currentTime = 0; // Sesin başa sarılmasını sağlar
audio.play(); // Ses çal
}
function handleGameContainerClick(event) {
if (event.target === gameContainer) {
score--;
updateScore();
playSound2(); // İkinci sesi çal
}
}
function playSound2() {
audio2.currentTime = 0; // Sesin başa sarılmasını sağlar
audio2.play(); // İkinci sesi çal
}
updateScore();
moveTargetRandomly();
gameContainer.addEventListener('click', handleGameContainerClick);
const gameInterval = setInterval(updateTime, 1000);
target.addEventListener('click', handleTargetClick);