-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
243 lines (221 loc) · 5.46 KB
/
script.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Canvas
const { body } = document;
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const width = 500;
const height = 700;
const screenWidth = window.screen.width;
const canvasPosition = screenWidth / 2 - width / 2;
const isMobile = window.matchMedia('(max-width: 600px)');
const gameOverEl = document.createElement('div');
// Paddle
const paddleHeight = 10;
const paddleWidth = 50;
const paddleDiff = 25;
let paddleBottomX = 225;
let paddleTopX = 225;
let playerMoved = false;
let paddleContact = false;
// Ball
let ballX = 250;
let ballY = 350;
const ballRadius = 5;
// Speed
let speedY;
let speedX;
let trajectoryX;
let computerSpeed;
// Change Mobile Settings
if (isMobile.matches) {
speedY = -2;
speedX = speedY;
computerSpeed = 4;
} else {
speedY = -1;
speedX = speedY;
computerSpeed = 3;
}
// Score
let playerScore = 0;
let computerScore = 0;
const winningScore = 8;
let isGameOver = true;
let isNewGame = true;
// Render Everything on Canvas
function renderCanvas() {
// Canvas Background
context.fillStyle = '#0F2027';
context.fillRect(0, 0, width, height);
// Paddle Color
context.fillStyle = 'white';
// Player Paddle (Bottom)
context.fillRect(paddleBottomX, height - 20, paddleWidth, paddleHeight);
// Computer Paddle (Top)
context.fillRect(paddleTopX, 10, paddleWidth, paddleHeight);
// Dashed Center Line
context.beginPath();
context.setLineDash([4]);
context.moveTo(0, 350);
context.lineTo(500, 350);
context.strokeStyle = 'grey';
context.stroke();
// Ball
context.beginPath();
context.arc(ballX, ballY, ballRadius, 2 * Math.PI, false);
context.fillStyle = 'white';
context.fill();
// Score
context.font = '32px Courier New';
context.fillText(playerScore, 20, canvas.height / 2 + 50);
context.fillText(computerScore, 20, canvas.height / 2 - 30);
}
// Create Canvas Element
function createCanvas() {
canvas.width = width;
canvas.height = height;
body.appendChild(canvas);
renderCanvas();
}
// Reset Ball to Center
function ballReset() {
ballX = width / 2;
ballY = height / 2;
speedY = -3;
paddleContact = false;
}
// Adjust Ball Movement
function ballMove() {
// Vertical Speed
ballY += -speedY;
// Horizontal Speed
if (playerMoved && paddleContact) {
ballX += speedX;
}
}
// Determine What Ball Bounces Off, Score Points, Reset Ball
function ballBoundaries() {
// Bounce off Left Wall
if (ballX < 0 && speedX < 0) {
speedX = -speedX;
}
// Bounce off Right Wall
if (ballX > width && speedX > 0) {
speedX = -speedX;
}
// Bounce off player paddle (bottom)
if (ballY > height - paddleDiff) {
if (ballX > paddleBottomX && ballX < paddleBottomX + paddleWidth) {
paddleContact = true;
// Add Speed on Hit
if (playerMoved) {
speedY -= 1;
// Max Speed
if (speedY < -5) {
speedY = -5;
computerSpeed = 6;
}
}
speedY = -speedY;
trajectoryX = ballX - (paddleBottomX + paddleDiff);
speedX = trajectoryX * 0.3;
} else if (ballY > height) {
// Reset Ball, add to Computer Score
ballReset();
computerScore++;
}
}
// Bounce off computer paddle (top)
if (ballY < paddleDiff) {
if (ballX > paddleTopX && ballX < paddleTopX + paddleWidth) {
// Add Speed on Hit
if (playerMoved) {
speedY += 1;
// Max Speed
if (speedY > 5) {
speedY = 5;
}
}
speedY = -speedY;
} else if (ballY < 0) {
// Reset Ball, add to Player Score
ballReset();
playerScore++;
}
}
}
// Computer Movement
function computerAI() {
if (playerMoved) {
if (paddleTopX + paddleDiff < ballX) {
paddleTopX += computerSpeed;
} else {
paddleTopX -= computerSpeed;
}
}
}
function showGameOverEl(winner) {
// Hide Canvas
canvas.hidden = true;
// Container
gameOverEl.textContent = '';
gameOverEl.classList.add('game-over-container');
// Title
const title = document.createElement('h1');
title.textContent = `${winner} Wins!`;
// Button
const playAgainBtn = document.createElement('button');
playAgainBtn.setAttribute('onclick', 'startGame()');
playAgainBtn.textContent = 'Play Again';
// Append
gameOverEl.append(title, playAgainBtn);
body.appendChild(gameOverEl);
}
// Check If One Player Has Winning Score, If They Do, End Game
function gameOver() {
if (playerScore === winningScore || computerScore === winningScore) {
isGameOver = true;
// Set Winner
const winner = playerScore === winningScore ? 'Player 1' : 'Computer';
showGameOverEl(winner);
}
}
// Called Every Frame
function animate() {
renderCanvas();
ballMove();
ballBoundaries();
computerAI();
gameOver();
if(!isGameOver) {
window.requestAnimationFrame(animate);
}
}
// Start Game, Reset Everything
function startGame() {
if (isGameOver && !isNewGame) {
body.removeChild(gameOverEl);
canvas.hidden = false;
}
isGameOver = false;
isNewGame = false;
playerScore = 0;
computerScore = 0;
ballReset();
createCanvas();
animate();
canvas.addEventListener('mousemove', (e) => {
playerMoved = true;
// Compensate for canvas being centered
paddleBottomX = e.clientX - canvasPosition - paddleDiff;
if (paddleBottomX < paddleDiff) {
paddleBottomX = 0;
}
if (paddleBottomX > width - paddleWidth) {
paddleBottomX = width - paddleWidth;
}
// Hide Cursor
canvas.style.cursor = 'none';
});
}
// On Load
startGame();