-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.html
171 lines (153 loc) · 5.64 KB
/
pong.html
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
<html>
<head>
<title>Jason's pong game</title>
<style>
div { position: absolute; transition: 0.025s linear; }
#board {
border: 4px solid #444;
top: 10px;
left: 10px;
width: 800px;
height: 600px;
}
#ball {
background: black;
width: 16px;
height: 16px;
top: 400px;
left: 300px;
border-radius: 50%;
}
.paddle {
background: blue;
width: 20px;
height: 140px;
border-radius: 8px;
}
#paddle1 { top: 100px; left: 20px; }
#paddle2 { top: 200px; left: 784px; }
</style>
</head>
<body>
<div id="board"></div>
<div id="ball"></div>
<div id="paddle1" class="paddle"></div>
<div id="paddle2" class="paddle"></div>
<script>
// Ball location
let ballX = 400, ballY = 300;
let ballSpeed = 3;
let ballRadians = 0.1;
const ballSize = 16;
const board = document.getElementById('board');
const ball = document.getElementById('ball');
const paddle1 = document.getElementById('paddle1');
let paddle1y = 100;
let paddle1angle = 0;
const paddle2 = document.getElementById('paddle2');
let paddle2y = 200;
let paddle2angle = 0;
let paddle1Height = 140;
let paddle2Height = 140;
const paddleWidth = 20;
// board boundaries
const boardRect = board.getBoundingClientRect();
function rotatePaddle1(delta) {
paddle1angle = Math.max(-0.25, Math.min(0.25, paddle1angle + delta));
paddle1.style.transform = "rotate(" + paddle1angle + "rad)";
}
function rotatePaddle2(delta) {
paddle2angle = Math.max(-0.25, Math.min(0.25, paddle2angle + delta));
paddle2.style.transform = "rotate(" + paddle2angle + "rad)";
}
document.addEventListener('keydown', idKey);
function idKey(e) {
// Player 1 uses WASD keys.
if (e.key == "d") {
rotatePaddle1(0.02);
}
else if (e.key == "a") {
rotatePaddle1(-0.02);
}
else if (e.key == "w") {
paddle1y = Math.max(boardRect.top + 5, paddle1y - 10);
paddle1.style.top = paddle1y + "px";
}
else if (e.key == "s") {
paddle1y = Math.min(boardRect.bottom - 5 - paddle1Height, paddle1y + 10);
paddle1.style.top = paddle1y + "px";
}
// Player 2 uses arrow keys
else if (e.code == "ArrowRight") {
rotatePaddle2(0.02);
}
else if (e.code == "ArrowLeft") {
rotatePaddle2(-0.02);
}
else if (e.code == "ArrowUp") {
paddle2y = Math.max(boardRect.top + 5, paddle2y - 10);
paddle2.style.top = paddle2y + "px";
}
else if (e.code == "ArrowDown") {
paddle2y = Math.min(boardRect.bottom - 5 - paddle2Height, paddle2y + 10);
paddle2.style.top = paddle2y + "px";
}
else {
console.log("wrong key");
console.log(e);
}
}
function checkCollision(paddleEl, newX, newY) {
const paddleRect = paddleEl.getBoundingClientRect();
return (newY + ballSize >= paddleRect.top &&
newY <= paddleRect.bottom &&
newX + ballSize >= paddleRect.left &&
newX <= paddleRect.right);
}
function moveBallLoop() {
// Think about where the ball would be after this movement.
let dx = ballSpeed * Math.cos(ballRadians);
let dy = ballSpeed * Math.sin(ballRadians);
let newX = ballX + dx;
let newY = ballY + dy;
let newBallRadians = ballRadians;
// Bounce off of each paddle
if (checkCollision(paddle1, newX, newY)) {
newBallRadians = Math.PI - ballRadians + (paddle1angle * 2);
ballSpeed = ballSpeed + 0.1;
paddle1Height = Math.max(20, paddle1Height - 5);
paddle1.style.height = paddle1Height + 'px';
rotatePaddle1(0.1 * ((newY - paddle1y - (paddle1Height/2)) / paddle1Height));
}
else if (checkCollision(paddle2, newX, newY)) {
newBallRadians = Math.PI - ballRadians + (paddle2angle * 2);
ballSpeed = ballSpeed + 0.1;
paddle2Height = Math.max(20, paddle2Height - 5);
paddle2.style.height = paddle2Height + 'px';
rotatePaddle2(-0.1 * ((newY - paddle2y - (paddle2Height/2)) / paddle2Height));
}
// Bounce off the walls of the board.
else if (newX < boardRect.left || newX + ballSize > boardRect.right) {
newBallRadians = Math.PI - ballRadians;
ballSpeed = 3;
paddle1Height = 140;
paddle1.style.height = paddle1Height + 'px';
paddle2Height = 140;
paddle2.style.height = paddle2Height + 'px';
}
else if (newY < boardRect.top || newY + ballSize > boardRect.bottom) {
newBallRadians = 0 - ballRadians;
}
// Once we have computed the new angle, save that and update the elements.
ballRadians = newBallRadians;
dx = ballSpeed * Math.cos(ballRadians);
dy = ballSpeed * Math.sin(ballRadians);
ballX = ballX + dx;
ballY = ballY + dy;
ball.style.left = newX + "px";
ball.style.top = newY + "px";
}
setInterval(moveBallLoop, 25);
</script>
</body>
</html>