-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPONG.html
198 lines (167 loc) · 5.87 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pong Game</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #000;
}
canvas {
border: 2px solid #fff;
}
</style>
</head>
<body>
<canvas id="pong" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('pong');
const context = canvas.getContext('2d');
const net = {
x: canvas.width / 2 - 1,
y: 0,
width: 2,
height: 10,
color: "#FFF"
};
const user = {
x: 0,
y: canvas.height / 2 - 50,
width: 10,
height: 100,
color: "#008542",
score: 0,
dy: 0
};
const com = {
x: canvas.width - 10,
y: canvas.height / 2 - 50,
width: 10,
height: 100,
color: "#E70001",
score: 0
};
const ball = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 10,
speed: 5,
velocityX: 5,
velocityY: 5,
color: "#FFE000"
};
function drawRect(x, y, w, h, color) {
context.fillStyle = color;
context.fillRect(x, y, w, h);
}
function drawCircle(x, y, r, color) {
context.fillStyle = color;
context.beginPath();
context.arc(x, y, r, 0, Math.PI * 2, false);
context.closePath();
context.fill();
}
function drawText(text, x, y, color) {
context.fillStyle = color;
context.font = "45px Arial";
context.fillText(text, x, y);
}
function drawNet() {
for (let i = 0; i <= canvas.height; i += 15) {
drawRect(net.x, net.y + i, net.width, net.height, net.color);
}
}
function render() {
drawRect(0, 0, canvas.width, canvas.height, "#000");
drawNet();
drawText(user.score, canvas.width / 4, canvas.height / 5, "#FFF");
drawText(com.score, 3 * canvas.width / 4, canvas.height / 5, "#FFF");
drawRect(user.x, user.y, user.width, user.height, user.color);
drawRect(com.x, com.y, com.width, com.height, com.color);
drawCircle(ball.x, ball.y, ball.radius, ball.color);
}
function movePaddleWithMouse(event) {
let rect = canvas.getBoundingClientRect();
user.y = event.clientY - rect.top - user.height / 2;
}
function movePaddleWithKeys(event) {
switch(event.key) {
case "ArrowUp":
user.dy = -7;
break;
case "ArrowDown":
user.dy = 7;
break;
}
}
function stopPaddle(event) {
switch(event.key) {
case "ArrowUp":
case "ArrowDown":
user.dy = 0;
break;
}
}
canvas.addEventListener("mousemove", movePaddleWithMouse);
document.addEventListener("keydown", movePaddleWithKeys);
document.addEventListener("keyup", stopPaddle);
function collision(b, p) {
b.top = b.y - b.radius;
b.bottom = b.y + b.radius;
b.left = b.x - b.radius;
b.right = b.x + b.radius;
p.top = p.y;
p.bottom = p.y + p.height;
p.left = p.x;
p.right = p.x + p.width;
return b.right > p.left && b.bottom > p.top && b.left < p.right && b.top < p.bottom;
}
function resetBall() {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
ball.velocityX = -ball.velocityX;
ball.speed = 5;
}
function update() {
ball.x += ball.velocityX;
ball.y += ball.velocityY;
user.y += user.dy;
if (user.y < 0) user.y = 0;
if (user.y + user.height > canvas.height) user.y = canvas.height - user.height;
com.y += (ball.y - (com.y + com.height / 2)) * 0.1;
if (ball.y + ball.radius > canvas.height || ball.y - ball.radius < 0) {
ball.velocityY = -ball.velocityY;
}
let player = (ball.x < canvas.width / 2) ? user : com;
if (collision(ball, player)) {
let collidePoint = (ball.y - (player.y + player.height / 2));
collidePoint = collidePoint / (player.height / 2);
let angleRad = (Math.PI / 4) * collidePoint;
let direction = (ball.x < canvas.width / 2) ? 1 : -1;
ball.velocityX = direction * ball.speed * Math.cos(angleRad);
ball.velocityY = ball.speed * Math.sin(angleRad);
ball.speed += 0.5; // Incremento de velocidad de 0.1 unidades cada vez que la bola rebota en una paleta
}
if (ball.x - ball.radius < 0) {
com.score++;
resetBall();
} else if (ball.x + ball.radius > canvas.width) {
user.score++;
resetBall();
}
}
function game() {
update();
render();
}
const framePerSecond = 50;
setInterval(game, 1000 / framePerSecond);
</script>
</body>
</html>