xxxxxxxxxx
75
let myPoseNet;
let video;
let poseResults;
let catPos, dogPos;
let catSong, dogSong;
let xLoc;
let yLoc;
let xDirection;
let yDirection;
let r;
let g;
let b;
function preload() {
coinSound = loadSound("coin.wav");
coin = loadImage("Gold_Coin.png");
star = loadImage("Star.png");
}
function setup() {
video = createCapture(VIDEO);
video.hide();
createCanvas(640, 480);
fill(255, 0, 0);
myPoseNet = ml5.poseNet(video, gotModel);
textSize(100);
catPos = [100, height / 2];
dogPos = [width - 100, height / 2];
xLoc = random(width);
yLoc = random(height);
xDirection = 7;
yDirection = 6;
}
function gotModel() {
console.log(myPoseNet);
myPoseNet.on("pose", gotPose);
}
function gotPose(results) {
if (results && results[0]) {
poseResults = results;
const newnose = poseResults[0].pose.nose;
const toCoin = dist(newnose.x, newnose.y, xLoc, yLoc);
if (toCoin < 80 && !coinSound.isPlaying()) {
coinSound.play();
}
}
}
function draw() {
image(video, 0, 0, width, height);
if (poseResults && poseResults[0]) {
const nose = poseResults[0].pose.nose;
image(star, nose.x - 25, nose.y - 30, 80, 80);
}
image(coin, xLoc, yLoc, 50, 50);
xLoc = xLoc + xDirection;
yLoc = yLoc + yDirection;
if (xLoc + 45 >= width || xLoc < 0) {
xDirection = -xDirection;
}
if (yLoc + 45 >= height || yLoc < 0) {
yDirection = -yDirection;
}
}
function mouseClicked() {
xDirection = xDirection + 10;
yDirection = yDirection + 10;
}