-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdragonbars.html
More file actions
360 lines (309 loc) · 8.31 KB
/
Copy pathdragonbars.html
File metadata and controls
360 lines (309 loc) · 8.31 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<!DOCTYPE html>
<html>
<head>
<title>Dragon Bars</title>
<h4>Press space to pause!</h4>
</style>
</head>
<body>
<h1 class="Ella" id="title">Dragon Bars!</h1>
<button onclick="myFunction()"> How to Play</button>
<script>
function myFunction() {
alert("Hello! Are you ready to play Dragon Bars? Enter your nickname. You are a water dragon.");
alert("All of your dragons friends are in dragon jail. Break them out to earn new dragon types!");
alert("If you free all of the dragons, you unlock the next dragon type.");
alert("llORDER OF UNLOCKED DRAGONSll *water *fire *air *earth *light *nature *shadow *ice *lightning ");
}
</script>
<input id="nickNameInput" placeholder="Enter your dragon's name!"></input>
<button class="someMargin" onclick="startClicked()">START</button>
<input id="ballColorInput" placeholder="Enter your dragon type!"></input>
<canvas id="canvas"></canvas>
<h2 class="Ella" id="score">SCORE:0
<div id="highScores"></div>
</body>
</html>
<script>
function myFunction() {
alert("Hello! Are you ready to play Dragon Bars? Enter your nickname. You are a water dragon.");
alert("All of your dragons friends are in dragon jail. Break them out to earn new dragon types!");
alert("If you free all of the dragons, you unlock the next dragon type.");
alert("llORDER OF UNLOCKED DRAGONSll *water *fire *air *earth *light *nature *shadow *ice *lightning ");
}
</script>
<input id="nickNameInput" placeholder="Enter your name!"></input>
<button class="someMargin" onclick="startClicked()">START</button>
<input id="ballColorInput" placeholder="Enter your ball color!"></input>
<canvas id="canvas"></canvas>
<h2 class="Ella" id="score">SCORE:0
<div id="highScores"></div>
</body>
</html>
<style>
body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
background-color:blue;
font-family: monospace;
}
canvas {
border: 1px solid aliceblue;
display: block;
border-bottom: 1px solid aliceblue;
background-image:url(https://physicsworld.com/wp-content/uploads/2013/09/clouds-3.jpg);
}
#title{
text-align: center;
width:100%;
height:75px;
line-height:75px;
font-size: 50px;
background-color: aqua;
}
.someMargin {
margin-bottom: 30px;
}
</style>
<script>
var CANVAS_SIZE = 300;
var BLOCK_WIDTH = 50;
var BLOCK_HEIGHT = 20;
var PADDLE_WIDTH = 100;
var PADDLE_HEIGHT = 5;
var SCORE_ADD = 10;
var TITLE = 'Dragon Bars!';
var PAUSED = 'Paused';
var GAME_OVER = 'You were captured...';
var YOU_WIN = 'You freed them all!';
var SCORE = 'Dragons freed: ';
var LEFT_ARROW = 37;
var RIGHT_ARROW = 39;
var SPACE_BAR = 32;
var R_KEY = 82;
var score = 0;
var highScores = [];
var nickname = [];
var ballColor = [];
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.canvas.width = CANVAS_SIZE;
ctx.canvas.height = CANVAS_SIZE;
function startClicked(){
nickname=document.getElementById('nickNameInput').value;
ballColor=document.getElementById('ballColorInput').value;
paused=false;
}
function newBall() {
return {
position: {
x: 10,
y: 100
},
velocity: { // in pixels per second
x: 100,
y: 150
},
time: Number(new Date()),
size: 10
}
}
var ball = newBall()
function newBlocks() {
return [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5],
[1, 0], [1, 1], [1, 2], [1, 3], [1, 4], [1, 5],
[2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5],
[3, 0], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5],
[4, 0], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5]];
}
var blocks = newBlocks();
var paddle = {
position: {
x: 100,
y: 250
},
speed: 15,
shouldMoveLeft: false,
shouldMoveRight: false
};
var paused = true;
function clear() {
ctx.clearRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);
}
function drawFrame() {
clear();
drawBall();
drawBlocks();
drawPaddle();
drawScore();
}
function bound(coord, lower, upper) {
return Math.max(lower, Math.min(coord, upper));
}
function drawBall() {
var now = Number(new Date());
if (paused) {
ball.time += (now - paused)
paused = now
}
var secondsPassed = (now - ball.time) / 1000;
var dx = secondsPassed * ball.velocity.x;
var dy = secondsPassed * ball.velocity.y;
var x = ball.position.x;
var y = ball.position.y;
ctx.beginPath();
ctx.rect(x + dx, y + dy, ball.size, ball.size);
ctx.fillStyle = ballColor;
ctx.fill();
ctx.stroke();
var shouldBounceLeft = x + dx + ball.size > CANVAS_SIZE;
var shouldBounceRight = x + dx < 0;
if (shouldBounceLeft || shouldBounceRight) {
ball.position.x = bound(x + dx, 0, CANVAS_SIZE - ball.size);
ball.position.y = y + dy;
ball.time = now;
ball.velocity.x = -ball.velocity.x;
}
var shouldBounceUp = y + dy + ball.size > CANVAS_SIZE;
var shouldBounceDown = y + dy < 0;
if (shouldBounceUp || shouldBounceDown ||
didBreakBlock(x + dx, y + dy) || didHitPaddle(x + dx, y + dy)) {
ball.position.x = x + dx;
ball.position.y = bound(y + dy, 0, CANVAS_SIZE - ball.size);
ball.time = now;
ball.velocity.y = -ball.velocity.y;
}
if (shouldBounceUp) {
endGame(GAME_OVER);
}
}
function drawBlocks() {
var blocksLeft = 0;
for (var i = 0; i < blocks.length; i++) {
var block = blocks[i];
if (block) {
blocksLeft += 1;
var x = block[1] * BLOCK_WIDTH;
var y = block[0] * BLOCK_HEIGHT;
ctx.beginPath();
ctx.rect(x, y, BLOCK_WIDTH, BLOCK_HEIGHT);
ctx.fillStyle = pickFillColor(block[0]);
ctx.fill();
ctx.stroke();
}
}
if (blocksLeft === 0) {
endGame(YOU_WIN);
}
}
function drawScore() {
document.getElementById('score').innerHTML = SCORE + score;
}
function didBreakBlock(ballX, ballY) {
for (var i = 0; i < blocks.length; i++) {
var block = blocks[i];
if (block) {
var blockX = block[1] * BLOCK_WIDTH;
var blockY = block[0] * BLOCK_HEIGHT;
if (blockX < ballX && ballX < blockX + BLOCK_WIDTH &&
blockY > ballY - BLOCK_HEIGHT) {
blocks[i] = null;
score += SCORE_ADD;
return true;
}
}
}
return false;
}
function pickFillColor(n) {
if (n === 0) {
return 'darkBLue';
} else if (n === 1) {
return 'blue';
} else if (n === 2) {
return 'dodgerBlue';
} else if(n === 3) {
return 'deepSkyBlue';
}else if(n===4){
return 'cyan';
}
}
function drawPaddle() {
if (paddle.shouldMoveLeft) {
paddle.position.x -= paddle.speed;
paddle.shouldMoveLeft = false;
} else if (paddle.shouldMoveRight) {
paddle.position.x += paddle.speed;
paddle.shouldMoveRight = false;
}
ctx.beginPath();
ctx.rect(paddle.position.x, paddle.position.y, PADDLE_WIDTH, PADDLE_HEIGHT);
ctx.fillStyle = 'white';
ctx.fill();
ctx.stroke();
}
function didHitPaddle(ballX, ballY) {
return paddle.position.x < ballX &&
ballX < paddle.position.x + PADDLE_WIDTH &&
paddle.position.y < ballY + ball.size &&
ball.velocity.y > 0;
}
function endGame(message) {
if (window.title.innerHTML === TITLE) {
ball.velocity.y = 0
ball.velocity.x = 0
window.title.innerHTML = message;
highScores += score;
var el = document.createElement('h2');
el.innerHTML = score;
document.getElementById('highScores').appendChild(el);
}
}
// canvas.onmousemove = function (e) {
// var r = canvas.getBoundingClientRect();
// var x = e.clientX - r.x;
// var y = e.clientY - r.y;
// paddle.position.x = x
// paddle.position.y = y
// }
document.onkeydown = function(e) {
switch (e.keyCode) {
case LEFT_ARROW:
paddle.shouldMoveLeft = true;
break;
case RIGHT_ARROW:
paddle.shouldMoveRight = true;
break;
case SPACE_BAR:
var title = window.title.innerHTML;
if (title === TITLE) {
window.title.innerHTML = PAUSED;
paused = Number(new Date())
} else {
window.title.innerHTML = TITLE;
paused = false;
}
break;
case R_KEY:
window.title.innerHTML = TITLE;
ball = newBall();
blocks = newBlocks();
score = 0;
break;
}
};
var motion = (function () {
var i = 0;
return {
start: () => {
i = setInterval(drawFrame, 10);
},
stop: () => {
clearInterval(i);
}
}
})();
motion.start();
</script>