-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
60 lines (49 loc) · 1.11 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
const totalBirds = 500;
let birds = [];
let savedBirds = [];
let pipes = [];
let counter = 0;
function setup() {
createCanvas(640, 480);
for (let i = 0; i < totalBirds; i++) {
birds[i] = new Bird();
}
}
function draw() {
if (counter % 75 == 0) {
pipes.push(new Pipe());
}
counter++;
for (let i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
for (let j = birds.length - 1; j >= 0; j--) {
if (pipes[i].hits(birds[j])) {
savedBirds.push(birds.splice(j, 1)[0]);
}
}
if (pipes[i].offscreen()) {
pipes.splice(i, 1);
}
}
for (let i = birds.length - 1; i >= 0; i--) {
if (birds[i].offScreen()) {
savedBirds.push(birds.splice(i, 1)[0]);
}
}
for (let bird of birds) {
bird.think(pipes);
bird.update();
}
if (birds.length === 0) {
counter = 0;
nextGeneration();
pipes = [];
}
background(0);
for (let bird of birds) {
bird.show();
}
for (let pipe of pipes) {
pipe.show();
}
}