-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.js
76 lines (61 loc) · 2.07 KB
/
ball.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
(function () {
'use strict';
if (typeof window.PongGame === "undefined") {
window.PongGame = {};
}
var Ball = window.PongGame.Ball = function(context) {
this.context = context;
this.position = [400, 250];
this.radius = 7;
this.direction = [1, 1];
this.hits = 0;
};
Ball.prototype.isTop = function() {
return (this.position[1] - this.radius) <= 0;
}
Ball.prototype.isBottom = function() {
return (this.position[1] + this.radius) > this.context.canvas.height;
}
Ball.prototype.isLeft = function() {
return (this.position[0] + this.radius) < 0 && this.direction[0] < 0;
}
Ball.prototype.isRight = function() {
return (this.position[0] + this.radius) > this.context.canvas.width && this.direction[0] > 0;
}
Ball.prototype.moreLeft = function(x1, x2) {
return (this.position[0] - this.radius >= x1 && this.position[0] - this.radius <= x2);
}
Ball.prototype.moreRight = function(x1, x2) {
return (this.position[0] + this.radius >= x1 && this.position[0] + this.radius <=x2);
}
Ball.prototype.betweenY = function(y1, y2) {
return (this.position[1] >= y1 && this.position[1] <=y2);
}
Ball.prototype.move = function () {
if (this.isTop() || this.isBottom()) {
this.direction[1] = -this.direction[1];
}
this.position[0] += this.direction[0];
this.position[1] += this.direction[1];
};
Ball.prototype.changeBallDirection = function() {
this.direction[0] = -this.direction[0];
}
Ball.prototype.checkHits = function () {
console.log("ball hits: " + this.hits);
return (this.hits % 5 === 0 && this.hits > 0)
};
Ball.prototype.increaseBallSpeed = function () {
if (this.checkHits()) {
this.direction[0] += (this.direction[0] < 0) ? -0.1 : 0.1;
this.direction[1] += (this.direction[1] < 0) ? -0.1 : 0.1;
this.hits = 0;
}
};
Ball.prototype.render = function () {
this.context.beginPath();
this.context.arc(this.position[0], this.position[1], this.radius, 0, 2 * Math.PI);
this.context.fillStyle = "#fff"
this.context.fill();
}
})();