Categories
INTERACTON LAB

Processing code

import processing.sound.*; import processing.serial.*; int endTime; // Variable to store the end time int startTime; SoundFile sound1; SoundFile sound2; Serial serialPort; boolean gameEnded = false; float characterSize = 150; // Size of the character square float characterX, characterY; // Position of the character float characterSpeed = 25; // Speed of the character (increase this value for faster speed) float bubbleSize = 200; // Size of the bubbles float bubbleSpeed = 3; // Speed of the bubbles int numBubbles = 20; // Number of bubbles int score = 0; // Player's score float[] bubbleX; // Array to store bubble positions float[] bubbleY; // Array to store bubble positions String[] words; // Array to store bubble text float[] starX = new float[3]; float[] starY = new float[3]; float[] starSpeed = new float[3]; boolean openingScene = true; // Flag to indicate if the opening scene is being displayed PImage Image2;  ArrayList<String> chosenWords = new ArrayList<String>(); void addChosenWord(String word) { chosenWords.add(word); } void setup() { fullScreen(); characterX = width / 2; characterY = height / 2; PImage Image; Image = loadImage("star.png"); // Load star image Image2 = loadImage("galaxy.jpg"); sound1 = new SoundFile(this, "arcade_sound.wav" ); sound2 = new SoundFile(this, "drive.mp3"); sound2.loop(); bubbleX = new float[numBubbles]; bubbleY = new float[numBubbles]; words = new String[numBubbles]; for (int i = 0; i < numBubbles; i++) { bubbleX[i] = random(width); // Randomize starting X position bubbleY[i] = -bubbleSize * i; // Set starting Y position above the screen //bubbleText[i] = "Text " + i; // Set bubble text words[0] = "CREATIVE"; words[1] = "MINDFUL"; words[2] = "RELAXED"; words[3] = "EXPLORE"; words[4] = "TRAVEL MORE"; words[5] = "HEALTH"; words[6] = "HUMBLE"; words[7] = "PRODUCTIVE"; words[8] = "FORGIVE"; words[9] = "CONSISTENT"; words[10] = "HAVE FUN"; words[11] = "EMBRACE"; words[12] = "BE BRAVE"; words[13] = "APPRECIATE"; words[14] = "BALANCE"; words[15] = "INSIPRE"; words[16] = "IMPROVE"; words[17] = "BRIGHT"; words[18] = "SELF-LOVE"; words[19] = "FIND LOVE"; } String portName = "/dev/cu.usbmodem14201"; // Specify the correct port name int baudRate = 9600; // Must match the Arduino baud rate serialPort = new Serial(this, portName, baudRate); } void draw() { if (openingScene) { displayOpeningScene(); } else { playGame(); } //println(millis()); } void displayOpeningScene() { background(0, 0, 51); // Set dark blue background for (int i = 0; i < 100; i++) { float x = random(width); float y = random(height); float starSize = random(10, 15); float alpha = random(150, 255); fill(255, 255, 0, alpha); noStroke(); ellipse(x, y, starSize, starSize); } float shootingStarX = -1000; float shootingStarY = random(height / 2); float shootingStarSpeed = random(5, 10); shootingStarX += shootingStarSpeed; shootingStarY += shootingStarSpeed / 2; // Draw shooting star fill(255, 255, 0); noStroke(); triangle(shootingStarX, shootingStarY, shootingStarX - 50, shootingStarY - 20, shootingStarX - 50, shootingStarY + 20); // Set text properties fill(254, 255, 70); textAlign(CENTER, CENTER); textSize(110); float textX = width / 2; float textY = height / 2; text("CHOOSE YOUR 2024 GOAL", textX, textY); if (millis() > 18000) { openingScene = false; } } void playGame() // Resize the background image to match the full screen size { Image2.resize(displayWidth, displayHeight); // Set the resized image as the background background(Image2); // Rest of your code... // Rest of your code... fill(#6B9EB4); rectMode(CENTER); rect(characterX, characterY, characterSize, characterSize); for (int i = 0; i < 100; i++) { float x = random(width); float y = random(height); float starSize = random(3, 6); float alpha = random(150, 255); fill(255, 255, 0, alpha); noStroke(); ellipse(x, y, starSize, starSize); } float shootingStarX = -1000; float shootingStarY = random(height / 2); float shootingStarSpeed = random(5, 10); shootingStarX += shootingStarSpeed; shootingStarY += shootingStarSpeed / 2; fill(255, 255, 0); noStroke(); triangle(shootingStarX, shootingStarY, shootingStarX - 50, shootingStarY - 20, shootingStarX - 50, shootingStarY + 20); // Draw stars for (int i = 0; i < numBubbles; i++) { fill(#FEFF46); drawStar(bubbleX[i], bubbleY[i], bubbleSize); bubbleY[i] += bubbleSpeed; // Move the star downwards // Check if the star has reached the bottom of the screen if (bubbleY[i] > height + bubbleSize / 2) { fill(255); bubbleX[i] = random(width); // Randomize X position bubbleY[i] = -bubbleSize; // Reset Y position above the screen } // Check collision with the character if ((dist(characterX, characterY, bubbleX[i], bubbleY[i]) < characterSize / 2 + bubbleSize / 2) && !gameEnded) { score++; addChosenWord(words[i]); fill(255); bubbleX[i] = random(width); // Randomize X position bubbleY[i] = random(-bubbleSize*2, -height*2); // Reset Y position above the screen sound1.play(); } // Display text inside stars fill(0); textAlign(CENTER, CENTER); textSize(30); text(words[i], bubbleX[i], bubbleY[i]); } // Display score fill(255); textAlign(LEFT); textSize(80); text("Word counts: " + score, 30, 80); // Read serial data from Arduino while (serialPort.available() > 0) { String data = serialPort.readString().trim(); if (data.equals("1")) { characterX -= characterSpeed; // Move character to the left } else if (data.equals("0")) { characterX += characterSpeed; // Move character to the right } } characterX = constrain(characterX, characterSize / 2, width - characterSize / 2); characterY = constrain(characterY, characterSize / 2, height - characterSize / 2); // Check if the game should end if (score >= 10) { if (endTime == 0) { endTime = millis() + 2000; // Set the end time 2 seconds (2000 milliseconds) from now } else if (millis() >= endTime) { endGame(); // Call the endGame() function when the specified time has elapsed } } } void drawStar(float x, float y, float size) { float angle = TWO_PI / 10; float halfAngle = angle / 2.0; float radius1 = size / 2.0; float radius2 = radius1 / 2.5; beginShape(); for (float a = 0; a < TWO_PI; a += angle) { float sx = x + cos(a) * radius1; float sy = y + sin(a) * radius1; vertex(sx, sy); sx = x + cos(a + halfAngle) * radius2; sy = y + sin(a + halfAngle) * radius2; vertex(sx, sy); } endShape(CLOSE); } void endGame() { sound1.stop(); gameEnded = true; background(#000033); // Set dark blue background for (int i = 0; i < 3; i++) { starX[i] = random(width); starY[i] = random(height); starSpeed[i] = random(3, 6); } for (int i = 0; i < 3; i++) { starX[i] += starSpeed[i]; if (starX[i] > width) { starX[i] = 0; starY[i] = random(height); starSpeed[i] = random(3, 6); } float starSize = random(10, 20); float alpha = random(100, 200); fill(#FFFF66, alpha); ellipse(starX[i], starY[i], starSize, starSize); } fill(#FEFF46); textAlign(CENTER, CENTER); textSize(95); text("YOU GAINED "+ score + " AFFIRMATIONS ", width / 2, height / 5); textSize(70); text("MAY ALL YOUR WISH COME TRUE IN 2024", width / 2, height / 2); String[] tempwordlist = chosenWords.toArray(new String[chosenWords.size()]); String combinedString = join(tempwordlist," "); text(combinedString, width/2,height/1.5); } void keyPressed() { if (keyCode == 32) { // Space bar key code if (openingScene) { openingScene = false; // Start the game here } else { openingScene = true; // Restart the game here score = 0; chosenWords.clear(); // Clear the chosenWords list for (int i = 0; i < numBubbles; i++) { bubbleX[i] = random(width); bubbleY[i] = -bubbleSize * i; } startTime = millis(); } } } void endGame2() { if (openingScene) { // Display the opening scene background(0); fill(255); textSize(50); textAlign(CENTER, CENTER); text("Game Over", width / 2, height / 2 - 100); text("Score: " + score, width / 2, height / 2); text("Press SPACE to restart", width / 2, height / 2 + 100); } else { // Ending scene with chosen words background(Image2); fill(#6B9EB4); rectMode(CENTER); rect(characterX, characterY, characterSize, characterSize); // Display the chosen words in a list fill(255); textSize(20); textAlign(LEFT, TOP); float wordListX = 30; float wordListY = 150; for (int i = 0; i < chosenWords.size(); i++) { text(chosenWords.get(i), wordListX, wordListY + i * 30); } for (int i = 0; i < 100; i++) { float x = random(width); float y = random(height); float starSize = random(3, 6); float alpha = random(150, 255); fill(255, 255, 0, alpha); noStroke(); ellipse(x, y, starSize, starSize); } float shootingStarX = -1000; float shootingStarY = random(height / 2); float shootingStarSpeed = random(5, 10); shootingStarX += shootingStarSpeed; shootingStarY += shootingStarSpeed / 2; fill(255, 255, 0); noStroke(); triangle(shootingStarX, shootingStarY, shootingStarX - 50, shootingStarY - 20, shootingStarX - 50, shootingStarY + 20); // Draw stars for (int i = 0; i < numBubbles; i++) { fill(#FEFF46); drawStar(bubbleX[i], bubbleY[i], bubbleSize); bubbleY[i] += bubbleSpeed; // Move the star downwards // Check if the jstar has reached the bottom of the screen if (bubbleY[i] > height + bubbleSize / 2) { fill(255); bubbleX[i] = random(width); // Randomize X position bubbleY[i] = -bubbleSize; // Reset Y position above the screen } // Check collision with the character if ((dist(characterX, characterY, bubbleX[i], bubbleY[i]) < characterSize / 2 + bubbleSize / 2) && !gameEnded) { score++; fill(255); bubbleX[i] = random(width); // Randomize X position bubbleY[i] = random(-bubbleSize*2, -height*2); // Reset Y position above the screen sound1.play(); } // Display text inside stars fill(0); textAlign(CENTER, CENTER); textSize(30); text(words[i], bubbleX[i], bubbleY[i]); } // Display score fill(255); textAlign(LEFT); textSize(80); text("Word counts: " + score, 30, 80); // Read serial data from Arduino while (serialPort.available() > 0) { String data = serialPort.readString().trim(); if (data.equals("1")) { characterX -= characterSpeed; // Move character to the left } else if (data.equals("0")) { characterX += characterSpeed; // Move character to the right } } characterX = constrain(characterX, characterSize / 2, width - characterSize / 2); characterY = constrain(characterY, characterSize / 2, height - characterSize / 2); if (score >= 10) { if (endTime == 0) { endTime = millis() + 2000; // Set the end time 2 seconds (2000 milliseconds) from now } else if (millis() >= endTime) { endGame(); // Call the endGame() function when the specified time has elapsed } } } }

Leave a Reply

Your email address will not be published. Required fields are marked *