xxxxxxxxxx
217
let classifier;
// Model URL
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/9nDial15G/';
let label= "waiting.... ";
let video;
let dino;
let dinoRun_1;
let dinoRun_2;
let dinoJump;
let dinoDeadJump;
let dinoDeadRun;
let cactusSmall;
let cactusBig;
let cacti;
let replayButton;
let gameOverText;
let cloudImg;
let obstacles = [];
let clouds = [];
function preload() {
dinoRun_1 = loadImage('dinoRun_1.png');
dinoRun_2 = loadImage('dinoRun_2.png');
dinoJump = loadImage('dinoJump.png');
cactusSmall = loadImage('cactusSmall.png');
cactusBig = loadImage('cactusBig.png');
cacti = loadImage('cactusMult.png');
dinoDeadJump = loadImage('dinoJumpDead.png');
dinoDeadRun = loadImage('dinoRunDead.png');
replayButton = loadImage('replayButton.png')
gameOverText = loadImage('gameOver.png')
cloudImg = loadImage('cloud.png');
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
function setup() {
createCanvas(windowWidth, windowHeight);
video = createCapture(VIDEO);
dino = new Dino();
video.size(320, 240);
video.hide();
flippedVideo = ml5.flipImage(video);
// Start classifying
classifyVideo();
}
function keyPressed() {
if (key == ' ') {
if (dino.jumping == false)
dino.jump();
}
}
function mousePressed() {
if (!dino.dead) {
if (dino.jumping == false)
dino.jump();
}
}
let obstacleTimer = 0;
let randAddition = 0;
let minTimeBetweenObstacles = 60;
let cloudTimer = 0;
let randCloudAddition = 40;
let minTimeBetweenClouds = 50;
function draw() {
//console.log(clouds.length)
updateObstacles();
updateClouds();
background(255);
for (let cloud of clouds) {
cloud.move();
cloud.show();
deleteClouds(cloud);
}
if (!dino.dead) {
dino.show();
dino.move();
for (let c of obstacles) {
c.move();
c.show();
deleteObstacle(c);
if (dino.hits(c)) {
console.log('game over');
dino.dead = true;
dino.gravity = 0;
dino.vy = 0;
}
}
} else {
gameOverAnimation();
if (mouseIsPressed) {
resetParameters();
redraw()
}
}
}
function updateObstacles() {
obstacleTimer++;
if (obstacleTimer > minTimeBetweenObstacles + randAddition) {
addObstacles();
}
}
function updateClouds() {
cloudTimer++;
if (cloudTimer > minTimeBetweenClouds + randCloudAddition) {
addClouds();
}
}
function addObstacles() {
let typeInput = floor(random(3));
randAddition = floor(random(50));
obstacles.push(new Cacti(typeInput));
obstacleTimer = 0;
}
function addClouds() {
randCloudAddition = floor(random(200));
clouds.push(new Cloud());
cloudTimer = 0;
}
function deleteObstacle(obs) {
if ((obs.x + obs.w) < 0)
obstacles.shift();
}
function deleteClouds(deadCloud) {
if ((deadCloud.x + deadCloud.w) < 0) {
clouds.shift();
//console.log("cloud deleted");
}
}
function resetParameters() {
dino.run = true;
dino.jumping = false;
dino.gravity = 2;
dino.y = dino.base;
obstacles = [];
obstacleTimer = 0;
randAddition = 0;
dino.dead = false;
}
function gameOverAnimation() {
dino.show();
dino.move();
for (let c of obstacles) {
c.vx = 0;
c.show();
c.move();
}
push();
imageMode(CENTER);
image(replayButton, width / 2, height / 2, 60, 60);
image(gameOverText, width / 2, height / 3);
pop();
}
function control() {
if (label === 'jump') {
if (dino.jumping == false)
dino.jump();
}
}
function classifyVideo(){
classifier.classify(video, gotResults);
}
function gotResults(error, results){
if(error){
console.error(error);
return;
}
label= results[0].label;
control();
classifyVideo();
}