-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
411 lines (394 loc) · 9.7 KB
/
Copy pathsketch.js
File metadata and controls
411 lines (394 loc) · 9.7 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
// Game scren size recommend between 1.1 to 2
const screen_s = 1.6;
//var for menu highlight
var xCursor, yCursor;
//var for state of the game
var estado = 1;
var selected = 1;
var perdeu = 0;
//state of the game
var inimigos = [];
var balas = [];
var powerups = [];
var pontos = 0;
var character_stats = [(life = 3), (firespeed = 8), (firerate = 10)];
//Variables for assets folder (images and sounds)
var menuLoading;
var characterSprite;
var heart;
var enemySprite;
var healthpowerup;
var speedpowerup;
var fireratepowerup;
var bg;
var projectile;
var mainsound;
var losesound;
var shootsound;
function preload() {
characterSprite = loadImage("assets/maincharacter.png");
heart = loadImage("assets/Heart.png");
enemySprite = loadImage("assets/astroid.png");
healthpowerup = loadImage("assets/Health.png");
speedpowerup = loadImage("assets/Speed increaser.png");
fireratepowerup = loadImage("assets/firerate increaser.png");
bg = loadImage("assets/space1.png");
projectile = loadImage("assets/laser_small.png");
mainsound = loadSound("assets/battleThemeA.mp3");
losesound = loadSound("assets/game_over_bad_chest.wav");
shootsound = loadSound("assets/laserpew.ogg");
}
function setup() {
createCanvas(400*screen_s, 400*screen_s);
xCursor = 150*screen_s;
yCursor = 120*screen_s;
//puts the game music
playsound(1);
}
// This fuction creates a button on the main menu
function botoes(xmenu, ymenu, textx) {
textSize(14);
stroke(0);
strokeWeight(1);
fill(68, 5, 15, 50);
rect(xmenu*screen_s, ymenu*screen_s, 100, 60);
fill(0);
text(textx, xmenu*screen_s + 28, ymenu*screen_s + 30 + 6);
fill(220);
//desenho
}
// highlight for buttons
function buttonselect() {
noFill();
stroke(255, 0, 0);
strokeWeight(4);
rect(xCursor, yCursor, 100, 60);
}
// function for enemy generation
function enemyGenerator(amount) {
for (i = 0; i < amount; i++) {
var siz = parseInt(random(10, 60))
let inimigo = {
x: random(0, width - 25*screen_s),
y: random(-600*screen_s, 0),
size: siz,
health: parseInt(random(0,(siz/10))),
speed: parseInt(random(1,5)-((siz/10)/2))
};
inimigos.push(inimigo);
}
}
function powerup_generate(amount) {
for (i = 0; i < amount; i++) {
let powerup = {
x: random(0, width - 25*screen_s),
y: random(-1100, 0),
type: parseInt(random(1, 3)),
};
powerups.push(powerup);
}
}
// fuction for playing diferent songs
function playsound(type) {
if (type == 1) {
losesound.stop();
mainsound.play();
mainsound.setVolume(0.02);
mainsound.setLoop(true);
} else {
mainsound.stop();
losesound.play();
mainsound.setLoop(false);
losesound.setVolume(0.2);
}
}
function screen_heart_generation(xmenu, ymenu) {
image(heart, xmenu*screen_s, ymenu*screen_s);
}
function draw() {
if (estado == 1) {
menu();
}
if (estado == 2) {
jogar();
}
if (estado == 3) {
instrucoes();
}
if (estado == 4) {
creditos();
}
if (estado == 5) {
story();
}
}
// Keyboard Controls
function keyPressed() {
//UP
if (keyCode == UP_ARROW) {
if (estado == 1) {
//lower than last but higher than first button
if (yCursor <= 280*screen_s && yCursor > 120*screen_s) {
yCursor -= 80*screen_s;
selected -= 1;
}
}
}
if (estado == 1) {
//DOWN
if (keyCode == DOWN_ARROW) {
if (yCursor < 280*screen_s) {
yCursor += 80*screen_s;
selected += 1;
}
}
}
//Back to the menu
if (keyCode == LEFT_ARROW) {
if (estado != 1 && estado != 2) {
estado = 1;
}
}
// Enter for selecting a button menu
if (estado == 1) {
if (keyCode == ENTER) {
//jogar
if (selected == 1 && estado == 1) {
estado = 5;
}
//instruções
if (selected == 2 && estado == 1) {
estado = 3;
}
//creditos
if (selected == 3 && estado == 1) {
estado = 4;
}
}
}
//if the player lose
if (perdeu == 1 && estado == 2 && keyCode != ENTER) {
estado = 1;
perdeu = 0;
// wipe enimies and projectiles
inimigos = [];
balas = [];
//create new enemies and play the first song
playsound(1);
enemyGenerator(8);
powerup_generate(1);
// reset player stats
character_stats[0] = 3;
character_stats[1] = 8;
character_stats[2] = 10;
gametime=0;
spawn = 0;
}
}
//When a mouse click happens
function mousePressed() {
//creates a projectile
if (estado == 2 && perdeu == 0) {
let bala = {
x: mouseX,
y: height - 50,
};
balas.push(bala);
shootsound.play();
shootsound.setVolume(0.02);
}
}
//Main menu loop
function menu() {
//background for the button
stroke(0);
strokeWeight(1);
background(60);
fill(170, 60, 70);
rect(100*screen_s, 100*screen_s, 200*screen_s/1.2, 250*screen_s);
textSize(18);
text("Asteroid Fall", width/2-50, 100);
//play button
botoes(150, 120, " Jogar");
//instruction button
botoes(150, 200, "Instruções");
//credits button
botoes(150, 280, "Creditos");
// button highlight
buttonselect();
}
gametime=0;
//Main play loop
function jogar() {
image(bg, -150, -350);
gametime+=1;
// The player
image(characterSprite, mouseX - 17, height - 50);
//The player Health on the screen
if (character_stats[0] > 0) {
for (i = 0; i < character_stats[0] * 21; i += 21) {
screen_heart_generation(10 + i, 35);
}
}
for (let bala of balas) {
image(projectile, bala.x - 5, bala.y);
bala.y -= character_stats[1];
// if the projectile goes out of screen it will remove it
if(bala.y<=-110){balas.splice(balas.indexOf(bala), 1);}
}
//Enemy loop
for (let inimigo of inimigos) {
image(
enemySprite,
inimigo.x - 10,
inimigo.y,
25 + inimigo.size,
25 + inimigo.size
);
//The enemy keeps moving until you lose
if (perdeu == 0) {
//gravity
inimigo.y += 1.1+inimigo.speed;
}
//Lose condition
if (inimigo.y > height) {
if (character_stats[0] <= 0) {
strokeWeight(2);
textSize(16);
text("Você perdeu, uma cidade foi destruida! Precione qualquer botão.", width/10*screen_s, height / 2);
if (perdeu == 0) {
playsound(2);
}
perdeu = 1;
} else {
character_stats[0] -= 1;
inimigos.splice(inimigos.indexOf(inimigo), 1);
enemyGenerator(1)
if(gametime>500){
enemyGenerator(parseInt(random(0,1)));
}
if(gametime>2000){enemyGenerator(1);}
}
}
}
// Power up loop
for (let powerup of powerups) {
if (powerup.type == 1) {
image(healthpowerup, powerup.x, powerup.y);
}
if (powerup.type == 2) {
image(speedpowerup, powerup.x, powerup.y);
}
if (powerup.type == 3) {
image(fireratepowerup, powerup.x, powerup.y);
}
if (perdeu == 0) {
powerup.y += 1.5;
}
if (powerup.y > height) {
powerups.splice(powerups.indexOf(powerup), 1);
powerup_generate(1);
}
}
//Collision detection Enemy x Projectile
for (let inimigo of inimigos) {
for (let bala of balas) {
//se estiver no alcance
if (dist(inimigo.x, inimigo.y, bala.x, bala.y) < inimigo.size) {
// if lower or equal 1 health the enemy dies
if(inimigo.health<=1){
inimigos.splice(inimigos.indexOf(inimigo), 1);
enemyGenerator(1);
pontos += 1;
if(gametime>1000){
enemyGenerator(parseInt(random(0,1)));
}
if(gametime>15000){enemyGenerator(1);}
}
if(inimigo.health>0){inimigo.health-=1;}
balas.splice(balas.indexOf(bala), 1);
}
}
}
for (let powerup of powerups) {
if (dist(powerup.x, powerup.y, mouseX - 17, height - 50) < 45) {
if (powerup.type == 1) {
character_stats[0] += 1;
}
if (powerup.type == 2) {
character_stats[1] += 3;
}
if (powerup.type == 3) {
character_stats[2] += 3;
}
powerups.splice(powerups.indexOf(powerup), 1);
powerup_generate(1);
}
}
// text with your score
text(pontos, 15, 25);
}
function instrucoes() {
// As instruções
background(150);
fill(170);
rect(50, 100*screen_s, 330*screen_s, 250*screen_s);
fill(0, 255, 0);
stroke(0, 200, 0);
text("Instruções", width / 2 - 25, height / 2 - 50);
text(
"Utilize o mouse para controlar o personagem. Os cliques irão atirar",
width / 3,
height / 2 + 20
);
text(
"Não deixe que os inimigos cheguem até em baixo da tela",
width / 7,
height / 2 + 40
);
//fim background
}
function creditos() {
// Os creditos
background(150);
fill(170);
rect(50, 100*screen_s, 330*screen_s, 250*screen_s);
fill(0, 255, 0);
stroke(0, 200, 0);
text("Creditos", width / 2 - 25*screen_s, height / 2 - 50*screen_s);
text(
"Aluno: Rafael Sales de Almeida Cavalcanti Turma: 3C",
width / 7,
height / 2 + 20
);
text("Tema: EF03CI07", width / 7, height / 2 + 40);
}
var tempo = 570;
var spawn = 0
// story before play loop
function story(){
textSize(32);
strokeWeight(1);
background(0);
stroke(255, 255, 255);
tempo -= 1
if(tempo>100&&tempo<600){
text("A terra está em perigo!", width/6*screen_s, height/10*screen_s);
textSize(19);
text("Vários asteroides entraram na orbita solar", width/6*screen_s, height/6*screen_s);
textSize(22);
text("Só você pode nos salvar", width/6*screen_s, height/2);
text("'Use o mouse para jogar'", width/6*screen_s, height/1.3);
}
if(tempo>0&&tempo<70){
var spawned = 0
textSize(32);
text("Prepare-se... 3 2 1", width/6*screen_s, height/3);
if(!spawn){
spawn = 1;
enemyGenerator(8);
powerup_generate(1);
}
}
if(tempo<0){estado=2;tempo=600}
}