-
Notifications
You must be signed in to change notification settings - Fork 4
/
sketch.js
300 lines (252 loc) · 6 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
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
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/cXgA1d_E-jY
// P5 exported functions (eslint flags)
/* exported preload, setup, draw, keyPressed */
// Exported sprites (eslint flags)
/* exported birdSprite, pipeBodySprite, pipePeakSprite */
var parallax = 0.8;
var score = 0;
var maxScore = 0;
var gameoverFrame = 0;
var isOver = false;
let playerWon = false;
let numBirds = 0;
let counter = 0;
var touched = false;
var prevTouched = touched;
const POP_TOTAL = 500;
// UI elements
let speedSlider;
let speedSpan;
let highScoreSpan;
let allTimeHighScoreSpan;
// High score
let highScore = 0;
let birds = [];
let savedBirds = [];
let pipes = [];
// Challenge mode
let birdbrain = null;
let aibird = null;
let playerbird = null;
let challengemode = false;
let c1, c2;
let scroll = 0;
function preload() {
brainJSON = loadJSON("bird.json");
}
function setup() {
let canvas = createCanvas(600, 500);
canvas.parent("canvascontainer");
// Check if challenge mode
challengeCheckbox = select("#challenge-checkbox");
speedSlider = select("#speedSlider");
speedSpan = select("#speed");
highScoreSpan = select("#hs");
allTimeHighScoreSpan = select("#ahs");
// Access the interface elements
birdbrain = NeuralNetwork.deserialize(brainJSON);
c1 = color(52, 197, 162);
c2 = color(137, 106, 228);
for (let i = 0; i < POP_TOTAL; ++i) {
birds[i] = new Bird();
}
reset();
}
// Callback function for when checkbox/button pressed
function challenge() {
console.log("CHALLENGE TOGGLED");
if (challengeCheckbox.checked()) {
savedBirds = [];
birds = [];
} else {
for (let i = 0; i < POP_TOTAL; ++i) {
birds[i] = new Bird();
}
}
challengemode = challengeCheckbox.checked();
reset();
}
function draw() {
if (challengeCheckbox.checked()) {
startChallenge(1);
} else {
let cycles = speedSlider.value();
speedSpan.html(cycles);
startChallenge(cycles);
let tempHighScore = 0;
// Which is the best bird?
let tempBestBird = null;
for (let i = 0; i < birds.length; i++) {
let s = birds[i].score;
if (s > tempHighScore) {
tempHighScore = s;
tempBestBird = birds[i];
}
}
// Is it the all time high scorer?
if (tempHighScore > highScore) {
highScore = tempHighScore;
bestBird = tempBestBird;
}
// Update DOM Elements
highScoreSpan.html(tempHighScore);
allTimeHighScoreSpan.html(highScore);
numBirds = birds.length;
}
drawCanvas();
scroll += 2;
}
function drawCanvas() {
drawBackground();
drawSinusoidalPattern();
for (let bird of birds) {
bird.show();
}
if (challengeCheckbox.checked()) {
aibird.show();
playerbird.show();
}
for (let pipe of pipes) {
pipe.show();
}
drawGameOver();
showScores();
}
function startChallenge(cycles) {
if (challengemode) {
select("#traincontainer").hide();
aibird.show();
playerbird.show();
} else {
select("#traincontainer").show();
}
for (let n = 0; n < cycles; n++) {
if (counter % 50 == 0) {
pipes.push(new Pipe());
}
counter++;
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
if (challengemode) {
if (pipes[i].hits(playerbird)) {
gameover(false);
} else if (pipes[i].hits(aibird)) {
gameover(true);
}
} else {
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);
score++;
if (score > maxScore) {
maxScore = score;
}
}
}
if (challengemode) {
if (playerbird.offScreen()) {
gameover(false);
} else if (aibird.offScreen()) {
gameover(true);
}
aibird.think(pipes);
aibird.update();
playerbird.update();
} else {
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 = [];
}
}
}
}
function showScores() {
if (challengemode) {
textSize(32);
text("score: " + score, 60, 32);
text("record: " + maxScore, 70, 64);
}
}
function drawGameOver() {
textSize(64);
textAlign(CENTER, CENTER);
fill(255);
stroke(0);
strokeWeight(5);
if (isOver && playerWon) {
console.log("PLAYER WON");
text("YOU WIN!!!", width / 2, height / 2);
} else if (isOver) {
console.log("PLAYER LOST");
text("YOU LOSE :(", width / 2, height / 2);
}
noStroke();
}
function gameover(playerWins) {
playerWon = playerWins;
maxScore = max(score, maxScore);
isOver = true;
noLoop();
}
function reset() {
isOver = false;
score = 0;
bgX = 0;
counter = 0;
pipes = [];
if (challengeCheckbox.checked()) {
pipes = [];
playerbird = new Bird(null, true);
aibird = new Bird(birdbrain);
}
gameoverFrame = frameCount - 1;
loop();
}
function keyPressed() {
if (key === " " && challengeCheckbox.checked()) {
playerbird.up();
if (isOver) reset(); //you can just call reset() in Machinelearning if you die, because you cant simulate keyPress with code.
}
if (key === "S" && !challengeCheckbox.checked()) {
let bird = birds[0];
console.log("SAVED BIRD WEIGHTS");
saveJSON(bird.brain, "bird.json");
}
}
function touchStarted() {
if (isOver) reset();
}
function drawBackground() {
for (let y = 0; y < height; y++) {
const n = map(y, 0, height, 0, 1);
const newc = lerpColor(c1, c2, n);
stroke(newc);
line(0, y, width, y);
}
}
function drawSinusoidalPattern() {
for (let x = 0; x < width; x++) {
const y = map(sin(radians(x + scroll)), -1, 1, 200, 600);
noStroke();
fill(255, 255, 255);
ellipse(x, y, 15, 15);
}
}