-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
420 lines (258 loc) · 10.5 KB
/
index.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
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
414
415
416
417
418
419
420
<!DOCTYPE html>
<!-- This is based on DillingerLee's great template here:
https://github.com/Team-Code/KA_Offline -->
<html>
<head>
<title>Neptuns Gaffel</title>
</head>
<body>
<h1 align="center">Neptuns Gaffel</h1>
<p align="center">
<!--This draws the Canvas on the webpage -->
<canvas id="mycanvas"></canvas>
</p>
</body>
<!-- Run all the JavaScript stuff -->
<!-- Include the processing.js library -->
<!-- See https://khanacademy.zendesk.com/hc/en-us/articles/202260404-What-parts-of-ProcessingJS-does-Khan-Academy-support- for differences -->
<script src="https://cdn.jsdelivr.net/processing.js/1.4.8/processing.min.js"></script>
<script>
var sketchProc = function(processingInstance) {
with (processingInstance) {
//******SETUP****** - canvas størrelse og framerate/hastighed på draw funktionen
size(1000, 600);
frameRate(120);
//funktionskald der bestemmer hvor billedet bliver tegnet fra (centrum af billedet her, men der kan også bruges CORNER (hjørne))
imageMode(CENTER);
//billeder loades fra lokale filer til variable ved at skrive stien til dem to gange som nedenunder
/* @pjs preload="gaffel.png"; */
var forkImage = loadImage("gaffel.png");
/* @pjs preload="neptun.png"; */
var neptuneImage = loadImage("neptun.png");
/* @pjs preload="goppel_1.png"; */
var gobbelImage1 = loadImage("goppel_1.png");
/* @pjs preload="goppel_2.png"; */
var gobbelImage2 = loadImage("goppel_2.png");
var gobbelImages = [gobbelImage1, gobbelImage2];
/* @pjs preload="diver1.png"; */
var diverImage1 = loadImage("diver1.png");
/* @pjs preload="diver2.png"; */
var diverImage2 = loadImage("diver2.png");
var diverImages = [diverImage1, diverImage2];
//******SPIL VARIABLE******
//liste der kommer til at holde vores gaffels vej
var forkPath = [];
//fjender
var enemies = [];
//tid der går til en ny fjende bliver skabt (i milliskunder)
var enemySpawnTime = 1000;
var lastEnemyTimestamp = 0;
//musen-trykket-ned variabel, vi bliver nødt til at holde styr på denne selv, på en hjemmeside, i forhold til KhanAcademy
var mouseIsPressed = false;
//Den fart et skud har, større tal gør den hurtigere
var forkSpeed = 12;
var forkTimestamp = millis();
//Vores hovedpersons hastighed
var enemySpeed = .25;
//fjende animationer
var enemyAnimSpeed = 400;
var enemyAnimTiemstamp = millis();
var enemyAnimIndex = 0;
//Objektet med vores hovedperson
//Den har en position (x,y) og det man kalder en "retning" (dirX, dirY)
//dirX og dirY fortæller hvor mange x og y pixels vi hver draw-runde skal bevæge os
//f.eks. dirX = 5, betyder vi går 5 pixels til højre ved hver draw runde
var mainCharacter = {
x : width / 2,
y :100,
};
// størrelsen i pixels på vores figurer i spillet
var mainCharacterPixelSize = 180;
var forkPixelSize = 100;
var enemySize = 50;
var diverSize = 100;
//our fork initial state
var fork = {
x : (width / 2) - 50,
y : 80
};
var score = 0;
//******** SPIL KODE ********
//Vi laver en Enemy funktion, der returnere et objekt.
//Her er objektet en fjende som vi skaber, der skal løbe fra sin start position, mod hovedpersonen.
//Vi bruger en random funktion til at bestemme x og y positionen, så den altid ved starte et sted på en kant (top, bund, højre eller venstre)
//(Vi giver også fjenden noget random liv, der afgør hvor mange skud den kan holde til).
var Enemy = function(){
//det tomme fjende objekt
var enemy = {};
enemy.x = random(0,width);
enemy.y = height ;
enemy.dirX = 0;
enemy.dirY = -1 * enemySpeed;
enemy.isDiver = random(0,3) < 1;
//til sidst returnere vi vores fjende objekt
return enemy;
};
var releasedFork = false;
//vi styrer vores mouseIsPressed fra disse to funktioner.
mousePressed = function() {
mouseIsPressed = true;
//hvis gafflen IKKE er ude at flyve - så kan vi påbegynde et nyt path
if(!releasedFork){
forkPath = [];
}
}
mouseOut = function() {
mouseReleased();
}
mouseReleased = function() {
mouseIsPressed = false;
if(releasedFork){
return;
}
//vi start vores gaffel ved den originale start.
//vi putter to ekstra paths ind i vores musepath: fra neptuns hånd til forkPath start og fra forkPaths slutning tilbage til Neptuns hånd
fork = {
x : (width / 2) - 50,
y : 100
};
var firstPoint = forkPath[0];
//dette udregner distance mellem to punkter på et koordinatsystem
var distance = Math.sqrt( (fork.x-firstPoint.x)*(fork.x-firstPoint.x) + (fork.y-firstPoint.y)*(fork.y-firstPoint.y) );
for(var i = 0; i < distance; i = i + forkSpeed){
var newX = lerp(firstPoint.x,fork.x, i/distance);
var newY = lerp(firstPoint.y, fork.y, i/distance);
forkPath.splice(0, 0, {x: newX, y: newY});
}
var lastPoint = forkPath[forkPath.length -1];
distance = Math.sqrt( (fork.x-lastPoint.x)*(fork.x-lastPoint.x) + (fork.y-lastPoint.y)*(fork.y-lastPoint.y) );
for(var i = 0; i < distance; i = i + forkSpeed){
var newX = lerp(lastPoint.x,fork.x, i/distance);
var newY = lerp(lastPoint.y, fork.y, i/distance);
forkPath.splice(forkPath.length , 0, {x: newX, y: newY});
}
releasedFork = true;
}
/*
Det gøres muligt at roterer fork-billedet.
Vi udregner nedenunder vinklen mellem forks position og dens næste punkt (retning).
Derefter laver vi et "tegnelag" ovenpå vores normale canvas. (en Matrix, laves med "pushMatrix()").
Dette tegnelag kan flyttes og roteres, for derefter at smelte tilbage sammen med vores orginale lag (ved at bruge "popMatrix()").
Ved at flytte og roterer et tegnelag som vi tegner vores fork-billede på, vises billedet rigtigt når vi smelter vores tegnelag tilbage i det normale billede (via popMatrix()).
*/
var drawFork = function(){
//kun hvis vores fork er ude at flyve roterer vi den. Ellers bliver den bare tegnet i neptuns hånd.
if(forkPath.length > 0 && releasedFork){
var point = forkPath[0];
//vinkel mellem position og retning
var angle = atan2(point.x - fork.x ,fork.y - point.y);
pushMatrix();
translate(fork.x,fork.y);
rotate(angle);
image(forkImage ,0,0, forkPixelSize,forkPixelSize);
popMatrix();
}else{
fork = {
x : (width / 2) - 50,
y : 80
};
image(forkImage ,fork.x,fork.y, forkPixelSize,forkPixelSize);
}
}
/*
**TEGNE /DRAW FUNKTIONEN**
Her køres vores game-loop. Denne funktion kører 30 gange i sekundet.
Alle de ting der hele tiden skal undersøges og ændre omkring vores mainCharacter, fjender og gaffel, sker her eller kaldes gennem funktioner her.
*/
var forkMoveTimestamp = millis();
draw = function() {
//baggrund, hav og neptun tegnes.
background(255,243,243);
fill(68, 76,174);
noStroke();
rect(0,100, width, height-100);
text("SCORE: " + score, 50,50 );
//nu tegner vi vores hovedperson der hvor den nu befinder sig
image(neptuneImage ,mainCharacter.x,mainCharacter.y, mainCharacterPixelSize,mainCharacterPixelSize);
//Hvis man ikke kan loade billeder, kan ellipse bruges. (fjern // fra linjen nedenunder og sæt // ved "image" ovenover
//ellipse(mainCharacter.x,mainCharacter.y, 20, 20);
drawFork();
//hvis musen er trykket ned og vi ikke er ude at flyve, tilføjer vi punkter som vores gaffel skal flyve efter.
//for ikke at klumpe for meget sammen, sørger vi for vi kun tilføjer punkter hvis de er langt nok væk
var distance = 100000000;
if(forkPath.length > 0){
var lastPoint = forkPath[forkPath.length -1];
distance = Math.sqrt( (mouseX-lastPoint.x)*(mouseX-lastPoint.x) + (mouseY-lastPoint.y)*(mouseY-lastPoint.y) );
}
//her tilføjes puntkerne
if(mouseIsPressed && !releasedFork && distance > 10){
forkPath.push({x: mouseX, y: mouseY, origin: true});
}
//her tegnes punkterne i forkpath - vi tegner KUN dem som man selv har trykket ned
fill(224, 207,62 );
for(var i = 0; i < forkPath.length; i++){
var point = forkPath[i];
if(point.origin != null)
ellipse(point.x, point.y, 5, 5);
}
//her "æder" vi den vej som gafflen kører af. Når den er slut, kan vi som spiller lave en ny.
if(releasedFork){
if(forkPath.length > 0 && millis() - forkTimestamp >= forkSpeed){
forkTimestamp = millis();
var point = forkPath[0];
fork.x = point.x;
fork.y = point.y;
forkPath.splice(0,1);
}
}
if(forkPath.length == 0){
releasedFork = false;
}
//hver gang der er gået lidt tid, laver vi en ny fjende (1 sekund som default)
if(millis() - lastEnemyTimestamp >= enemySpawnTime){
lastEnemyTimestamp = millis() ;
//dette skubber et nyt fjende objekt på vores fjende liste
enemies.push(Enemy());
}
//for hver fjende, så tegner vi fjenden og renger ud om den er blevet ramt af en gafflen
for(var i = enemies.length-1; i >=0; i--){
//vi hiver hver fjende ud fra listen af fjender
var enemy = enemies[i];
//og sætter dens nye position ligesom med hovedpersonen
enemy.x = enemy.x + enemy.dirX;
enemy.y = enemy.y + enemy.dirY;
//hvis fjenden ryger ud fra banen, så sletter vi det.
if(enemy.x < 0 || enemy.x > width || enemy.y < 0 || enemy.y > height){
enemies.splice(i,1);
}
//dette er en formel, der udregner længden mellem to punkter (her fjenden og kuglen)
var distance = Math.sqrt( (fork.x-enemy.x)*(fork.x-enemy.x) + (fork.y-enemy.y)*(fork.y-enemy.y) );
//hvis distancen er for lav, så har skuddet ramt
//så vi sætter fjenden til lavere liv og fjerner skuddet
if(distance < enemySize ){
score += 1;
enemies.splice(i,1);
}
//hver gang der er gået lidt tid, laver vi en ny fjende (1 sekund som default)
if(millis() - enemyAnimTiemstamp >= enemyAnimSpeed){
//tegn fjenden
enemyAnimIndex = (enemyAnimIndex + 1) % gobbelImages.length;
enemyAnimTiemstamp = millis();
}
if(!enemy.isDiver)
image(gobbelImages[enemyAnimIndex],enemy.x,enemy.y, enemySize ,enemySize );
else
image(diverImages[enemyAnimIndex],enemy.x,enemy.y, diverSize ,diverSize );
if(enemy.y < 100 ){
enemies = [];
score = 0;
}
}
};
}};
// Get the canvas that Processing-js will use
var canvas = document.getElementById("mycanvas");
// Pass the function sketchProc (defined in myCode.js) to Processing's constructor.
var processingInstance = new Processing(canvas, sketchProc);
</script>
</html>