-
Notifications
You must be signed in to change notification settings - Fork 7
/
random-balls.html
75 lines (56 loc) · 1.47 KB
/
random-balls.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
<!DOCTYPE html>
<html>
<head>
<title>Pong.js - Shrink player</title>
<script type="text/javascript" src="../build/Pong.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript">
window.onload = function () {
var pong = new Pong(document.getElementById('pong'));
function resize () {
var gameHeight = window.innerHeight - 40 + 'px';
document.getElementById('pong').style.height = gameHeight;
pong.resize();
}
resize();
window.onresize = resize;
pong.players.a.addControls({
'up': 'up',
'down': 'down',
});
pong.players.b.speed = 280;
pong.on('update', function () {
if (pong.balls.length) {
if (pong.players.b.y < pong.balls[0].y) {
pong.players.b.move(1);
} else if (pong.players.b.y > pong.balls[0].y) {
pong.players.b.move(-1);
}
}
});
function randRange (min, max) {
return Math.random() * (max - min) + min;
}
function addBall () {
pong.addBall();
pong.balls[pong.balls.length - 1].setSize(randRange(5, 30));
}
pong.on('start', function () {
pong.balls[pong.balls.length - 1].setSize(randRange(5, 30));
});
pong.players.a.on('hit', function () {
addBall();
});
pong.players.b.on('hit', function () {
addBall();
});
};
</script>
</head>
<body>
<div id="pong"></div>
<div class="panel">
Move with [ UP ], [ DOWN ], score points to get the enemy's paddle to shrink
</div>
</body>
</html>