-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
101 lines (83 loc) · 1.7 KB
/
sketch.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
// Global Variables //
var ship;
var speed = 2;
var meteorX = 200;
var meteorShower = [];
var starField = [];
var laserBeams = [];
var paused = false;
/********************/
function setup() {
noStroke();
colorMode(HSB, 360, 100, 100, 1);
createCanvas(600, 600);
ship = new SpaceShip();
for (let i = 0; i < 300; i++) {
starField.push(new Star(0, 600));
}
}
function draw() {
background(200, 100, 1, 1);
renderStarField();
ship.display();
ship.move();
ship.borderWrap();
for (let i = 0; i < laserBeams.length; i++) {
laserBeams[i].display();
laserBeams[i].trajectory();
if (laserBeams[i].isOffScreen()) {
laserBeams.splice(i, 1);
i--;
}
}
for (let i = 0; i < meteorShower.length; i++) {
meteorShower[i].display();
meteorShower[i].fall();
if (meteorShower[i].isOffScreen()) {
meteorShower.splice(i, 1);
i--;
}
}
for (let i = 0; i < starField.length; i++) {
starField[i].display();
starField[i].fall();
if (starField[i].isOffScreen()) {
starField.splice(i, 1);
i--;
}
}
}
var showerInterval = setInterval(renderMeteors, 200);
function renderStarField() {
starField.push(new Star(0, 1));
}
function renderMeteors() {
meteorShower.push(new Meteor(meteorX));
if (meteorX < width + 10 && meteorX > -10) {
meteorX += random(-50, 100);
} else {
meteorX = random(0, 500);
}
}
// Event Handlers //
function keyPressed() {
if (keyCode == 32) { // Space
ship.fireLaser();
}
}
function mousePressed() {
if (paused) {
paused = false;
pauseGame();
} else {
paused = true;
pauseGame();
}
}
function pauseGame() {
if (paused) {
noLoop();
} else {
loop();
}
}