xxxxxxxxxx
280
let gameState = "play";
// image variables
let fruits = [];
let bkg;
let title;
let addFruits = [];
let rState, yState, gState, bState, distanceState;
function preload() {
title = loadImage("assets/title.png");
for (let i = 0; i < 8; i++) {
fruits[i] = loadImage("assets/fruit" + i + ".png");
}
bkg = loadImage("assets/bkg.jpg");
}
function setup() {
createCanvas(windowWidth, windowHeight);
addFruits.push(new Fruit());
}
function draw() {
if (gameState == "start") {
instructionScreen();
} else if (gameState == "play") {
gameScreen();
}
}
function instructionScreen() {
background(255);
if (!serialActive) {
textSize(18);
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
push();
scale(0.7);
image(title, width/2 + 50, height/2);
pop();
}
function gameScreen() {
image(bkg, 0, 0, windowWidth, windowHeight);
for (let i = addFruits.length - 1; i >= 0; i--) {
addFruits[i].moveFruit();
addFruits[i].show();
if (rState == 1) {
addFruits.splice(i, 1);
} else if (yState == 1) {
addFruits.splice(i, 1);
} else if (gState == 1) {
addFruits.splice(i, 1);
} else if (bState == 1) {
addFruits.splice(i, 1);
}
}
if (frameCount % 100 == 0) {
addFruits.push(new Fruit());
}
// if (distanceState <= 10) {
// rect(width/2, height/2, 120, 100);
// text("Please step back a little to resume", width/2, height/2);
// noLoop();
// } else {
// loop();
// }
}
// function scoreDisplay() {
// textSize(60);
// noStroke();
// textStyle(BOLD);
// fill(255);
// let scoreMark0 = text("X", 30, 60);
// let scoreMark1 = text("X", 80, 60);
// let scoreMark2 = text("X", 130, 60);
// scoreMarker.push(scoreMark0);
// scoreMarker.push(scoreMark1);
// scoreMarker.push(scoreMark2);
// }
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
if (keyCode == ENTER) {
gameState = "play";
}
}
class Fruit {
constructor() {
this.x = random(50, windowWidth - 50);
this.y = windowHeight + 10;
this.size = random(10, 20);
// this.speed = random(0.5, 1);
this.speed = random(1, 5);
this.gravity = 0.1;
this.fruit = random(fruits);
// this.fruit = fruits[0];
}
moveFruit() {
this.y -= this.speed;
this.speed += this.gravity;
// print(this.y)
}
// checkFruitSliced() {
// if(rButton == 1) {
// }
// }
show() {
image(this.fruit, this.x, this.y);
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 4) {
// only store values here
// do everything with those values in the main draw loop
rState = fromArduino[0];
yState = fromArduino[1];
gState = fromArduino[2];
bState = fromArduino[3];
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = "0,0,0,0" + "\n"; // need to make sure this matches the length of Arduino data
writeSerial(sendToArduino);
}
}
//Arudino Code
/*
// Distance sensor:
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// Constants for button pins:
const int RED = 12;
const int YELLOW = 11;
const int GREEN = 10;
const int BLUE = 9;
const int LED_PIN = 6;
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
void setup() {
Serial.begin(9600);
pinMode(RED, INPUT);
pinMode(YELLOW, INPUT);
pinMode(GREEN, INPUT);
pinMode(BLUE, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
// start the handshake
while (Serial.available() <= 0) {
Serial.println("0,0"); // send a starting message
delay(300); // wait 1/3 second
}
}
void loop() {
// wait for data from p5 before doing something
while (Serial.available()) {
int left = Serial.parseInt();
int right = Serial.parseInt();
if (Serial.read() == '\n') {
// digitalWrite(2, left);
// digitalWrite(5, right);
checkButtons();
// checkDistance();
}
}
}
void checkButtons() {
int rState = digitalRead(RED);
delay(1);
int yState = digitalRead(YELLOW);
delay(1);
int gState = digitalRead(GREEN);
delay(1);
int bState = digitalRead(BLUE);
delay(1);
Serial.print(rState);
Serial.print(",");
Serial.print(yState);
Serial.print(",");
Serial.print(gState);
Serial.print(",");
Serial.println(bState);
// Serial.print(",");
// if (rState == HIGH) {
// analogWrite(LED_PIN, 255);
// delay(100);
// analogWrite(LED_PIN, 0);
// } else if (yState == HIGH) {
// analogWrite(LED_PIN, 255);
// delay(100);
// analogWrite(LED_PIN, 0);
// } else if (gState == HIGH) {
// analogWrite(LED_PIN, 255);
// delay(100);
// analogWrite(LED_PIN, 0);
// } else if (bState == HIGH) {
// analogWrite(LED_PIN, 255);
// delay(100);
// analogWrite(LED_PIN, 0);
// } else {
// analogWrite(LED_PIN, 0);
// }
}
void checkDistance() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
// Serial.print("Distance: ");
int distanceState = digitalRead(distance);
Serial.println(distanceState);
// Serial.println(" cm");
if (distance < 10) {
// digitalWrite(LED_PIN, HIGH);
analogWrite(LED_PIN, 255);
} else {
digitalWrite(LED_PIN, LOW);
}
}
*/