-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
132 lines (119 loc) · 3.54 KB
/
engine.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
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
var PongEngine = (function (canvas, FPS) {
var ctx = canvas.getContext('2d');
var gameloop;
var engine = {};
engine.STOPPED = true;
var Player = {
score: 0
};
var ball = {
startSize: 8,
minSize: 2,
r: 8,
x: (canvas.width / 2),
y: (canvas.height / 2),
vx: Math.random() * 20 + -1,
vy: Math.random() * 20 + -1,
center: {
x: function() {
return ball.x + ball.r;
},
y: function() {
return ball.y + ball.r;
}
}
};
var paddle1 = {
w: 10,
h: 50,
x: 1,
y: 0
};
engine.drawPaddle = function (paddle) {
if (paddle.y > canvas.height - paddle.h){
paddle.y = canvas.height - paddle.h;
} else if (paddle.y < 0) {
paddle.y = 0;
}
ctx.beginPath();
ctx.fillStyle = "white";
ctx.rect(paddle.x, paddle.y, paddle.w, paddle.h);
ctx.closePath();
ctx.fill();
};
engine.drawBall = function () {
ball.x = ball.x + ball.vx;
ball.y = ball.y + ball.vy;
if (ball.x < 0) {
engine.endGame();
return;
}
if (ball.x > canvas.width - ball.r) {
ball.vx = -1 * ball.vx;
}
if (ball.y > canvas.height - ball.r || ball.y < 0 + ball.r) {
ball.vy = -1 * ball.vy;
}
ctx.beginPath();
ctx.fillStyle = "white";
ctx.arc(ball.x, ball.y, ball.r * 2, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
};
engine.checkCollision = function (paddle, ball) {
var paddleCenter = {
x: (paddle.x + paddle.w) / 2,
y: (paddle.y + paddle.h) / 2
};
if (ball.x - ball.r <= (paddle.w + paddle.x)) {
if ((ball.y + ball.r + ball.r <= paddle.y + paddle.h || ball.y <= paddle.y + paddle.h) && (ball.y + ball.r + ball.r >= paddle.y || ball.y >= paddle.y)) {
ball.vx = -1 * ball.vx;
if (ball.y < paddleCenter.y) {
ball.vy = -1 * ball.vy;
}
ball.vx += 1;
ball.vy += 1;
Player.score += 1;
if (ball.r > ball.minSize && ball.r <= ball.startSize) {
ball.r -= 1;
}
}
}
};
engine.renderScore = function () {
var scoreBoard = document.getElementById('score');
scoreBoard.innerHTML = Player.score;
};
engine.startGame = function () {
engine.STOPPED = false;
ball.x = (canvas.width / 2);
ball.y = (canvas.width / 2);
ball.vx = Math.random() * 3 + 1;
ball.vy = Math.random() * 3 + 1;
ball.r = ball.startSize;
Player.score = 0;
gameloop = setInterval(function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
engine.drawBall();
engine.drawPaddle(paddle1);
engine.checkCollision(paddle1, ball);
engine.renderScore();
}, 1000 / FPS);
};
engine.endGame = function () {
engine.STOPPED = true;
ctx.clearRect(0, 0, canvas.width, canvas.height);
clearInterval(gameloop);
ctx.font="20px Arial";
ctx.fillText("Game Over! Final Score: " + Player.score, canvas.width / 2 - 125, canvas.height / 2);
ctx.fillText("Press Enter to Begin!", canvas.width / 2 - 95, canvas.height / 2 + 25);
};
engine.mousePosition = function (evt) {
var gameBounds = canvas.getBoundingClientRect();
paddle1.y = evt.clientY - gameBounds.top;
};
ctx.font="20px Arial";
ctx.fillStyle = 'white';
ctx.fillText("Press Enter to Begin!", canvas.width / 2 - 95, canvas.height / 2);
return engine;
});