-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lost_in_Space.js
163 lines (126 loc) · 3.07 KB
/
Lost_in_Space.js
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
//Group 5
//Mayank Malhotra - 20BCE7308
//Rukhsar Rahman - 20BCE7302
//Swetha Sree J.A - 20BCE7299
//R.Soundarya - 20BCE7307
let mr_alien;
let space;
let start_screen;
let game_over;
let scrollSpeed = 2;
let x1 = 0,x2;
let start = 0;
function preload() {
// Loading the Images
mr_alien = loadImage("mr_alien.png");
space = loadImage("starry_night.jpg");
start_screen = loadImage("start_screen.jpeg");
game_over = loadImage("game_over.jpeg");
}
function Alien() {
//Setting up the Alien and Its movement
this.y = height / 2;
this.x = 45;
this.gravity = 0.4; // Alien Falling Speed
this.velocity = 0;
this.lift = -10;
this.show = function () {
image(mr_alien, this.x, this.y, 70, 70);
};
this.update = function () {
this.velocity += this.gravity; // Alien falling Down
this.velocity *= 0.9;
this.y += this.velocity;
if (this.y > height) {
this.y = height;
this.velocity = 0;
}
if (this.y < 0) {
this.y = 0;
this.velocity = 0;
}
};
this.fly = function () {
// Lifting Alien On a Mouse Click
this.velocity += this.lift;
};
}
function Pillar() {
//Function For Obstacles
this.top = random(20, height / 2); // Generating top Randomly
this.bottom = random(this.top + 85, height - 20); // Generating Bottom Randomly
this.x = width;
this.speed = 7;
this.show = function () {
// Displaying Pillars
fill(255, 204, 0);
rect(this.x, 0, 20, this.top);
rect(this.x, this.bottom, 20, height - this.bottom);
};
this.update = function () {
this.x -= this.speed;
};
this.collision = function (spaceship) {
// Function To check the Collision
if (spaceship.y < this.top || spaceship.y > this.bottom) {
if (spaceship.x > this.x && spaceship.x < this.x + 20)
return true;
}
}
}
let spaceship;
let pillars = [];
function setup() {
createCanvas(400, 600); // Creating the canvas
spaceship = new Alien();
pillars.push(new Pillar()); // Generating new pillars
x2 = width;
}
function draw() {
if (start == 0) {
fill(0, 0, 255);
image(start_screen, 0, 0, width, height);
}
if (start == 1) {
scrollyBackground(); // Scrolling the background backwards
spaceship.update();
spaceship.show();
textSize(20);
fill(255);
text("Score: " + (pillars.length - 1), 310, 30);
if (frameCount % 80 == 0) {
pillars.push(new Pillar());
}
for (let i = 0; i < pillars.length; i++) {
pillars[i].show();
pillars[i].update();
if (pillars[i].collision(spaceship)) {
image(game_over, 0, 0, width, height);
textSize(25);
text("Score: " + (pillars.length - 1), width / 2 - 45, 500);
noLoop();
}
}
}
}
function keyPressed() {
// On Key pressed Flying the alien
if (key == " ") {
spaceship.fly();
}
}
function scrollyBackground() {
image(space, x1, 0, width, height);
image(space, x2, 0, width, height);
x1 -= scrollSpeed;
x2 -= scrollSpeed;
if (x1 < -width) {
x1 = width;
}
if (x2 < -width) {
x2 = width;
}
}
function mouseClicked() {
start = 1;
}