-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.html
More file actions
203 lines (170 loc) · 6.37 KB
/
pong.html
File metadata and controls
203 lines (170 loc) · 6.37 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Pong Game</title>
</head>
<body>
<div style="text-align: center;">
<canvas id="gameCanvas" width="800" height="600"></canvas>
</div>
<script>
/**
* Pong Game in plain Javascript
*
* Author: Carlos E. Torres
* E-mail: [email protected]
* Github: https://github.com/cetorres
*/
var canvas;
var canvasContext;
var ballX = 0;
var ballY = 0;
const ballSize = 20;
const framesPerSecond = 170;
var ballXVelocity = ballYVelocity = 1;
const padWidth = 15;
const padHeight = 120;
const padSpeedIncrement = 2;
const padOffset = 10;
var pad1Y = 0;
var pad2Y = 0;
var pad1Speed = 0;
var pad2Speed = 0;
var pad1X = 0;
var pad2X = 0;
var pad1Score = 0;
var pad2Score = 0;
var computerRate = 3.1;
window.onload = function () {
canvas = document.getElementById("gameCanvas");
canvasContext = canvas.getContext("2d");
pad1X = padOffset;
pad2X = canvas.width - padOffset - padWidth;
pad1Y = canvas.height / 2 - padHeight / 2;
pad2Y = canvas.height / 2 - padHeight / 2;
ballXVelocity = Math.floor(Math.random() * 2) % 2 == 0 ? 1 : -1
resetGame();
document.addEventListener("keydown", keyPush);
document.addEventListener("keyup", keyUp);
setInterval(game, 1000 / framesPerSecond);
}
function game() {
// Draw background
canvasContext.fillStyle = "black";
canvasContext.fillRect(0, 0, canvas.width, canvas.height);
// Check ball collision with border
if (ballX < 0) {
pad2Score++;
resetGame();
}
if (ballX > canvas.width) {
pad1Score++;
if (pad1Score % 3 == 0)
computerRate -= 0.1;
resetGame();
}
if (ballY < ballSize / 2) {
ballYVelocity *= -1;
}
if (ballY > canvas.height - ballSize / 2) {
ballYVelocity *= -1;
}
// Check pad1 collision with border
if (pad1Y < padOffset + padSpeedIncrement) {
pad1Y = padOffset + padSpeedIncrement;
}
if (pad1Y > canvas.height - padHeight - padOffset - padSpeedIncrement) {
pad1Y = canvas.height - padHeight - padOffset - padSpeedIncrement;
}
// Check pad2 collision with border
if (pad2Y < padOffset + padSpeedIncrement) {
pad2Y = padOffset + padSpeedIncrement;
}
if (pad2Y > canvas.height - padHeight - padOffset - padSpeedIncrement) {
pad2Y = canvas.height - padHeight - padOffset - padSpeedIncrement;
}
// Check ball collision with pad1
if (ballX < pad1X + padWidth + ballSize / 2 && ballY > pad1Y && ballY < (pad1Y + padHeight)) {
ballXVelocity *= -1;
// Change ball's Y velocity if ball hits closer to the pad's edge
var deltaY = ballY - (pad1Y + padHeight / 2);
ballYVelocity = deltaY * 0.035;
}
// Check ball collision with pad2
if (ballX > pad2X - ballSize / 2 && ballY > pad2Y && ballY < (pad2Y + padHeight)) {
ballXVelocity *= -1;
// Change ball's Y velocity if ball hits closer to the pad's edge
var deltaY = ballY - (pad2Y + padHeight / 2);
ballYVelocity = deltaY * 0.035;
}
// Computer movement
computerMovement();
// Move ball
ballX += ballXVelocity;
ballY += ballYVelocity;
// Move pads
pad1Y += pad1Speed;
pad2Y += pad2Speed;
// Draw center net
for (var i = 10; i < canvas.height; i += 40)
drawRect(canvas.width / 2 - 1, i, 1, 20, "grey");
// Draw ball
drawCircle(ballX, ballY, ballSize / 2);
// Draw pads
drawRect(pad1X, pad1Y, padWidth, padHeight);
drawRect(pad2X, pad2Y, padWidth, padHeight);
// Draw scores
drawText(pad1Score, canvas.width / 6, canvas.height / 8, "grey");
drawText(pad2Score, canvas.width - canvas.width / 6, canvas.height / 8, "grey");
}
function resetGame() {
ballX = canvas.width / 2 - ballSize / 2;
ballY = canvas.height / 2 - ballSize / 2;
ballXVelocity *= -1;
ballYVelocity = Math.floor(Math.random() * 2) % 2 == 0 ? 1 : -1;
}
function computerMovement() {
if (pad2Y + (padHeight / 2) < ballY - ballSize * computerRate) {
pad2Y += padSpeedIncrement;
} else if (pad2Y + (padHeight / 2) > ballY + ballSize * computerRate) {
pad2Y -= padSpeedIncrement;
}
}
function drawText(text, x, y, color = "white") {
canvasContext.fillStyle = color;
canvasContext.font = "20px Arial";
canvasContext.fillText(text, x, y);
}
function drawRect(x, y, w, h, color = "white") {
canvasContext.fillStyle = color;
canvasContext.fillRect(x, y, w, h);
}
function drawCircle(x, y, radius, color = "white") {
canvasContext.fillStyle = color;
canvasContext.beginPath();
canvasContext.arc(x, y, radius, 0, Math.PI * 2, true);
canvasContext.fill();
}
function keyPush() {
switch (event.keyCode) {
// case 87: // w
// pad1Speed = -padSpeedIncrement;
// break;
// case 83: // s
// pad1Speed = padSpeedIncrement;
// break;
case 38: // UP
pad1Speed = -padSpeedIncrement;
break;
case 40: // DOWN
pad1Speed = padSpeedIncrement;
break;
}
}
function keyUp() {
pad1Speed = 0;
}
</script>
</body>
</html>