-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
163 lines (142 loc) · 4.01 KB
/
Copy pathscript.js
File metadata and controls
163 lines (142 loc) · 4.01 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
// Canvas setup
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 500;
let score = 0;
let gameFrame = 0;
ctx.font = '50px Georgia';
// Mouse Interactivity
let canvasPosition;
canvasPosition = canvas.getBoundingClientRect();
window.addEventListener('resize', function () {
canvasPosition = canvas.getBoundingClientRect();
console.log(canvasPosition)
})
const mouse = {
x: canvas.width / 2,
y: canvas.height / 2,
click: false
}
canvas.addEventListener('mousedown', function (event) {
mouse.click = true;
mouse.x = event.x - canvasPosition.left;
mouse.y = event.y - canvasPosition.top;
console.log(mouse.x, mouse.y);
})
canvas.addEventListener('mouseup', function (event) {
mouse.click = false;
})
// Player
const playerLeft = new Image();
playerLeft.src = 'sprites/fish_swim_left.png';
const playerRight = new Image();
playerRight.src = 'sprites/fish_swim_right.png';
class Player {
constructor() {
this.x = canvas.width;
this.y = canvas.height / 2;
this.radius = 50;
this.angle = 0;
this.frameX = 0;
this.frameY = 0;
this.frame = 0;
this.spriteWidth = 418;
this.spriteHeight = 397;
}
update() {
const dx = this.x - mouse.x;
const dy = this.y - mouse.y;
if (mouse.x != this.x) {
this.x -= dx / 20;
}
if (mouse.y != this.y) {
this.y -= dy / 20;
}
}
draw() {
if (mouse.click) {
ctx.lineWidth = 0.2;
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(mouse.x, mouse.y);
ctx.stroke();
}
ctx.fillStyle = "red";
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
ctx.fillRect(this.x, this.y, this.radius, 10);
}
}
const player = new Player();
// Bubbles
const bubblesArray = [];
class Bubble {
constructor() {
this.x = Math.random() * canvas.width;
this.y = canvas.height + 100;
this.radius = 50;
this.speed = Math.random() * 5 + 1;
this.distance;
this.counted = false;
this.sound = Math.random() <= 0.5 ? 'sound1' : 'sound2';
}
update() {
this.y -= this.speed;
const dx = this.x - player.x;
const dy = this.y - player.y;
this.distance = Math.sqrt(dx * dx + dy * dy)
}
draw() {
ctx.fillStyle = 'blue';
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
ctx.stroke();
}
}
const bubblePop1 = document.createElement('audio');
bubblePop1.src = 'sounds/Plop.ogg';
const bubblePop2 = document.createElement('audio');
bubblePop1.src = 'sounds/bubbles-single1.wav';
function handleBubbles() {
if (gameFrame % 50 == 0) {
bubblesArray.push(new Bubble());
}
for (let i = 0; i < bubblesArray.length; i++) {
if (bubblesArray[i].y < 0 - bubblesArray[i].radius * 2) {
bubblesArray.splice(i, 1);
}
bubblesArray[i].update();
bubblesArray[i].draw();
if (bubblesArray[i]) {
if (bubblesArray[i].distance < bubblesArray[i].radius + player.radius) {
if (!bubblesArray[i].counted) {
if (bubblesArray[i].sound == 'sound1') {
bubblePop1.play();
} else {
bubblePop2.play();
}
score++;
bubblesArray[i].counted = true;
bubblesArray.splice(i, 1);
}
}
}
}
}
// Animation Loop
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
handleBubbles();
player.update();
player.draw();
ctx.fillStyle = 'black';
ctx.fillText('score: ' + score, 10, 50);
gameFrame++;
requestAnimationFrame(animate);
}
animate();