-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlyssaIsConfused.html
252 lines (222 loc) · 6.64 KB
/
AlyssaIsConfused.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
<!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 diameter = ballRadius * 2;
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 2;
var dy = -2;
//paddle1
var paddle1Height = 10;
var paddle1Width = 75;
var paddle1X = 5;
var paddle1Y = ((canvas.height-paddle1Height) / 2) - 45;
var up1Pressed = false;
var down1Pressed = false;
//paddle2
var paddle2Height = 10;
var paddle2Width = 75;
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 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 = "#0010dd";
ctx.fill();
ctx.closePath();
}
function drawPaddle2() {
ctx.beginPath();
ctx.rect(paddle2X, paddle2Y, paddle2Height, paddle2Width);
ctx.fillStyle = "#0010dd";
ctx.fill();
ctx.closePath();
}
function drawScore() {
ctx.font = "16px Arial Black";
ctx.fillStyle = "#ff000b";
ctx.fillText("Score P1: "+score, 8, 20);
}
function drawScore1() {
ctx.font = "16px Arial Black";
ctx.fillStyle = "#ff000b";
ctx.fillText("Score P2: "+score1, canvas.width-100, 20);
}
function drawLives() {
ctx.font = "16px Arial Black";
ctx.fillStyle = "#ff000b";
ctx.fillText("Round #"+ rounds, canvas.width/2 - 35, 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-7;
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();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle1();
drawPaddle2();
drawScore();
drawScore1();
drawLives();
drawFace();
if(x + dx > canvas.width-ballRadius - 10 || x + dx < ballRadius + 12) {
if (y > paddle1Y && y < paddle1Y + paddle1Width && dx < 0) {
dx = -dx;
}
else if (y > paddle2Y && y < paddle2Y + paddle2Width && dx > 0) {
dx = -dx;
}
// remember to add separate if functions for x +dx > canvas.width - balRadius and x + dx < ballRadius for different scores
else {
if (score == 6||score1==6) {
if (score>score1) {
score++;
alert("Player 1 has won!\nPlayer 1: " + score + "\nPlayer 2: " + score1);
document.location.reload();
}
else{
score ++;
alert("Player 2 has won!\nPlayer 1: "+score+"\nPlayer 2: "+score1);
document.location.reload();
}
}
else if (x + dx > canvas.width-ballRadius) {
score++;
rounds++;
x = canvas.width / 2;
y = canvas.height - 30;
dx = 2;
dy = -2;
paddle1Y = (canvas.height - paddle1Width) / 2;
paddle2Y = (canvas.height - paddle2Width) / 2;
}
else if (x + dx < ballRadius) {
score1++;
rounds++;
x = canvas.width / 2;
y = canvas.height - 30;
dx = 2;
dy = -2;
paddle1Y = (canvas.height - paddle1Width) / 2;
paddle2Y = (canvas.height - paddle2Width) / 2;
}
}
}
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>