-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.js
54 lines (46 loc) · 1.82 KB
/
pong.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
(function () {
'use strict';
if (typeof window.PongGame === "undefined") {
window.PongGame = {};
}
var Game = PongGame.Game = function (canvas, width, height) {
this.canvas = canvas;
this.context = canvas.getContext('2d');
this.canvas.width = width;
this.canvas.height = height;
this.ball = new PongGame.Ball(this.context);
this.playerLeft = new PongGame.Player(this.context, "left");
this.playerRight = new PongGame.Player(this.context, "right");
this.leftDetector = new PongGame.CollisionDetector(this.playerLeft, this.ball, this.context);
this.rightDetector = new PongGame.CollisionDetector(this.playerRight, this.ball, this.context);
}
Game.prototype.renderBackground = function () {
this.context.fillStyle = 'white';
this.context.clearRect(0, 0, canvas.width, canvas.height);
this.context.fillRect(canvas.width/2, 0, 2, canvas.height);
}
Game.prototype.render = function () {
this.renderBackground();
this.ball.move();
this.ball.render();
this.ball.increaseBallSpeed();
this.playerLeft.render();
this.playerRight.render();
this.playerLeft.checkPaddlePosition();
this.playerRight.checkPaddlePosition();
this.leftDetector.checkCollision();
this.rightDetector.checkCollision();
this.leftDetector.score();
this.rightDetector.score();
this.renderScores();
}
Game.prototype.renderScores = function () {
this.context.fillStyle = 'white';
this.context.font = '30px Tahoma';
this.context.fillText(this.playerLeft.points, this.canvas.width/4 *2 - 40, 40);
this.context.fillText(this.playerRight.points, this.canvas.width/4 *2 + 25, 40);
}
Game.prototype.play = function () {
setInterval(this.render.bind(this), 7);
}
})();