import processing.serial.*;
Serial myPort; // Create a variable for the serial port
int circleFillColor = color(255); // Default fill color for the circle
char[] colorKeys = {'R', 'G', 'B'}; // Keys corresponding to colors
int points = 0; // Player's points
int missedColors = 0; // Counter for missed colors
boolean gameStarted = false; // Flag to start the game
void setup() {
size(800, 600); // Set the size of the Processing window
myPort = new Serial(this, "COM3", 9600); // Replace "COM3" with your Arduino's port
}
void draw() {
background(220); // Set background color
if (gameStarted) {
// Display the circle based on the selected color
fill(circleFillColor);
ellipse(width / 2, height / 2, 100, 100); // Display a circle at the center of the window
}
// Display score and missed colors
textAlign(CENTER);
displayInfo1();
displayInfo2();
displayInstructions(); // Display game instructions
// Check for game over condition
if (missedColors >= 3) {
gameOver();
}
// Read data from the serial port
while (myPort.available() > 0) {
String wasteType = myPort.readStringUntil('\n'); // Read data from serial port
if (wasteType != null) {
wasteType = wasteType.trim(); // Remove whitespace
// Set circle color based on received distance signal
if (wasteType.equals("RECYCLABLE")) {
circleFillColor = color(0, 255, 0); // Green for recyclable
} else if (wasteType.equals("NON_RECYCLABLE")) {
circleFillColor = color(255, 0, 0); // Red for non-recyclable
} else if (wasteType.equals("COMPOSTABLE")) {
circleFillColor = color(0, 0, 255); // Blue for compostable
}
gameStarted = true; // Start the game when signal received
}
}
}
void keyPressed() {
if (gameStarted) {
char keyPressed = Character.toUpperCase(key);
for (int i = 0; i < colorKeys.length; i++) {
if (keyPressed == colorKeys[i]) {
if (keyPressed == getColorChar(circleFillColor)) {
// If player presses the correct key corresponding to the displayed color
points++;
missedColors = 0; // Reset missed colors
gameStarted = false; // Set to false to refresh the color
pickRandomColor(); // Choose a new random color
} else {
missedColors++;
if (missedColors >= 3) {
gameOver();
}
}
break; // Exit loop after handling input
}
}
} else {
// Start the game when the ENTER key is pressed
if (keyCode == ENTER) {
gameStarted = true;
pickRandomColor(); // Choose a random color to start
}
}
}
void displayInfo1() {
// Display score and missed colors
fill(0);
textSize(35);
text("Points: " + points, width / 2, 40);
}
void displayInfo2() {
// Display score and missed colors
fill(0);
textSize(20);
text("Missed: " + missedColors, width / 2, 60);
}
void displayInstructions() {
// Display game instructions at the bottom
textAlign(CENTER);
fill(50);
textSize(16);
text("Play, Learn, Act: Games for Sustainable Impact – Aligning Fun with Global Goals! \n GREEN for RECYCLABLE RED for NON RECYCLABLE and BLUE for COMPOSTABLE \n Press 'R', 'G', or 'B' when corresponding color appears.\n If you miss 3 in row you'll lose.", width / 2, height - 80);
}
void pickRandomColor() {
// Pick a random color and display the corresponding color
int randomIndex = int(random(colorKeys.length));
char selectedColor = colorKeys[randomIndex];
if (selectedColor == 'R') {
circleFillColor = color(255, 0, 0); // Red
} else if (selectedColor == 'G') {
circleFillColor = color(0, 255, 0); // Green
} else if (selectedColor == 'B') {
circleFillColor = color(0, 0, 255); // Blue
}
}
char getColorChar(int fillColor) {
// Get the character corresponding to the color
if (fillColor == color(0, 255, 0)) {
return 'G';
} else if (fillColor == color(255, 0, 0)) {
return 'R';
} else if (fillColor == color(0, 0, 255)) {
return 'B';
}
return ' ';
}
void gameOver() {
// Display game over message
fill(0, 0, 2);
textSize(80);
textAlign(CENTER);
text("Game Over!", width / 2, height / 2);
// Show information about waste recycling in a new window
showRecyclingInfo();
}
void showRecyclingInfo() {
// Open a new window to display waste recycling information
PGraphics recyclingWindow = createGraphics(780, 580);
recyclingWindow.beginDraw();
recyclingWindow.background(240);
recyclingWindow.fill(0);
recyclingWindow.textAlign(CENTER);
recyclingWindow.textSize(25);
recyclingWindow.text("Let's make this world awesome together!", recyclingWindow.width / 2, 100);
// Add your recycling information text or graphics here
recyclingWindow.text("Hey, Want to make a splash in saving our planet? \n Here's the deal: \n Sort your waste like a pro – \n recycle plastics, papers, and cans. \n Compost those food scraps for a happy garden. \n Reduce, reuse, recycle – it's your superhero trio against waste! \n Let's team up and make our world cleaner, cooler, and super awesome!", recyclingWindow.width / 2, 150);
recyclingWindow.text("Join the movement for a sustainable future!", recyclingWindow.width / 2, 450);
recyclingWindow.endDraw();
image(recyclingWindow, width / 2 - recyclingWindow.width / 2, height / 2 - recyclingWindow.height / 2);
}