-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (85 loc) · 2.03 KB
/
index.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
const TOTAL = 500;
var current_generation = 0;
var birds = [];
var savedBirds = []
let counter = 0;
let cycles = 100;
var player;
let slider;
var pipes = [];
var scores = 0;
var touched = false;
var scored = false;
function setup() {
createCanvas(600, 700);
slider = createSlider(1, 100, 1);
background(0);
for (let i = 0; i < TOTAL; i++) {
birds.push(new Bird());
}
generation();
score();
}
function draw() {
for (let n = 0; n < slider.value(); n++) {
if (counter % 300 == 0) {
pipes.push(new Pipe());
}
counter++;
for (let bird of birds) {
bird.think(pipes);
bird.update();
}
if (birds.length == 0) {
counter = 0;
for (let bird of savedBirds) {
if (scores < bird.score) {
scores = bird.score;
score();
}
}
newGeneration();
current_generation++;
generation();
pipes = []
}
for (var i = 0; i < pipes.length; i++) {
if (pipes[i].x < pipes[i]) {
pipes.splice(i, 1);
} else {
if (pipes[i].x > width / 2 - pipes[i].w / 2 && pipes[i].x < width / 2 + pipes[i].w / 2) {
for (let j = 0; j < birds.length; j++) {
if (birds[j].y < (pipes[i].gap - pipes[i].height / 2)) {
pipes[i].color = color(255, 0, 0);
savedBirds.push(birds.splice(j, 1)[0]);
} else if (birds[j].y > pipes[i].gap + pipes[i].height / 2) {
pipes[i].color = color(255, 0, 0);
savedBirds.push(birds.splice(j, 1)[0]);
}
}
} else {
pipes[i].color = color(255, 255, 255);
}
pipes[i].update();
}
}
}
background(0);
for (bird of birds) {
bird.show()
}
for (pipe of pipes) {
pipe.show()
}
}
// function keyPressed() {
// if (key == ' ') {
// player.jump(1.10);
// }
// }
function generation() {
document.getElementById("generation").innerHTML = current_generation;
}
function score() {
document.getElementById("score").innerHTML = scores;
}