xxxxxxxxxx
103
// Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/147-chrome-dinosaur.html
// https://youtu.be/l0HoJHc-63Q
// Google Chrome Dinosaur Game (Unicorn, run!)
// https://editor.p5js.org/codingtrain/sketches/v3thq2uhk
let unicorn;
let uImg;
let tImg;
let bImg;
const trains = [];
const spacing = 80;
let timer = spacing;
let r = 1;
let soundClassifier;
let useSound;
let timerP;
function preload() {
const options = {
probabilityThreshold: 0.95
};
soundClassifier = ml5.soundClassifier('SpeechCommands18w', options);
uImg = loadImage('unicorn.png');
tImg = loadImage('train.png');
bImg = loadImage('background.jpg');
}
// function mousePressed() {
// trains.push(new Train());
// }
function setup() {
createCanvas(800, 450);
useSuond = select('#voice');
timerP = createP(`Timer: ${timer}`);
createP(`Code for Spawning of Trains:<br><br>
<pre>
timer--;
if (timer <= 0) {
timer = 0;
r = random(1);
if (r < 0.015) {
trains.push(new Train());
timer = spacing;
}
}</pre>`);
unicorn = new Unicorn();
soundClassifier.classify(gotCommand);
}
function gotCommand(error, results) {
if (error) {
//console.error(error);
}
//console.log(results[0].label, results[0].confidence);
const checked = useSuond.checked();
if (results[0].label == 'up' && checked) {
unicorn.jump();
}
}
function keyPressed() {
//const checked = useSuond.value();
if (key == ' '/* && !checked*/) {
unicorn.jump();
}
}
function draw() {
timer--;
if (timer <= 0) {
timer = 0;
r = random(1);
if (r < 0.015) {
trains.push(new Train());
timer = spacing;
}
}
timerP.html(`Train coming after ${nf(timer/frameRate(), 1, 2)}`);
background(bImg);
for (let t of trains) {
t.move();
t.show();
if (unicorn.hits(t)) {
console.log('game over');
noLoop();
}
}
unicorn.show();
unicorn.move();
}