-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreakout.html
More file actions
413 lines (372 loc) · 19 KB
/
Breakout.html
File metadata and controls
413 lines (372 loc) · 19 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<!DOCTYPE html>
<html lang="eng" dir="ltr">
<head>
<meta charset="utf-8">
<title>Helicopter Helicopter</title>
<link href="index.css" type="text/css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript" src="https://static.codehs.com/gulp/80550f5fc4071985bfef352d04f005ca3de931d3/chs-js-lib/chs.js"></script>
</head>
<body>
<div class="body">
<header>
<h1>Game Design and Development Class </h1>
<nav>
<ul id="buttons">
<li><a href="index.html">Home</a></li>
</ul>
</nav>
</header>
<img
id="myImage3"
src="https://images.cooltext.com/5667329.png"
alt="samcon30" />
<! this is the code for my button at the bottom it also adds an id for formating with CSS
otherwise apon click it acts like a hyperlink and takes you to my youtbe channel>
<button id="button" onclick = "window.location.href='https://www.youtube.com/@Samcon30';"></button>
<canvas
width="400"
height="500"
class="codehs-editor-canvas"></canvas>
<pre id="console"></pre>
<script>
window.onload = function() {
setSize(1024, 768);
// Player
var SPEED = 6;
var copter;
var dy = 0;
var clicking = false;
var MAX_DY = 12;
//Canvas
var X = getWidth();
var Y = getHeight();
var CENTER_X = X/2;
var CENTER_Y = Y/2;
var HELICOPTER = new Text ("Helicopter Helicopter");
var GAME_START_TUTORIAL = new Text ("Press any button or mouse to start the Game.");
var TUTORIAL = new Text ("Hold down any button or mouse to fly up.");
//Functional Oddities
var windex;
var GAME_STARTED;
var TIMEOUTS = [];
//Game
var POINTS_PER_ROUND = 1;
var score;
var points=0;
//Obstacles
var obstacles = [];
var OBST_H = 120;
var OBST_W = 30;
var NUM_OBSTACLES = Number(prompt("How many Obstacles? (reccomended 6-12)"));
//Terrain
var TERRAIN_WIDTH = 10;
var MIN_TERRAIN_HEIGHT = 10;
var MAX_TERRAIN_HEIGHT = 85;
var top_terrain = [];
var bottom_terrain = [];
//Dust
var DUST_RADIUS = 3;
var DUST_BUFFER = 10;
var dust = [];
//Power-Up's
var POWER_UP_GO_SPEED = false;
var POWER_UP_GO_SLOW = false;
var PowSize = 50;
var PowerUpSpeed;
var PowerUpSlow;
function start(){
setup();
HELICOPTER.setColor(Color.blue);
HELICOPTER.setPosition(CENTER_X-HELICOPTER.getWidth()/2, Y/4);
add(HELICOPTER);
GAME_START_TUTORIAL.setColor(Color.blue);
GAME_START_TUTORIAL.setPosition(CENTER_X-GAME_START_TUTORIAL.getWidth()/2, (Y/4)*2);
add(GAME_START_TUTORIAL);
TUTORIAL.setColor(Color.blue);
TUTORIAL.setPosition(CENTER_X-TUTORIAL.getWidth()/2, (Y/4)*3);
add(TUTORIAL);
keyDownMethod(gameStart);
mouseDownMethod(gameStart);
}
function setup() {
GAME_STARTED = false;
setBackgroundColor(Color.black);
copter = new WebImage("https://codehs.com/uploads/fc441749f475620a34b8d3013d637177");
copter.setSize(50, 25);
copter.setColor(Color.blue);
copter.setPosition(X/3, CENTER_Y);
add(copter);
score=new Text("0");
score.setColor(Color.white);
score.setPosition(0+score.getWidth(), 0+MAX_TERRAIN_HEIGHT+score.getHeight());
score.layer = 20;
add(score);
PowerUpSpeed = new WebImage("https://codehs.com/uploads/65ca67d341ff692fbfa3f8de2a014730");
PowerUpSlow = new WebImage("https://codehs.com/uploads/b542389c840cb12667cd4a4b1d6803b6");
PowerUpSpeed.setSize(PowSize, PowSize);
PowerUpSlow.setSize(PowSize, PowSize);
PowerUpSpeed.setPosition(X+PowSize, Randomizer.nextInt(0+MAX_TERRAIN_HEIGHT+PowSize, Y-MAX_TERRAIN_HEIGHT-PowSize));
PowerUpSlow.setPosition(X+PowSize, Randomizer.nextInt(0+MAX_TERRAIN_HEIGHT+PowSize, Y-MAX_TERRAIN_HEIGHT-PowSize));
PowerUpSpeed.isActive = false;
TIMEOUTS.push(setTimeout(add, 5000, PowerUpSpeed));
TIMEOUTS.push(setTimeout(add, 5000, PowerUpSlow));
addObstacles();
addTerrain();
}
function gameStart() {
if (GAME_STARTED == false) {
GAME_STARTED = true;
windex = requestAnimationFrame(game);
mouseDownMethod(onMouseDown);
mouseUpMethod(onMouseUp);
keyDownMethod(onMouseDown);
keyUpMethod(onMouseUp);
}
}
function updateScore() {
points += POINTS_PER_ROUND;
points = Math.floor(points);
score.setText(points);
}
function powerUpSpeedOff() {
SPEED-=100;
TIMEOUTS.push(setTimeout(add, 5000, PowerUpSpeed));
TIMEOUTS.push(setTimeout(collisionOn, 3000));
addTerrain();
}
function powerUpSlowOff() {
SPEED+=4;
TIMEOUTS.push(setTimeout(add, 5000, PowerUpSlow));
}
function collisionOn() {
PowerUpSpeed.isActive=false;
}
function game() {
updateScore();
remove(HELICOPTER);
remove(GAME_START_TUTORIAL);
remove(TUTORIAL);
if (POWER_UP_GO_SPEED == true) {
POWER_UP_GO_SPEED = false;
SPEED+=100;
TIMEOUTS.push(setTimeout(powerUpSpeedOff, 5000));
TIMEOUTS.push(setTimeout(add, 5000, PowerUpSlow));
}
if (POWER_UP_GO_SLOW == true) {
POWER_UP_GO_SLOW = false;
SPEED-=4;
TIMEOUTS.push(setTimeout(powerUpSlowOff, 5000));
TIMEOUTS.push(setTimeout(add, 5000, PowerUpSpeed));
}
if (hitWall()) {
if (PowerUpSpeed.isActive == false) {
lose();
return;
}
}
if (getCollider() != null && getCollider() != copter && getCollider() != PowerUpSpeed && getCollider() != PowerUpSlow) {
if (PowerUpSpeed.isActive == false) {
lose();
return;
}
}
copter.move(0, dy);
if (getCollider() != null && getCollider() != copter && getCollider() != PowerUpSpeed && getCollider() != PowerUpSlow) {
if (PowerUpSpeed.isActive == false) {
lose();
return;
}
}
if (getCollider() == PowerUpSpeed) {
POWER_UP_GO_SPEED = true;
PowerUpSpeed.isActive = true;
remove(PowerUpSlow);
remove(PowerUpSpeed);
}
if (getCollider() == PowerUpSlow) {
POWER_UP_GO_SLOW = true;
remove(PowerUpSlow);
remove(PowerUpSpeed);
}
if (clicking) {
dy -= 0.2;
if (dy < -MAX_DY) {
dy = -MAX_DY;
}
}else{
dy += 0.2;
if (dy > MAX_DY) {
dy = MAX_DY;
}
}
if (getCollider() != null && getCollider() != copter && getCollider() != PowerUpSpeed && getCollider() != PowerUpSlow) {
if (PowerUpSpeed.isActive == false) {
lose();
return;
}
}
moveObstacles();
moveTerrain();
moveDust();
addDust();
SPEED += 0.00000001;
movePowerUps();
POINTS_PER_ROUND = (SPEED/6)*(NUM_OBSTACLES/6);
if (SPEED <=0) {
SPEED += 6;
}
windex = requestAnimationFrame(game);
}
function onMouseDown(e) {
clicking = true;
}
function onMouseUp(e) {
clicking = false;
}
function addObstacles() {
for (var i=0; i<NUM_OBSTACLES; i++) {
var obstacle = new Rectangle(OBST_W, OBST_H);
obstacle.setColor(Color.green);
obstacle.setPosition(X+i*(X/NUM_OBSTACLES), Randomizer.nextInt(0+MAX_TERRAIN_HEIGHT, Y-OBST_H-MAX_TERRAIN_HEIGHT));
obstacles.push(obstacle);
add(obstacle);
}
}
function moveObstacles() {
for (var i=0; i<obstacles.length; i++) {
var obstacle = obstacles[i];
obstacle.move(-SPEED, 0);
if (obstacle.getX() <= 0) {
obstacle.setPosition(X, Randomizer.nextInt(0+MAX_TERRAIN_HEIGHT, Y-OBST_H-MAX_TERRAIN_HEIGHT));
}
}
}
function movePowerUps() {
PowerUpSpeed.move(-SPEED, 0);
PowerUpSlow.move(-SPEED, 0);
if (PowerUpSpeed.getX() <= 0) {
PowerUpSpeed.setPosition(X+PowSize, Randomizer.nextInt(0+MAX_TERRAIN_HEIGHT+PowSize, Y-MAX_TERRAIN_HEIGHT-PowSize));
remove(PowerUpSpeed);
TIMEOUTS.push(setTimeout(add, 5000, PowerUpSpeed));
}
if (PowerUpSlow.getX() <= 0) {
PowerUpSlow.setPosition(X+PowSize, Randomizer.nextInt(0+MAX_TERRAIN_HEIGHT+PowSize, Y-MAX_TERRAIN_HEIGHT-PowSize));
remove(PowerUpSlow);
TIMEOUTS.push(setTimeout(add, 5000, PowerUpSlow));
}
}
function hitWall() {
var hit_top = copter.getY() < 0;
var hit_bottom = copter.getY() + copter.getHeight() > Y;
return hit_top || hit_bottom;
}
function lose() {
cancelAnimationFrame(windex);
dy = 0;
points = 0;
SPEED = 6;
var text = new Text("You Lose!");
text.setColor(Color.red);
text.setPosition(CENTER_X - text.getWidth()/2, CENTER_Y - text.getHeight()/2);
add(text);
var text1 = new Text ('Press "r" to reset.');
text1.setColor(Color.red);
text1.setPosition(CENTER_X-text1.getWidth()/2, text.getY()+text.getHeight()*4);
add(text1);
for (var i=0; i<=TIMEOUTS.length; i++) {
clearTimeout(TIMEOUTS[i]);
}
var ti = TIMEOUTS.pop();
while (ti != undefined) {
ti = TIMEOUTS.pop();
}
keyDownMethod(reset);
}
function reset(e) {
if (e.key == "r") {
if (GAME_STARTED == true) {
GAME_STARTED = false;
removeAll();
start();
}
}
}
function getCollider() {
var topLeft = getElementAt(copter.getX()-1, copter.getY()-1);
if (topLeft != null) {
return topLeft;
}
var topRight = getElementAt(copter.getX() + copter.getWidth() + 1, copter.getY() - 1);
if (topRight != null) {
return topRight;
}
var bottomLeft = getElementAt(copter.getX()-1, copter.getY()+copter.getHeight()+1);
if (bottomLeft != null) {
return bottomLeft;
}
var bottomRight = getElementAt(copter.getX() + copter.getWidth() + 1, copter.getY() + copter.getHeight() + 1);
if (bottomRight != null) {
return bottomRight;
}
return null;
}
function addTerrain() {
for (var i = 0; i <= X/TERRAIN_WIDTH; i++) {
var height = Randomizer.nextInt(MIN_TERRAIN_HEIGHT, MAX_TERRAIN_HEIGHT);
var terrain = new Rectangle(TERRAIN_WIDTH, height);
terrain.setPosition(TERRAIN_WIDTH*i, 0);
terrain.setColor(Color.green);
top_terrain.push(terrain);
add(terrain);
height = Randomizer.nextInt(MIN_TERRAIN_HEIGHT, MAX_TERRAIN_HEIGHT);
var bottomTerrain = new Rectangle(TERRAIN_WIDTH, height);
bottomTerrain.setPosition(TERRAIN_WIDTH*i, Y-bottomTerrain.getHeight());
bottomTerrain.setColor(Color.green);
bottom_terrain.push(bottomTerrain);
add(bottomTerrain);
}
}
function moveTerrain() {
for (var i=0; i < top_terrain.length; i++) {
var obj = top_terrain[i];
obj.move(-SPEED, 0);
if (obj.getX() < -obj.getWidth()) {
obj.setPosition(X, 0);
}
}
for (var i=0; i < bottom_terrain.length; i++) {
var obj = bottom_terrain[i];
obj.move(-SPEED, 0);
if (obj.getX() < -obj.getWidth()) {
obj.setPosition(X, Y-obj.getHeight());
}
}
}
function addDust() {
var d = new Circle(DUST_RADIUS);
d.setColor("#cccccc");
d.setPosition(copter.getX() - d.getWidth(), copter.getY() + DUST_BUFFER);
dust.push(d);
add(d);
}
function moveDust() {
for (var i=0; i<dust.length; i++) {
var d = dust[i];
d.move(-SPEED, 0);
d.setRadius(d.getRadius() - 0.05);
if (d.getX() < 0) {
remove(d);
dust.remove(i);
i--;
}
}
}
if (typeof start === 'function') {
start();
}
};
</script>
</div>
</body>
</html>