-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
47 lines (41 loc) · 1.27 KB
/
player.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
(function () {
'use strict';
if (typeof window.PongGame === "undefined") {
window.PongGame = {};
}
var Player = window.PongGame.Player = function (context, side, ball) {
this.context = context;
var paddleStartPosition = side == "left" ? [20, 215] : [780, 215];
this.paddle = new PongGame.Paddle(this.context, paddleStartPosition);
this.side = side;
this.setListeners();
this.paddleDirection = 0;
this.points = 0;
}
Player.prototype.render = function () {
this.paddle.move(this.paddleDirection);
this.paddle.render();
}
Player.prototype.checkPaddlePosition = function () {
if (this.paddle.isTop()) {
this.paddleDirection = 0;
} else if (this.paddle.isBottom()) {
this.paddleDirection = 0;
}
}
Player.prototype.setListeners = function () {
var player = this;
var upCode = this.side == "left" ? 119 : 111;
var downCode = this.side == "left" ? 115 : 108;
window.addEventListener("keypress", function (event) {
if (event.keyCode == upCode) {
player.paddleDirection = -1.5;
} else if (event.keyCode == downCode) {
player.paddleDirection = 1.5;
}
});
window.addEventListener("keyup", function (event) {
player.paddleDirection = 0;
});
}
})();