-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
160 lines (130 loc) · 4 KB
/
Copy pathscript.js
File metadata and controls
160 lines (130 loc) · 4 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
//rakettens hastighed defineres på forhånd.
speed = 7;
var distance;
var h;
var g;
//hvis der ikke findes en gemt highscore endnu, bliver den lavet nu
if(! localStorage.getItem(a)){
var a;
localStorage.setItem(a, 0)
}
//highscore og score defineres.
highscore = localStorage.getItem(a);
score = 0;
//array til boldene laves.
var balls = [];
//preload af billeder, så de er klar til visning inden resten af koden udføres/executes.
function preload(){
rocket = loadImage("images/rocket.png");
bg = loadImage("images/bg3.jpeg")
hit_sound = loadSound('sounds/hit.mp3');
}
//canvas laves og rakettens første position defineres. Funktionen der laver nye bolde får en "forsnikelse" på 0,1 sekund.
function setup(){
x_rocket = displayWidth/2-15;
y_rocket = displayHeight-250;
var interval = setInterval(newBall, 100);
}
//funktionen der laver nye bolde og definerer str. udfra hvor mange bolde der findes i forvejen (jo flere bolde, jo større bolde begynder der at komme).
function newBall(){
if (balls.length < 20){
ball_min = 20;
ball_max = 40;
}
else if (balls.length >= 20 && balls.length < 40){
ball_min = 30;
ball_max = 50;
}
else if (balls.length >= 40 && balls.length < 80){
ball_min = 30;
ball_max = 70;
}
else if (balls.length >= 80 && balls.length < 110){
ball_min = 40;
ball_max = 80;
}
else if (balls.length >= 110 && balls.length < 150){
ball_min = 50;
ball_max = 90;
}
else if (balls.length >= 150 && balls.length <170){
ball_min = 80;
ball_max = 120;
}
else if (balls.length >= 170 && balls.length <200){
ball_min = 100;
ball_max = 150;
balls.splice(0,20)
}
balls.push(new Ball(random (10,displayWidth-10), -20, random(ball_min, ball_max)));
score +=1;
}
function draw(){
//baggrund og raket bliver "tegnet".
clear();
createCanvas(displayWidth,displayHeight);
background(bg);
image(rocket, x_rocket , y_rocket);
ellipse(x_rocket+30,y_rocket+35,3,3)
text("Nuværende score: " + score,5,15);
text("Din highscore: " + localStorage.getItem(a),5,30);
// højre og venstre piltast og "A" og "D" bevæger raketten.
if (keyIsDown(65) && x_rocket>0 || keyIsDown(37) && x_rocket>0){
x_rocket -= speed;
}
if (keyIsDown(68) && x_rocket<(displayWidth-30) || keyIsDown(39) && x_rocket<(displayWidth-30)){
x_rocket += speed;
}
// alle boldene vises.
for (var i = 0; i < balls.length; i++) {
balls[i].fall();
balls[i].display();
}
//der tjekkes om distancen fra rakettens top, eller en af siderne til en bolds midtpunkt er mindre end boldens diameter, med andre ord bliver der tjekket for en coliision af de to objekter
for (i in balls){
if (dist(x_rocket+15, y_rocket, balls[i].x, balls[i].y) < balls[i].d/2 || dist(x_rocket, y_rocket+35, balls[i].x, balls[i].y) < balls[i].d/2 || dist(x_rocket+30, y_rocket+35, balls[i].x, balls[i].y) < balls[i].d/2){
gameOver();
}
}
//highscore opdateres hvis score er større
if (score> localStorage.getItem(a)){
localStorage.setItem(a,score)
};
}
// "Ball" defineres med farve og dennes placering/fald opdateres.
class Ball {
constructor(x_position, y_position, diameter) {
this.x = x_position;
this.y = y_position;
this.d = diameter;
this.speed = 5;
this.display = function () {
fill(226,88,34);
stroke(0,0,0);
ellipse(this.x, this.y, this.d, this.d);
};
this.fall = function () {
this.y = this.y + this.speed;
};
}
}
function gameOver(){
noLoop();
hit_sound.play(0,1,1,0.5);
window.alert("Din raket blev ramt af en meteor:(" + "\r\n" + "\r\n" + "Du scorede " + score + " point" + "\r\n" + "Din highscore er " + localStorage.getItem(a) + "\r\n" + "\r\n" + 'Tryk enter for at prøve igen');
location.reload();
}
/*
if(false){
noLoop();
};
*/
// Husk "noLoop();" til at stoppe spillet
/* brugt til at fastsætte de forskellige keyCodes (ikke relavant)
function keyPressed(){
if(keyCode === 65 || keyCode === 37){
console.log("venstre");}
else if(keyCode === 68 || keyCode === 39){
console.log("højre");}
}
*/