-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
236 lines (193 loc) · 6.42 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
console.log("Hello World!");
// Sparkle Effect Code (Taken from https://codepen.io/kucsatax/pen/vyWevX)
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const colors = ["#FF0000", "#FF8C00", "#FFD700", "#ADFF2F", "#00FF00", "#00FA9A", "#00FFFF", "#1E90FF", "#0000FF", "#8A2BE2", "#FF00FF", "#FF1493"];
class Sparkle {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = Math.random() * 10 + 5;
this.speedX = Math.random() * 3 - 2.5;
this.speedY = Math.random() * 3 - 2.5;
this.color = colors[Math.floor(Math.random() * colors.length)];
}
update() {
this.x += this.speedX;
this.y += this.speedY;
if (this.size > 0.2) this.size -= 0.1;
}
draw() {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
}
}
let sparkles = [];
function initSparkles() {
for (let i = 0; i < 30; i++) {
const x = Math.random() * canvas.width; // Rastgele x koordinatı
const y = Math.random() * canvas.height; // Rastgele y koordinatı
sparkles.push(new Sparkle(x, y));
}
}
function handleSparkles() {
for (let i = 0; i < sparkles.length; i++) {
sparkles[i].update();
sparkles[i].draw();
if (sparkles[i].size <= 0.2) {
sparkles.splice(i, 1);
i--;
}
}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
handleSparkles();
requestAnimationFrame(animate);
}
animate();
window.addEventListener("resize", function () {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
});
// Sparkle Effect End
console.log("Sparkle Effect Ready");
const rock = document.querySelector('.rock');
const paper = document.querySelector('.paper');
const scissors = document.querySelector('.scissors');
const OutCome = document.querySelector('.outcome');
const versus = document.querySelector('.versus');
const p = document.createElement('p');
const result = document.createElement('h2');
const clickSfx = new Audio('./sounds/clicksfx.wav');
const winSfx = new Audio('./sounds/winnersfx.wav');
const lostSfx = new Audio('./sounds/lostsfx.wav');
//score
const score = document.querySelector('.score');
const playerScoreSpan = document.querySelector('.player-score');
const computerScoreSpan = document.querySelector('.computer-score');
// playRound p
p.style.fontSize = "22px";
p.style.textShadow = '2px 2px 4px black';
let playerScore = 0;
let computerScore = 0;
playerScoreSpan.textContent = `Player Score: ${playerScore}`;
computerScoreSpan.textContent = `Computer Score: ${computerScore}`;
function getComputerChoice() {
let randomChoice = Math.floor(Math.random() * 3);
if (randomChoice === 0) {
return "rock";
} else if (randomChoice === 1) {
return "paper";
} else {
return "scissors";
}
}
function updateScores(playerScore, computerScore) {
playerScoreSpan.innerText = `Player Score: ${playerScore}`;
computerScoreSpan.innerText = `Computer Score: ${computerScore}`;
}
rock.addEventListener('click', () => {
if (playerScore == 0 && computerScore == 0) {
result.innerText = " ";
}
const computerChoice = getComputerChoice();
const playerChoice = 'rock';
clickSfx.play();
playRound(playerChoice, computerChoice);
updateScores(playerScore, computerScore);
FinalResults(playerScore, computerScore);
})
rock.addEventListener('mouseover', (event) => {
event.target.textContent = '⛰️';
})
rock.addEventListener('mouseout', (event) => {
event.target.textContent = 'ROCK';
})
paper.addEventListener('click', () => {
if (playerScore == 0 && computerScore == 0) {
result.innerText = " ";
}
const computerChoice = getComputerChoice();
const playerChoice = 'paper';
clickSfx.play();
playRound(playerChoice, computerChoice);
updateScores(playerScore, computerScore);
FinalResults(playerScore, computerScore);
})
paper.addEventListener('mouseover', (event) => {
event.target.textContent = '📜';
})
paper.addEventListener('mouseout', (event) => {
event.target.textContent = 'PAPER';
})
scissors.addEventListener('click', () => {
if (playerScore == 0 && computerScore == 0) {
result.innerText = " ";
}
const computerChoice = getComputerChoice();
const playerChoice = 'scissors';
clickSfx.play();
playRound(playerChoice, computerChoice);
updateScores(playerScore, computerScore);
FinalResults(playerScore, computerScore);
})
scissors.addEventListener('mouseover', (event) => {
event.target.textContent = '✂️';
})
scissors.addEventListener('mouseout', (event) => {
event.target.textContent = 'SCISSORS';
})
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
p.innerText = `Ties ${playerSelection} to ${computerSelection}.`;
p.style.color = 'white';
OutCome.append(p);
} else if (playerSelection === "rock" && computerSelection === "scissors") {
playerScore++
p.innerText = `You Win! ${playerSelection} beats ${computerSelection}.`;
p.style.color = '#00FFFF';
OutCome.append(p);
} else if (playerSelection === "paper" && computerSelection === "rock") {
playerScore++
p.innerText = `You Win! ${playerSelection} beats ${computerSelection}.`;
p.style.color = '#00FFFF';
OutCome.append(p);
} else if (playerSelection === "scissors" && computerSelection === "paper") {
playerScore++
p.innerText = `You Win! ${playerSelection} beats ${computerSelection}.`;
p.style.color = '#00FFFF';
OutCome.append(p);
} else {
computerScore++
p.innerText = `You Lose! ${computerSelection} beats ${playerSelection}.`;
p.style.color = '#EF0107';
OutCome.append(p);
}
}
function FinalResults() {
if (playerScore === 5) {
result.innerText = `Awesome! You Won the Game. Your Score is ${playerScore} against ${computerScore}.`;
OutCome.append(result)
winSfx.play();
result.style.color = '#00FF00';
result.style.fontWeight = '700';
result.style.textShadow = '2px 2px 4px black';
playerScore = 0;
computerScore = 0;
initSparkles(); // Sparkle effect
} else if (computerScore === 5) {
result.innerText = `Yikes! You Lose the Game. Your Score is ${playerScore} against ${computerScore}.`;
OutCome.append(result)
lostSfx.play();
result.style.color = 'red';
result.style.fontWeight = '700';
result.style.textShadow = '2px 2px 4px black';
playerScore = 0;
computerScore = 0;
}
}