-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weirder.html
192 lines (163 loc) · 4.67 KB
/
Weirder.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Gamedev Canvas Workshop</title>
<style>
* { padding: 0; margin: 0; }
canvas { background: #eee; 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 hey = Math.floor((Math.random() + 1) * 3);
var x = canvas.width/2;
var y = canvas.height/2;
var dx = hey;
var dy = -(hey) + (Math.random() + 1) * 3;
//paddle1
var paddle1Height = 10;
var paddle1Width = 75;
var paddle1X = 5;
var paddle1Y = ((canvas.height-paddle1Height) / 2) - 43;
var up1Pressed = false;
var down1Pressed = false;
//paddle2
var paddle2Height = 10;
var paddle2Width = 75;
var paddle2X = canvas.width - 15;
var paddle2Y = ((canvas.height-paddle2Height) / 2) - 43;
var upPressed = false;
var downPressed = false;
//score/lives
var score = 0;
var lives = 5;
//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 = "#0095DD";
ctx.fill();
ctx.closePath();
}
function drawPaddle1() {
ctx.beginPath();
ctx.rect(paddle1X, paddle1Y, paddle1Height, paddle1Width);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
function drawPaddle2() {
ctx.beginPath();
ctx.rect(paddle2X, paddle2Y, paddle2Height, paddle2Width);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
function drawScore() {
ctx.font = "16px Arial";
ctx.fillStyle = "#0095DD";
ctx.fillText("Score: "+score, 8, 20);
}
function drawLives() {
ctx.font = "16px Arial";
ctx.fillStyle = "#0095DD";
ctx.fillText("Rounds: "+lives, canvas.width-65, 20);
}
function collision(){
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
drawPaddle1();
drawPaddle2();
drawScore();
drawLives();
if(x + dx > canvas.width-ballRadius-10 || x + dx < ballRadius) {
if(y > paddle1Y && y < paddle1Y + paddle1Width) {
dx = -dx - ((Math.random() + 1) * 3);
}
else if (y > paddle2Y && y < paddle2Y + paddle2Width ) {
dx = -dx - ((Math.random() + 1) * 3);
}
// remember to add separate if functions for x +dx > canvas.width - balRadius and x + dx < ballRadius for different scores
else {
alert("GAME OVER");
document.location.reload();
x = canvas.width/2;
y = canvas.height-30;
dx = 3;
dy = -3;
paddleX = (canvas.width-paddleWidth)/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>