xxxxxxxxxx
300
/// Code adapted from p5 Serialport resources:
/// https://github.com/p5-serial/p5.serialport
/// by David Rios IMALR 07-27
/// and https://editor.p5js.org/ehersh/sketches/Hk52gNXR7, https://editor.p5js.org/jaf765/sketches/zL2nY7K4v
//Image from: https://www.flaticon.com/free-icon/standing-male-silhouette-with-raised-arms_46994?term=person%20with%20arms%20raised&page=1&position=2&page=1&position=2&related_id=46994&origin=search
/// adapted by Alexis Sanders 8/4
let serial; // variable for serial object
let options = {
baud: 9600, // Serial baud Rate
};
let port = "COM3"; // write your port info here
let score;
let y;
let x;
let speed;
let size;
let gameCounter;
let redImg, blueImg, greenImg, yellowImg, blackImg;
let col;
let ball;
let xMouse;
let sensors;
let redBut, blueBut, greenBut, yellowBut;
let levelTwo = false;
let levelThree = false;
let easing = 500;
let newX=1;
let dx;
function preload() {
redImg = loadImage("red person.svg");
blueImg = loadImage("blue person.svg");
greenImg = loadImage("green person.svg");
yellowImg = loadImage("yellow person.svg");
blackImg = loadImage("black person.svg");
serial = new p5.SerialPort(options); // Create the serial object
let portlist = serial.list(); // get an array of available serial ports
// Register some callbacks
serial.on("connected", serverConnected); // print feedback on connection
serial.on("list", gotList); // print out list of available ports
serial.on("error", gotError); // print out any errors when they occur
serial.on("open", gotOpen); // print out feedback when the port is opened
serial.on("data", gotData); // callback for when p5 receives data from arduino
serial.open(port); // open the defined serial port
}
function setup() {
createCanvas(windowWidth, 500);
score = 0;
x = width / 2;
size = 30;
y = 0;
speed = 3;
col = ["red", "blue", "lime", "yellow"];
ball = new Ball(x, y, size, random(col), speed);
alert("Welcome! \n\nThe goal of this game is to collect circles to move through the levels. Use the potentiometer to move the character across the bottom of the screen. Press and hold the buttons to match the box to the color of the circle. Otherwise the points won't count.\n\nHave fun!")
}
function draw() {
start();
}
function gotData() {
let currentString = serial.readStringUntil("\r\n"); // read incoming sensor info
if (currentString) {
sensors = split(currentString, ",");
// console.log(sensors);
xMouse = map(int(sensors[0]), 0, 1023, 50, width - 50);
dx = int(xMouse - newX);
newX += dx * easing
// console.log(xMouse, newX, dx);
blueBut = sensors[1];
redBut = sensors[2];
greenBut = sensors[3];
yellowBut = sensors[4];
serial.write("x");
}
// console.log(ball);
}
// We are connected and ready to go
function serverConnected() {
console.log("We are connected!");
}
// Got the list of ports
function gotList(thelist) {
// theList is an array of their names
for (let i = 0; i < thelist.length; i++) {
// Display in the console
console.log(i + " " + thelist[i]);
}
}
// Connected to our serial device
function gotOpen() {
console.log("Serial Port is open!");
serial.write("x");
}
// disconnected from our serial device
function gotClose() {
console.log("Serial Port is closed!");
}
// Ut oh, here is an error, let's log it
function gotError(theerror) {
console.log(theerror);
}
function start() {
bgColor();
fill("black");
textSize(14);
text("Score: " + score, 20, 30);
imageMode(CENTER);
newPerson();
ball.display();
if (ball.y > height - 100) {
end();
}
if (ball.y > 320 && ball.x > xMouse - 30 && ball.x < xMouse + 30) {
checkScore();
}
if (score == 20) {
newLevel2();
}
if (score == 20 && levelTwo == true) {
newLevel3();
}
if (score == 10 && levelThree == true) {
win();
}
}
function bgColor() {
if (levelTwo == false && levelThree == false) {
background('lightcyan');
} else if (levelTwo == true && levelThree == false) {
background('peachpuff')
} else if (levelTwo == false && levelThree == true) {
background('lavender');
}
}
function newBall() {
ball.colr = random(col);
ball.y = 0;
if (levelTwo == false && levelThree == false) {
ball.speed += 0.5;
} else if (levelTwo == true && levelThree == false) {
ball.speed+=1;
} else if (levelTwo == false && levelThree == true) {
ball.speed+=1.5;
}
ball.x = int(random(50, windowWidth - 50));
start();
}
function newPerson() {
// console.log(ball);
if (redBut == 1) {
image(redImg, xMouse, height - 90);
} else if (blueBut == 1) {
image(blueImg, xMouse, height - 90);
} else if (greenBut == 1) {
image(greenImg, xMouse, height - 90);
} else if (yellowBut == 1) {
image(yellowImg, xMouse, height - 90);
} else {
image(blackImg, xMouse, height - 90);
}
}
function checkScore() {
if (ball.colr == "red" && redBut == 1) {
image(redImg, xMouse, height - 90);
score+=1;
newBall();
} else if (ball.colr == "blue" && blueBut == 1) {
image(blueImg, xMouse, height - 90);
score+=1;
newBall();
} else if (ball.colr == "lime" && greenBut == 1) {
image(greenImg, xMouse, height - 90);
score+=1;
newBall();
} else if (ball.colr == "yellow" && yellowBut == 1) {
image(yellowImg, xMouse, height - 90);
score+=1;
newBall();
} else {
end();
}
}
function newLevel2() {
background('lightgreen');
score = 0;
let newLevelTwo = alert ("CONGRATS! You completed the first level. Click 'ok' to start the second level.");
ball.y = 0;
ball.speed = 5;
ball.x = int(random(50, windowWidth-50));
levelTwo = true;
levelThree = false;
}
function newLevel3() {
score = 0;
let newLevelThree = alert ("CONGRATS! You completed the second level. Click 'ok' to start the third level.");
ball.y = 0;
ball.speed = 7;
ball.x = int(random(50, windowWidth-50));
levelTwo = false;
levelThree = true;
}
function reset() {
ball.y = y;
ball.speed = speed;
score = 0;
levelTwo = false;
levelThree = false;
}
function win() {
let winGame = prompt("CONGRATS! You have completed the game! If you want to play again, type yes. Otherwise, type no and the game will end.");
winGame = winGame.toLowerCase();
if (winGame == "yes") {
reset();
start();
} else if (winGame == "no") {
ball.y = 0;
ball.speed = 0;
} else {
alert("Sorry I'm not sure I understand what you mean. Please try again.")
}
}
function end() {
// alert("YOU LOSE. Click ok to play again");
let again = prompt("You lost. Do you want to play again?");
again = again.toLowerCase();
if (again == "yes") {
reset();
start();
} else if (again == "no") {
ball.y=0;
ball.speed=0;
} else {
alert("Sorry I'm not sure I understand what you are saying. Please try again.")
}
}
class Ball {
constructor(x, y, size, colr, speed) {
this.x = x;
this.y = y;
this.size = size;
this.colr = colr;
this.speed = speed;
}
display() {
fill(this.colr);
this.y += this.speed;
circle(this.x, this.y, this.size);
}
}