-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
113 lines (91 loc) · 2.86 KB
/
app.js
File metadata and controls
113 lines (91 loc) · 2.86 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
(function($) {
"use strict";
let unit = 1;
let pongWidth = 600;
let pongHeight = 450;
let Pong = function(options) {
this.$elem = $(options.elem);
this.unit = unit;
this.unitWidth = pongWidth;
this.unitHeight = pongHeight;
this.stop = false;
this.run();
}
Pong.prototype.run = function() {
this.$elem.width(this.unitWidth * this.unit).height(this.unitHeight * this.unit);
this.ball = new Ball({
elem: ".ball"
});
this.ball.pong = this;
this.start();
}
Pong.prototype.start = function() {
let _this = this;
$('.stop-button').click(function() {
if ($(this).text() === "Stop!") {
$(this).text('Resume');
_this.stop = true;
} else {
$(this).text('Stop!');
_this.stop = false;
_this.ball.move();
}
});
this.ball.move();
}
let Ball = function(options) {
this.$elem = $(options.elem);
this.unit = unit;
this.unitWidth = 15;
this.unitHeight = 15;
/* Represent Position */
this.x = 0;
this.y = 0;
/* Represent Direction */
this.xDirection = 1;
this.yDirection = 1;
/* Represent Magnitude */
this.xMagnitude = 1;
this.yMagnitude = 1;
this.$elem.width(this.unitWidth * this.unit).height(this.unitHeight * this.unit);
}
Ball.prototype.move = function() {
this.y = this.y + (this.yDirection * this.yMagnitude * this.unit);
this.x = this.x + (this.xDirection * this.xMagnitude * this.unit);
this.$elem.css({
bottom : (this.y) + 'px',
left: (this.x) + 'px'
});
setTimeout(this.checkObstacles.bind(this), 0);
}
Ball.prototype.checkObstacles = function() {
let rightMost = this.x + this.unitWidth;
let topMost = this.y + this.unitHeight;
let topRightCorner = (rightMost === pongWidth) && (topMost === pongHeight) && this.xDirection === 1 && this.yDirection === 1;
let topLeftCorner = (this.x === 0) && (topMost === pongHeight) && this.xDirection === -1 && this.yDirection === 1;
let bottomRightCorner = (rightMost === pongWidth) && (this.y === 0) && this.xDirection === 1 && this.yDirection === -1;
let bottomleftCorner = (this.x === 0) && (this.y === 0) && this.xDirection === -1 && this.yDirection === -1;
/* if corner */
if (topRightCorner || topLeftCorner || bottomRightCorner || bottomleftCorner) {
this.xDirection = -this.xDirection;
this.yDirection = -this.yDirection;
} else {
/* if border */
if ((rightMost >= pongWidth) || this.x < 1) {
/* symmetry wrt x-axis */
this.xDirection = -this.xDirection;
} else if ((topMost >= pongHeight) || this.y < 1) {
/* symmetry wrt y-axis */
this.yDirection = -this.yDirection;
}
}
if (!this.pong.stop) {
this.move();
}
}
$(function() {
new Pong({
elem: ".grid"
});
});
})(window.jQuery);