-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
448 lines (368 loc) · 9.17 KB
/
js.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//Canvas düzenlemeleri
const cvs = document.getElementById("oyuncanvas");
const ctx = cvs.getContext("2d");
cvs.width = 1150;
cvs.height = 600;
let canvasposition = cvs.getBoundingClientRect();
console.log(canvasposition.x,canvasposition.y);
console.log(canvasposition.left,canvasposition.top);
const bg = new Image();
bg.src = "gorseller/bg.png";
const startbut = new Image();
startbut.src = "gorseller/start-button.png";
const isim = new Image();
isim.src = "gorseller/isim.png";
const arr = new Image();
arr.src = "gorseller/ok.png";
const playbg = new Image();
playbg.src = "gorseller/bg-play.png";
const charSprite = new Image();
charSprite.src = "karakter/sprite-char2.png";
const redball1 = new Image();
redball1.src = "gorseller/ball1.png"
const redball2 = new Image();
redball2.src = "gorseller/ball2.png"
const redball3 = new Image();
redball3.src = "gorseller/ball3.png"
const redball4 = new Image();
redball4.src = "gorseller/ball4.png"
let redballs = [redball1,redball2,redball3,redball4];
const blueball1 = new Image();
blueball1.src = "gorseller/ball5.png"
const blueball2 = new Image();
blueball2.src = "gorseller/ball6.png"
const blueball3 = new Image();
blueball3.src = "gorseller/ball7.png"
const blueball4 = new Image();
blueball4.src = "gorseller/ball8.png"
let blueballs = [blueball1,blueball2,blueball3,blueball4];
let ballChooser = 0;
let keys = [];
let score=0;
let baloon1;
let baloon2;
let controller = 0;
let basla= 0;
let ctr = 0;
const mouse = {
clicked: false,
x: null,
y: null,
width: 250,
height: 250,
}
function collision(first, second) {
if (!(first.x > second.x + second.width ||
first.x + first.width < second.x ||
first.y > second.y + second.height||
first.y + first.height < second.y
)
) {
return true;
};
};
cvs.addEventListener('mousedown', function () {
mouse.clicked = true;
console.log(mouse.x,mouse.y);
});
cvs.addEventListener('mouseup', function () {
mouse.clicked = false;
});
cvs.addEventListener('mousemove', function (e) {
mouse.x = e.x - canvasposition.x ;
mouse.y = e.y - canvasposition.y;
});
cvs.addEventListener('mouseleave', function () {
mouse.x = undefined;
mouse.y = undefined;
});
let player = {
x: 548,
y: 491,
width: 48,
height: 62,
frameX: 0,
frameY: 0,
speed: 5,
moving: false,
};
let arrow = {
x: player.x + 20,
y: player.y +15,
width: 10,
height: 32,
speed: 10,
moving: false,
showing: false,
};
const button={
x: cvs.width*45/100,
y: cvs.height/3,
width: 108,
height: 103,
}
console.log(button.x,button.y);
function start(){
if (collision(mouse,button) && mouse.clicked) {
basla=1;
}
ctx.drawImage(startbut,button.x,button.y,button.width,button.height);
}
function mainMenu(){
if (basla ==1) {
basla=0;
bouncingBalls(baloon1,redballs);
}
else{
ctx.clearRect(0,0,cvs.width,cvs.height);
ctx.drawImage(bg, 0, 0, cvs.width, cvs.height);
ctx.drawImage(isim, 350, 75, 460, 240);
requestAnimationFrame(mainMenu);
}
start();
}
mainMenu();
function bouncingBalls(ball,ballColor){
ctx.clearRect(0, 0, cvs.width, cvs.height);
ball = {
//starting positions
x: Math.floor(Math.random() * 800) + 200,
y: 50,
//sizes - r is radius of ball
sizenum: 0,
width: 87,
height: 87,
//speed - these will be
xv: 6,
yv: 6,
moving: true,
}
function drawSprite(img, sX, sY, sW, sH, dX, dY, dW, dH) {
ctx.drawImage(img, sX, sY, sW, sH, dX, dY, dW, dH);
}
function arrowAnimate() {
if (arrow.showing) {
drawSprite(
arr,
0,
0,
arrow.width,
arrow.height,
arrow.x,
arrow.y,
arrow.width,
arrow.height
);
if (arrow.y > -15 && arrow.moving == true) {
arrow.y -= arrow.speed;
} else {
arrow.y = player.y + 1;
arrow.x = player.x + 20;
arrow.moving = false;
arrow.showing = false;
}
requestAnimationFrame(arrowAnimate);
}
}
setInterval(function(){
if(!arrow.showing){
arrow.x = player.x + 20;
}
},1)//her saniye arrow.showing doğru mu diye bakıyor eğer yanlışsa eşitliyor.
setInterval(function () {
ctx.clearRect(0, 0, cvs.width, cvs.height);
ctx.drawImage(playbg, 0, 0, cvs.width, cvs.height);
drawSprite(
charSprite,
player.width * player.frameX,
player.height * player.frameY,
player.width,
player.height,
player.x,
player.y,
player.width,
player.height
);
movePlayer();
handlePlayerFrame();
}, 60);
window.addEventListener("keydown", function (e) {
if (e.keyCode == 32) {
if(!arrow.showing){
//Hızlanmayı çözen yer
arrow.moving = true;
arrow.showing = true;
arrowAnimate();
console.log("burdayım");
}} else {
keys[e.keyCode] = true;
player.moving = true;
}
// console.log(keys);
});
window.addEventListener("keyup", function (e) {
let keycode = e.keyCode;
if (e.keyCode == 32) {
} else {
delete keys[keycode];
player.moving = false;
}
});
window.addEventListener("keydown", function (e) {
let keycode = e.keyCode;
if (e.keyCode == 32 && e.target == document.body) {
e.preventDefault();
}
});
function moveArrow() {
if (arrow.y > -15 && arrow.moving == true) {
arrow.y -= arrow.speed;
} else {
arrow.y = player.y + 1;
arrow.moving = false;
}
}
function movePlayer() {
if (keys[65] && player.x > 0) {
player.x -= player.speed;
player.frameY = 2;
player.moving = true;
}
if (keys[68] && player.x < cvs.width - player.width) {
player.x += player.speed;
player.frameY = 1;
player.moving = true;
}
}
function handlePlayerFrame() {//Animasyonun adımlarını çizdiriyor
if (player.frameX < 3 && player.moving) {
player.frameX++;
} else if (player.frameX < 3 && player.moving == false) {
player.frameY = 0;
} else {
player.frameX = 0;
}
}
let fps, fpsInterval, startTime, now, then, elapsed;
function startAnimating(fps) {
fpsInterval = 1000 / fps;
then = Date.now();
startTime = then;
animate();
updateBall();
}
function animate() {
requestAnimationFrame(animate);
now = Date.now();
elapsed = now - then;
if (elapsed > fpsInterval) {
then = now - (elapsed % fpsInterval);
// if (ballChooser == 0) {
// }
ctx.clearRect(0, 0, cvs.width, cvs.height);
ctx.drawImage(playbg, 0, 0, cvs.width, cvs.height);
drawBall();
drawSprite(
charSprite,
player.width * player.frameX,
player.height * player.frameY,
player.width,
player.height,
player.x,
player.y,
player.width,
player.height
);
movePlayer();
handlePlayerFrame();
}
}
if (Math.floor(Math.random() *2) == 0) {
ball.xv=-ball.xv;
console.log("sa");
}
function updateBall(){
if (!(ball.sizenum<4)) {
ball.moving=false;
}
//move the ball
if (ball.moving) {
ball.x +=ball.xv;
ball.y+=ball.yv;
}
else {
ball.x = -5;
ball.y = -5;
}
//bounce ball
if (ball.sizenum == 1) {
ball.width=63;
ball.height=63;
}
if (ball.sizenum == 2) {
ball.width=31;
ball.height=31;
}
if (ball.sizenum == 3) {
ball.width=19;
ball.height=19;
}
if (ball.x - 5 <0 && ball.xv < 0) {
ball.xv = -ball.xv;
}
if (ball.x + ball.width > cvs.width && ball.xv > 0 ) {
ball.xv = -ball.xv;
}
if (ball.y < 0 && ball.yv <0) {
ball.yv = - ball.yv;
}
if (ball.y > 550-ball.height && ball.yv >0) {
ball.yv = - ball.yv;
}
//drawing ball
}
function drawBall(){
drawSprite(ballColor[ball.sizenum],0,0,ball.width,ball.height,ball.x,ball.y,ball.width,ball.height);
}
function collisionPlayer(first, second) {
if (!(first.x > second.x + second.width - 30 ||
first.x + first.width < second.x +18 ||
first.y > second.y + second.height ||
first.y + first.height < second.y +2
)
) {
return true;
};
};
function coll(){
if (collision(ball,arrow) && ball.sizenum < 4 && arrow.moving == true) {
ball.sizenum++;
score+=15;
drawBall();
arrow.y = player.y + 3;
arrow.x = player.x + 24;
arrow.moving = false;
arrow.showing = false;
}
else if (score == 60 && controller == 0) {
bouncingBalls(baloon2,blueballs);
controller++;
}
else if (score == 120 ) {
if (ctr==0) {
alert("Kazandınız!\nLütfen Sayfayı Yenileyiniz!\nSkorunuz: " + score);
ctr++;
}
}
if (collisionPlayer(ball, player) ) {
if (ctr==0) {
alert("Öldünüz\nLütfen Sayfayı Yenileyiniz!\nSkorunuz: " + score);
ctr++;
}
}
}
setInterval(coll,1000/60);
setInterval(updateBall, 1000/60);
startAnimating(60);
}
// bouncingBalls(baloon1,redballs);