-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsketch.js
185 lines (162 loc) · 4.22 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
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
let bgImg;
let birdSprite;
let pipeBodySprite;
let pipePeakSprite;
// How big is the population
let totalPopulation = 500;
// All active birds (not yet collided with pipe)
let activeBirds = [];
// All birds for any given population
let allBirds = [];
// Pipes
let pipes = [];
// A frame counter to determine when to add a pipe
let counter = 0;
// Interface elements
let speedSlider;
let speedSpan;
let highScoreSpan;
let allTimeHighScoreSpan;
// All time high score
let highScore = 0;
// Training or just showing the current best
let runBest = false;
let runBestButton;
function preload(){
bgImg = loadImage('assets/background.jpg');
birdSprite = loadImage('assets/bird1.png');
pipeBodySprite = loadImage('assets/pipebg.png');
pipePeakSprite = loadImage('assets/pipebg.png');
}
function setup() {
let canvas = createCanvas(700, 400);
var x = (windowWidth - width) / 2;
var y = (windowHeight - height) / 2;
canvas.position(x, y);
canvas.parent('canvascontainer');
// Access the interface elements
speedSlider = select('#speedSlider');
speedSpan = select('#speed');
highScoreSpan = select('#hs');
allTimeHighScoreSpan = select('#ahs');
runBestButton = select('#best');
runBestButton.mousePressed(toggleState);
// Create a population
for (let i = 0; i < totalPopulation; i++) {
let bird = new Bird();
activeBirds[i] = bird;
allBirds[i] = bird;
}
}
// Toggle the state of the simulation
function toggleState() {
runBest = !runBest;
// Show the best bird
if (runBest) {
resetGame();
runBestButton.html('continue training');
// Go train some more
} else {
nextGeneration();
runBestButton.html('Run Best');
}
}
function draw() {
background(bgImg);
// Should we speed up cycles per frame
let cycles = speedSlider.value();
speedSpan.html(cycles);
// How many times to advance the game
for (let n = 0; n < cycles; n++) {
// Show all the pipes
for (let i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
if (pipes[i].offscreen()) {
pipes.splice(i, 1);
}
}
// Are we just running the best bird
if (runBest) {
bestBird.think(pipes);
bestBird.update();
for (let j = 0; j < pipes.length; j++) {
// Start over, bird hit pipe
if (pipes[j].hits(bestBird)) {
resetGame();
break;
}
}
if (bestBird.bottomTop()) {
resetGame();
}
// Or are we running all the active birds
} else {
for (let i = activeBirds.length - 1; i >= 0; i--) {
let bird = activeBirds[i];
// Bird uses its brain!
bird.think(pipes);
bird.update();
// Check all the pipes
for (let j = 0; j < pipes.length; j++) {
// It's hit a pipe
if (pipes[j].hits(activeBirds[i])) {
// Remove this bird
activeBirds.splice(i, 1);
break;
}
}
if (bird.bottomTop()) {
activeBirds.splice(i, 1);
}
}
}
// Add a new pipe every so often
if (counter % 75 == 0) {
pipes.push(new Pipe());
}
counter++;
}
// What is highest score of the current population
let tempHighScore = 0;
// If we're training
if (!runBest) {
// Which is the best bird?
let tempBestBird = null;
for (let i = 0; i < activeBirds.length; i++) {
let s = activeBirds[i].score;
if (s > tempHighScore) {
tempHighScore = s;
tempBestBird = activeBirds[i];
}
}
// Is it the all time high scorer?
if (tempHighScore > highScore) {
highScore = tempHighScore;
bestBird = tempBestBird;
}
} else {
// Just one bird, the best one so far
tempHighScore = bestBird.score;
if (tempHighScore > highScore) {
highScore = tempHighScore;
}
}
// Update DOM Elements
highScoreSpan.html(tempHighScore);
allTimeHighScoreSpan.html(highScore);
// Draw everything!
for (let i = 0; i < pipes.length; i++) {
pipes[i].show();
}
if (runBest) {
bestBird.show();
} else {
for (let i = 0; i < activeBirds.length; i++) {
activeBirds[i].show();
}
// If we're out of birds go to the next generation
if (activeBirds.length == 0) {
nextGeneration();
}
}
}