Skip to content

Commit ad3c358

Browse files
authored
Merge pull request #134 from vclayton/pong-vclayton-branch
Super-basic pong game
2 parents 5765918 + 75da3d0 commit ad3c358

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

Pong/vclayton/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Simple Pong</title>
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<link rel="stylesheet" href="./style.css">
8+
</head>
9+
<body>
10+
<div class="main">
11+
<canvas id="game">
12+
</canvas>
13+
</div>
14+
<script src="pong.js"></script>
15+
</body>

Pong/vclayton/pong.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
const canvas = document.getElementById('game');
3+
const ctx = canvas.getContext('2d');
4+
5+
let maxX = canvas.width;
6+
let maxY = canvas.height;
7+
8+
let paddleWidth = 10;
9+
let paddleHeight = 35;
10+
let maxPaddleSpeed = .75;
11+
// All paddles need to store is y position
12+
let paddle1 = Math.floor(maxY / 2);
13+
let paddle2 = paddle1;
14+
let points1 = 0;
15+
let points2 = 0;
16+
let ballX = Math.floor(maxX / 2);
17+
let ballY = Math.floor(maxY / 2);
18+
let ballVx = 1;
19+
let ballVy = 1;
20+
21+
function render() {
22+
ctx.fillStyle = "#444444";
23+
ctx.fillRect(0, 0, canvas.width, canvas.height);
24+
25+
ctx.fillStyle = "#cccccc";
26+
ctx.fillRect(ballX-5, ballY-5, 10, 10);
27+
28+
ctx.fillRect(10, paddle1 - paddleHeight/2, 10, paddleHeight);
29+
ctx.fillRect(280, paddle2 - paddleHeight/2, 10, paddleHeight);
30+
31+
ctx.fillText(points1, 5, 10);
32+
ctx.fillText(points2, 285, 10);
33+
}
34+
35+
function reset(full) {
36+
paddle2 = Math.floor(maxY / 2);
37+
ballX = Math.floor(maxX / 2);
38+
ballY = Math.floor(maxY / 2);
39+
ballVx = Math.random() > .5 ? 1 : -1;
40+
ballVy = 1;
41+
if (full) {
42+
points1 = 0;
43+
points2 = 0;
44+
}
45+
}
46+
47+
function clamp(val, min, max) {
48+
return Math.min(Math.max(val, min), max);
49+
}
50+
51+
function handleMouseMove(e) {
52+
let targetY = (e.pageY - canvas.offsetTop) * canvas.height / canvas.offsetHeight;
53+
paddle1 = targetY;
54+
}
55+
56+
function main() {
57+
paddle2 += clamp(ballY - paddle2, -maxPaddleSpeed, maxPaddleSpeed);
58+
59+
let nextX = ballX + ballVx;
60+
let nextY = ballY + ballVy;
61+
if (nextX < 0) {
62+
points2++;
63+
reset();
64+
} else if (nextX > maxX) {
65+
points1++;
66+
reset();
67+
} else if (nextY < 0 || nextY > maxY) {
68+
ballVy = -ballVy;
69+
}
70+
if (ballX < 20 && ballX > 10 && Math.abs(ballY - paddle1) < paddleHeight / 2) {
71+
ballVx = -ballVx * 1.1;
72+
ballVy += (Math.random()-0.5) * .1;
73+
}
74+
if (ballX < 290 && ballX > 280 && Math.abs(ballY - paddle2) < paddleHeight / 2) {
75+
ballVx = -ballVx * 1.1;
76+
ballVy += (Math.random()-0.5) * .1;
77+
}
78+
79+
ballX += ballVx;
80+
ballY += ballVy;
81+
82+
render();
83+
84+
85+
requestAnimationFrame(main);
86+
}
87+
88+
document.onmousemove = handleMouseMove;
89+
reset(true);
90+
main();

Pong/vclayton/style.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
html, body {
2+
height: 100%;
3+
}
4+
5+
.main {
6+
width: 95%;
7+
height: 95%;
8+
margin: 0 auto;
9+
border: 2px dotted grey;
10+
}
11+
12+
#game {
13+
width: 100%;
14+
height: 100%;
15+
}

0 commit comments

Comments
 (0)