-
Notifications
You must be signed in to change notification settings - Fork 0
/
Isabel's File.html
342 lines (307 loc) · 9.47 KB
/
Isabel's File.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pong</title>
</head>
<body>
</body>
</html><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Gamedev Canvas Workshop</title>
<style>
* { padding: 0; margin: 0; }
canvas { background: rgba(3, 3, 3, 0.93); display: block; margin: 0 auto; }
</style>
</head>
<body>
<canvas id="myCanvas" width="640" height="420"></canvas>
<script>
//canvas
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
//ball
var ballRadius = 10;
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 4;
var dy = -4;
//paddle1
var paddle1Height = 10;
var paddle1Width = 76;
var paddle1X = 5;
var paddle1Y = ((canvas.height-paddle1Height) / 2) - 45;
var up1Pressed = false;
var down1Pressed = false;
//paddle2
var paddle2Height = 10;
var paddle2Width = 76;
var paddle2X = canvas.width - 15;
var paddle2Y = ((canvas.height-paddle2Height) / 2) - 45;
var upPressed = false;
var downPressed = false;
//score/rounds
var score = 0;
var score1 = 0;
var rounds = 1;
//Paddle 1 (left)
document.addEventListener("keydown", keyDownHandler1, false);
document.addEventListener("keyup", keyUpHandler1, false);
//Paddle 2 (right)
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
//paddle1
function keyDownHandler1(e) {
if(e.keyCode == '87') {
up1Pressed = true;
}
else if(e.keyCode == '83') {
down1Pressed = true;
}
}
function keyUpHandler1(e) {
if(e.keyCode == '87') {
up1Pressed = false;
}
else if(e.keyCode == '83') {
down1Pressed = false;
}
}
//paddle2
function keyDownHandler(e) {
if(e.keyCode == '38') {
upPressed = true;
}
else if(e.keyCode == '40') {
downPressed = true;
}
}
function keyUpHandler(e) {
if(e.keyCode == '38') {
upPressed = false;
}
else if(e.keyCode == '40') {
downPressed = false;
}
}
function drawLines() {
ctx.beginPath();
ctx.rect(canvas.width/2, 3, 3, 24);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(canvas.width/2, 53, 3, 24);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(canvas.width/2, 103, 3, 24);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(canvas.width/2, 153, 3, 24);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(canvas.width/2, 203, 3, 24);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(canvas.width/2, 253, 3, 24);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(canvas.width/2, 303, 3, 24);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(canvas.width/2, 353, 3, 24);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(canvas.width/2, 403, 3, 24);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
}
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI*2);
ctx.fillStyle = "#fffd49";
ctx.fill();
ctx.closePath();
}
function drawPaddle1() {
ctx.beginPath();
ctx.rect(paddle1X, paddle1Y, paddle1Height, paddle1Width);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(paddle2X, paddle2Y, paddle2Height, paddle2Width);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
}
function drawPaddle2() {
ctx.beginPath();
ctx.rect(paddle2X, paddle2Y, paddle2Height, paddle2Width);
ctx.fillStyle = "#FFF";
ctx.fill();
ctx.closePath();
}
function drawScore() {
ctx.font = "16px Arial";
ctx.fillStyle = "#FFF";
ctx.fillText("Score P1: "+score, 8, 20);
}
function drawScore1() {
ctx.font = "16px Arial";
ctx.fillStyle = "#FFF";
ctx.fillText("Score P2: "+score1, canvas.width-100, 20);
}
function drawLives() {
ctx.font = "16px Arial";
ctx.fillStyle = "#FFF";
ctx.fillText("Round #"+ rounds, canvas.width/3, 20);
}
function drawFace() {
if(dx > 0) {
var eye1 = x - 2;
var eye2 = x + 5;
var mouthX = x-4;
var mouthY = y+4;
} else {
var eye1 = x - 5;
var eye2 = x + 2;
var mouthX = x-6;
var mouthY = y+4;
}
ctx.beginPath();
ctx.arc(eye1, y-2, 2, 0, Math.PI*2);
ctx.fillStyle = "#000";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.arc(eye2, y-2, 2, 0, Math.PI*2);
ctx.fillStyle = "#000";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.rect(mouthX, mouthY, 10, 1);
ctx.fillStyle = "#000";
ctx.fill();
ctx.closePath();
}
//hi just using this to push
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle1();
drawPaddle2();
drawScore();
drawScore1();
drawLives();
drawFace();
drawLines();
if(x + dx > canvas.width-ballRadius - 10 || x + dx < ballRadius + 12) {
if (y > (paddle1Y) && y < (paddle1Y + 34) && dx < 0) {
dx = Math.floor(Math.random() * 7) + 3;
dy = -4;
document.body.style.backgroundColor = "cornflowerblue";
}
else if (y < (paddle1Y + paddle1Width) && y > (paddle1Y + 38) && dx < 0) {
dx = Math.floor(Math.random() * 7) + 3;
dy = 4;
document.body.style.backgroundColor = "blue";
}
else if (y <= (paddle1Y + 38) && y >= (paddle1Y + 34) && dx < 0) {
dx = Math.floor(Math.random() * 7) + 3;
dy = 0;
document.body.style.backgroundColor = "white";
}
else if (y > (paddle2Y) && y < (paddle2Y + 34)&& dx > 0) {
dx = -Math.floor(Math.random() * 7) - 3;
dy = -4;
document.body.style.backgroundColor = "orchid";
}
else if (y < (paddle2Y + paddle2Width) && y > (paddle2Y + 38) && dx > 0) {
dx = -Math.floor(Math.random() * 7) - 3;
dy = 4;
document.body.style.backgroundColor = "aqua";
}
else if (y <= (paddle2Y + 38) && y >= (paddle2Y + 34) && dx > 0) {
dx = -Math.floor(Math.random() * 7) - 3;
dy = 0;
document.body.style.backgroundColor = "coral";
}
// remember to add separate if functions for x +dx > canvas.width - balRadius and x + dx < ballRadius for different scores
else {
if (x + dx > canvas.width-ballRadius) {
score++;
rounds++;
x = canvas.width / 2;
y = canvas.height - 30;
dx = 4;
dy = -4;
paddle1Y = (canvas.height - paddle1Width) / 2;
paddle2Y = (canvas.height - paddle2Width) / 2;
document.body.style.backgroundColor = "black";
}
else if (x + dx < ballRadius) {
score1++;
rounds++;
x = canvas.width / 2;
y = canvas.height - 30;
dx = 4;
dy = -4;
paddle1Y = (canvas.height - paddle1Width) / 2;
paddle2Y = (canvas.height - paddle2Width) / 2;
document.body.style.backgroundColor = "black";
}
if (score == 6||score1==6) {
if (score>score1) {
alert("Player 1 has won!\nPlayer 1: " + score + "\nPlayer 2: " + score1);
document.location.reload();
}
else{
alert("Player 2 has won!\nPlayer 1: "+score+"\nPlayer 2: "+score1);
document.location.reload();
}
}
}
}
if(y + dy < ballRadius || y + dy > canvas.height-ballRadius) {
dy = -dy;
}
//paddle1
if(up1Pressed && paddle1Y > 5) {
paddle1Y -= 7;
}
else if(down1Pressed && paddle1Y+paddle1Height + 70 < canvas.height) {
paddle1Y += 7;
}
//paddle2
if(upPressed && paddle2Y > 5) {
paddle2Y -= 7;
}
else if(downPressed && paddle2Y+paddle2Height + 70 < canvas.height) {
paddle2Y += 7;
}
x += dx;
y += dy;
requestAnimationFrame(draw);
}
draw();
</script>
</body>
</html>