-
Notifications
You must be signed in to change notification settings - Fork 0
/
PONG.html
176 lines (156 loc) · 5.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pong - Kamil Jozwiak</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css">
<script src="modernizr.js"></script>
<script>
var ctx;
var screen;
var leftPaddle;
var rightPaddle;
var ball;
var score;
var mainElements;
var PaddleMoveAmount = 30;
var sounds;
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
function canvasSupport() {
return Modernizr.canvas;
};
var keyPress = {
rightUP: 38, // up arrow
rightDOWN: 40, // down arrow
leftUP: 88, // x (up)
leftDOWN: 90 // z (down)
};
function mainDraw() {
requestAnimationFrame(mainDraw);
mainElements.forEach(function(element) {
element.draw();
});
}
function paddlePress(e) {
e = e || window.event;
if (e.keyCode == keyPress.rightUP && rightPaddle.y > 0 + PaddleMoveAmount) {
rightPaddle.y -= PaddleMoveAmount;
} else if (e.keyCode == keyPress.rightDOWN && rightPaddle.y < canvas.height - rightPaddle.height - PaddleMoveAmount) {
rightPaddle.y += PaddleMoveAmount;
} else if (e.keyCode == keyPress.leftUP && leftPaddle.y > 0 + PaddleMoveAmount) {
leftPaddle.y -= PaddleMoveAmount;
} else if (e.keyCode == keyPress.leftDOWN && leftPaddle.y < canvas.height - leftPaddle.height - PaddleMoveAmount) {
leftPaddle.y += PaddleMoveAmount;
}
}
function init() {
if (!canvasSupport()) {
document.write("Your Browser doesn't support Canvas, please download the latest Mozilla Firefox Browser!");
} else {
var canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
requestAnimationFrame(mainDraw);
window.onkeydown = paddlePress;
screen = {
draw: function() {
ctx.fillStyle = "#000000";
ctx.fillRect (0, 0, canvas.width, canvas.height);
}
};
leftPaddle = {
x: 20,
y: 250,
width: 5,
height: 75,
draw: function() {
ctx.fillStyle = "#FFFFFF";
ctx.fillRect (this.x, this.y, this.width, this.height);
}
};
rightPaddle = {
x: 775,
y: 250,
width: 5,
height: 75,
draw: function() {
ctx.fillStyle = "#FFFFFF";
ctx.fillRect (this.x, this.y, this.width, this.height);
}
};
ball = {
x: canvas.width / 2,
y: canvas.height / 2,
speed: 5,
angle: 50,
radius: 8,
draw: function() {
var randomSide = Math.round(Math.random() * 1);
var radians = this.angle * Math.PI / 180;
var xunits = Math.cos(radians) * this.speed;
var yunits = Math.sin(radians) * this.speed;
this.x += xunits;
this.y += yunits;
if (this.x > canvas.width) {
sounds.playerScoreSound.play();
score.player1 += 1;
this.x = canvas.width / 2;
this.y = canvas.height / 2;
} else if (this.x < 0) {
sounds.playerScoreSound.play();
score.player2 += 1;
this.x = canvas.width / 2;
this.y = canvas.height / 2;
this.angle = 360 - this.angle;
} else if (this.y > canvas.height || this.y < 0) {
sounds.hittingWallSound.play();
this.angle = 360 - this.angle;
} else if (this.y < leftPaddle.y + leftPaddle.height && this.y > leftPaddle.y && this.x < leftPaddle.x + this.radius) {
sounds.hitPaddleSound.play();
this.angle = 180 - this.angle;
} else if (this.y < rightPaddle.y + rightPaddle.height && this.y > rightPaddle.y && this.x > rightPaddle.x - this.radius) {
sounds.hitPaddleSound.play();
this.angle = 180 - this.angle;
}
ctx.beginPath();
ctx.arc (this.x, this.y, this.radius, 0, Math.PI * 2, true);
ctx.fillStyle = "#FFFFFF";
ctx.fill ();
ctx.closePath ();
}
};
score = {
player1: 0,
player2: 0,
draw: function() {
ctx.font = "35px Arial sans-serif";
ctx.textAlign = "center";
ctx.fillText (this.player1, 70, 30);
ctx.fillText (this.player2, 730, 30);
}
};
mainElements = [
screen,
leftPaddle,
rightPaddle,
ball,
score
];
sounds = {
hittingWallSound: new Audio ("sounds/hitWall.wav"),
hitPaddleSound: new Audio ("sounds/hitPaddle.wav"),
playerScoreSound: new Audio ("sounds/playerScore.wav")
};
}
};
</script>
</head>
<body onload="init();">
<div id="instructions">
<b>Left Paddle Down:</b> Z - <b>Left Paddle Up:</b> X - <b>Right Paddle Down:</b> ↓ - <b>Right Paddle UP:</b> ↑
</div>
<div id="centerCanvas">
<canvas id="canvas" width="800" height="600"></canvas>
</div>
</body>
</html>