xxxxxxxxxx
299
// let capture;
// let moleImage;
// let moles = [];
// let score = 0;
// let holes = []; // Array to store hole positions
// let startTime;
// const gameDuration = 60; // Game duration in seconds
// function captureWebcam() {
// capture = createCapture(VIDEO, function(stream) {
// capture.hide();
// moleImage = capture.get(); // Capture the image from webcam
// capture.stop();
// setupGame();
// });
// }
// function setupGame() {
// createCanvas(640, 480);
// // Define hole positions
// for (let i = 0; i < 3; i++) {
// for (let j = 0; j < 3; j++) {
// holes.push({
// x: i * 200 + 80,
// y: j * 160 + 80
// });
// }
// }
// startTime = millis(); // Start the game timer
// setInterval(addMoles, 800); // Speed up mole appearance
// }
// function addMoles() {
// if (moles.length < 2) {
// let holeIndexes = [];
// for (let i = 0; i < holes.length; i++) {
// holeIndexes.push(i);
// }
// shuffle(holeIndexes, true);
// for (let i = 0; i < min(2, holes.length); i++) {
// createMole(holes[holeIndexes[i]].x, holes[holeIndexes[i]].y);
// }
// }
// }
// function createMole(x, y) {
// moles.push(new Mole(x, y, moleImage));
// }
// function setup() {
// // Leave this empty, setup is handled by captureWebcam()
// }
// function draw() {
// background(220);
// for (let i = 0; i < moles.length; i++) {
// moles[i].display();
// }
// textSize(32);
// fill(0);
// text(`Score: ${score}`, 10, 30);
// let currentTime = (millis() - startTime) / 1000;
// if (currentTime > gameDuration) {
// endGame();
// }
// }
// function mousePressed() {
// for (let i = 0; i < moles.length; i++) {
// if (moles[i].contains(mouseX, mouseY)) {
// moles.splice(i, 1);
// score++;
// break; // Only remove one mole per click
// }
// }
// }
// class Mole {
// constructor(x, y, img) {
// this.x = x;
// this.y = y;
// this.img = img;
// this.width = 80;
// this.height = 80;
// }
// display() {
// image(this.img, this.x, this.y, this.width, this.height);
// }
// contains(px, py) {
// return (
// px > this.x && px < this.x + this.width && py > this.y && py < this.y + this.height
// );
// }
// }
// function endGame() {
// noLoop(); // Stop the game
// background(220);
// textSize(40);
// fill(0);
// text(`Game Over! Your Score: ${score}`, 100, height / 2);
// }
let capture;
let moleImage;
let moles = [];
let score = 0;
let holes = [];
let startTime;
const gameDuration = 60;
let rVal = 0;
let alpha = 255;
let left = 0;
let right = 0;
let serialActive = false;
let serial;
function setUpSerial() {
navigator.serial.requestPort().then((port) => {
serial = new Serial(port);
serial.open({ baudrate: 9600 }).then(() => {
serialActive = true;
startReadSerial();
}).catch((error) => {
console.error(error);
});
}).catch((error) => {
console.error(error);
});
}
function draw() {
background(map(rVal, 0, 1023, 0, 255), 255, 255);
fill(255, 0, 255, map(alpha, 0, 1023, 0, 255));
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
text('rVal = ' + str(rVal), 20, 50);
text('alpha = ' + str(alpha), 20, 70);
}
if (mouseIsPressed) {
if (mouseX <= width / 2) {
left = 1;
} else {
right = 1;
}
} else {
left = right = 0;
}
}
function keyPressed() {
if (key == " ") {
setUpSerial();
}
}
function readSerial() {
serial.inputStream.getReader().read().then(({ value, done }) => {
if (!done) {
let data = new TextDecoder().decode(value);
let fromArduino = split(trim(data), ",");
if (fromArduino.length === 2) {
rVal = int(fromArduino[0]);
alpha = int(fromArduino[1]);
}
let sendToArduino = left + "," + right + "\n";
serial.outputStream.write(new TextEncoder().encode(sendToArduino)).then(() => {
readSerial(); // Continue reading serial data
}).catch((error) => {
console.error(error);
});
}
}).catch((error) => {
console.error(error);
});
}
function startReadSerial() {
readSerial();
}
function serialEvent() {
let data = serial.readStringUntil('\n');
if (data) {
let fromArduino = split(trim(data), ",");
if (fromArduino.length === 2) {
rVal = int(fromArduino[0]);
alpha = int(fromArduino[1]);
}
let sendToArduino = left + "," + right + "\n";
serial.write(sendToArduino);
}
}
function portOpen() {
console.log('Serial Port Open');
// You can start your mole-related setup here if needed
moleImage = capture.get();
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
holes.push({
x: i * 200 + 80,
y: j * 160 + 80
});
}
startTime = millis();
setInterval(addMoles, 800);
}
}
function addMoles() {
if (moles.length < 2) {
let holeIndexes = [];
for (let i = 0; i < holes.length; i++) {
holeIndexes.push(i);
}
shuffle(holeIndexes, true);
for (let i = 0; i < min(2, holes.length); i++) {
createMole(holes[holeIndexes[i]].x, holes[holeIndexes[i]].y);
}
}
}
function createMole(x, y) {
moles.push(new Mole(x, y, moleImage));
}
function draw() {
background(220);
for (let i = 0; i < moles.length; i++) {
moles[i].display();
}
textSize(32);
fill(0);
text(`Score: ${score}`, 10, 30);
let currentTime = (millis() - startTime) / 1000;
if (currentTime > gameDuration) {
endGame();
}
}
function serialEvent() {
let received = serial.readStringUntil('\n');
if (received) {
received = received.trim();
if (received === 'Button1') {
whackMole(0);
} else if (received === 'Button2') {
whackMole(1);
}
}
}
function whackMole(index) {
if (index < moles.length) {
moles.splice(index, 1);
score++;
}
}
class Mole {
constructor(x, y, img) {
this.x = x;
this.y = y;
this.img = img;
this.width = 80;
this.height = 80;
}
display() {
image(this.img, this.x, this.y, this.width, this.height);
}
}
function endGame() {
noLoop();
background(220);
textSize(40);
fill(0);
text(`Game Over! Your Score: ${score}`, 100, height / 2);
}
function printList(portList) {
for (let i = 0; i < portList.length; i++) {
console.log(i + ' ' + portList[i]);
}
}