-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
306 lines (273 loc) · 6.98 KB
/
script.js
File metadata and controls
306 lines (273 loc) · 6.98 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const GameStates = Object.freeze({
RUNNING: "RUNNING",
PAUSED: "PAUSED",
GAME_OVER: "GAME_OVER"
// NOT_STARTED: "NOT_STARTED"
});
const spells = [
{
name: "Ball Speed Boost",
apply: () => {
ballSpeed -= 80;
console.log("Ball Speed boosted to", ballSpeed);
},
reverse: () => {
ballSpeed += 80;
console.log("Ball Speed back to", ballSpeed);
},
duration: 5000
},
{
name: "ball size decreased",
apply: () => {
ballRadius -= 10;
console.log("ball size decreased to", ballRadius);
},
reverse: () => {
ballRadius += 10;
console.log("ball size increased to", ballRadius);
},
duration: 5000
},
{
name: "Extra Trial",
apply: () => {
Trials += 1;
document.getElementById("score").innerHTML = heartImgSrc.repeat(Trials);
console.log("Extra trial added. Total trials:", Trials);
},
reverse: () => {
return;
},
duration: 5000
},
{
name:"increase paddle speed",
apply: () => {
movingSpeed += 20;
console.log("Paddle speed increased");
}
,
reverse: () => {
movingSpeed -= 20;
console.log("Paddle speed back to normal");
},
duration: 5000
}
,
{
name:"decrease ball speed",
apply: () => {
ballSpeed += 80;
console.log("Ball speed decreased");
}
,
reverse: () => {
ballSpeed -= 80;
console.log("Ball speed back to normal");
},
duration: 5000
}
,
// TODO: add more spells as previous
// spells like increase the size of paddle but take care that collision is still correct
// increase ball size
// decrease paddle size
];
const canvas = document.getElementById('cv');
const ctx = canvas.getContext('2d');
const statebtn = document.getElementById('gameStatebtn');
const timer = document.getElementById('timer');
const heartImgSrc = '<img src="./assets/img/heart.png" class="heart-icon">';
// Set canvas dimensions
canvas.width = 800;
canvas.height = 600;
let ballRadius = 30;
let xBall=35;
let yBall=35;
let xpaddle = (canvas.width - 100) / 2;
let xdiraction = 1;
let ydiraction = 1;
let Trials = 3 ;
let gameState = GameStates.PAUSED;
let gameTime = 0;
let ballSpeed = 100;
let spellTimerId = null;
var spellX = 0;
let spellY = 0;
let spell = false;
let movingSpeed = 20;
let highScore = localStorage.getItem('brickBreakerHighScore') || 0;
function setUp()
{
if(gameState != GameStates.RUNNING) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle();
if(spell) drawSpell();
timer.innerText = getTimerValue();
// --- NEW: DRAW HIGH SCORE ---
ctx.font = "20px Arial";
ctx.fillStyle = "black";
// Shows "High Score: 15s" at the top left
ctx.fillText("High Score: " + (highScore / 1000).toFixed(0) + "s", 20, 30);
if(!spell && Math.random() > 0.95){
startSpell();
}
}
function getTimerValue() {
let time = gameTime / 1000;
let mins = Math.floor(time / 60);
let secs = Math.floor(time % 60);
if (secs < 10) secs = "0" + secs;
return `${mins}:${secs}`;
}
function drawBall()
{
ctx.beginPath();
ctx.arc(xBall, yBall, ballRadius, 0, Math.PI * 2);
ctx.fillStyle = "red";
ctx.fill();
}
function drawPaddle()
{
ctx.beginPath();
ctx.rect(xpaddle, canvas.height -20 , 100, 10);
ctx.fillStyle = "blue";
ctx.fill();
}
function moveLeft()
{
if(gameState != GameStates.RUNNING) return;
if( xpaddle <=0)
{
xpaddle =0;
return;
}
xpaddle -=movingSpeed;
setUp();
}
function moveRight()
{
if(gameState != GameStates.RUNNING) return;
if( xpaddle >= canvas.width-100 )
{
return;
}
xpaddle +=movingSpeed;
setUp();
}
function bounce(x,y)
{
if(CollisionWithPaddle(x,y)||y-ballRadius<=0)
{
ydiraction = -ydiraction;
}
else if(y+ballRadius>=canvas.height)
{
Trials -=1;
document.getElementById("score").innerHTML = heartImgSrc.repeat(Trials);
if(Trials==0)
{
if (gameTime > highScore) {
highScore = gameTime;
localStorage.setItem('brickBreakerHighScore', highScore);
alert("New Record! " + (highScore/1000).toFixed(0) + "s");
}
// Game Over
document.getElementById("score").innerHTML = "<b>GAME OVER</b>";
clearInterval(ballTimerId);
alert("Game Over Your Best Score is: " + (highScore/1000).toFixed(0) + "s");
}
else
{
// Reset Ball Position
xBall=35;
yBall=35;
xdiraction = 1;
ydiraction = 1;
}
}
else if(x-ballRadius<=0 ||x+ballRadius>=canvas.width)
{
xdiraction = -xdiraction;
}
}
function CollisionWithPaddle(x,y)
{
if(x>xpaddle && x<xpaddle+100 && y+30 >= canvas.height -20)
{
return true;
}
return false;
}
function toggleGameState(){
if(gameState == GameStates.RUNNING){
gameState = GameStates.PAUSED;
clearInterval(ballTimerId);
clearInterval(spellTimerId)
gameStatebtn.textContent = "Resume";
}
else if(gameState == GameStates.PAUSED){
gameState = GameStates.RUNNING;
startBall();
startSpell();
gameStatebtn.textContent = "Pause";
}
}
function startBall(){
ballTimerId = setInterval( function(){
if( xBall >= canvas.width || yBall >= canvas.height)
{
xBall=0;
yBall=0;
}
xBall +=10*xdiraction;
yBall +=10*ydiraction;
setUp();
bounce(xBall,yBall);
gameTime += ballSpeed;
}, ballSpeed);
}
function startSpell(){
if(!spell){
spellX = getRandomNumberBetween(30, canvas.width - 30);
}
spell = true;
spellTimerId = setInterval( function(){
if(spellY == canvas.height){
stopSpell();
return;
}
spellY += 10;
if(CollisionWithPaddle(spellX, spellY)){
console.log("spell catched")
castSpell(getRandomNumberBetween(0, spells.length));
stopSpell();
}
}, 100);
}
function stopSpell(){
clearInterval(spellTimerId);
spellY = 0;
spell = false;
}
function castSpell(index) {
const spell = spells[index];
if (!spell) return;
spell.apply();
setTimeout(() => {
spell.reverse();
}, spell.duration);
}
function drawSpell()
{
ctx.beginPath();
ctx.arc(spellX, spellY, 20, 0, Math.PI * 2);
ctx.fillStyle = "green";
ctx.fill();
}
function getRandomNumberBetween(a, b) {
return Math.floor(Math.random() * b) + a;
}
setUp();