1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import processing.serial.*; Serial myPort; int val; boolean isPlaying = false; Bubble b; Bubble bub; Bubble bubl; Timer tim; void setup() { size(600, 300); println(Serial.list()); String portName = Serial.list()[11]; myPort = new Serial(this, portName, 9600); myPort.readStringUntil('\n'); background(0); b = new Bubble(); bub = new Bubble(20); bubl = new Bubble(); tim = new Timer(); } void draw(){ if (myPort.available() > 0) { val = myPort.read(); } if (val == 0) { isPlaying = false; } else if (val == 1) { isPlaying = true; } if (isPlaying == true) { background(0); tim.Timer(); b.update(); b.renderBubble(); b.checkWorld(); bub.update(); bub.renderBubble(); bub.checkWorld(); b.bounceOff(bub); bub.bounceOff(b); bub.bounceOff(bubl); bubl.update(); bubl.renderBubble(); bubl.checkWorld(); bub.easyE(); b.GameOver(); bubl.GameOver(); } } void keyPressed(){ b.changeFill(); bub.changeFill(); } void serialEvent(Serial myP) { if (myPort == null) return; isPlaying = false; String input = myPort.readStringUntil('\n'); if(input != null) { input = input.trim(); isPlaying = true; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
Bubble(float localDiam) { diam = localDiam; fillBub = color(random(127, 255), random(127, 255), random(127, 255)); xPos = random(diam/2, width-diam/2); yPos = random(diam/2, height-diam/2); xSpeed = random(-5.0, 5.0); ySpeed = random(-5.0, 5.0); changexSpeed = random(-3.0, 5.0); changeySpeed = random(-3.0, 5.0); } Bubble() { diam = random(10.0, 50.0); fillBub = color(random(127, 255), random(127, 255), random(127, 255)); xPos = random(diam/2, width-diam/2); yPos = random(diam/2, height-diam/2); xSpeed = random(-5.0, 5.0); ySpeed = random(-5.0, 5.0); changexSpeed = random(-3.0, 5.0); changeySpeed = random(-3.0, 5.0); } void update(){ fill(fillBub); xPos = xPos+xSpeed; yPos = yPos+ySpeed; } void renderBubble() { ellipse(xPos, yPos, diam, diam); noStroke(); } void checkWorld() { if(xPos > width-diam/2 || xPos < diam/2) { xSpeed = xSpeed * -1; } // this needs to be fixed if(yPos < diam/2) { ySpeed = ySpeed * -1; } } void GameOver(){ if(yPos > height || yPos > height-diam/2) { background(0); textSize(20); text("Game Over", 250, 150); fill(255); } } void bounceOff(Bubble other) { float d = dist(xPos, yPos, other.xPos, other.yPos); if(d <= diam/2 + other.diam/2) { ySpeed = ySpeed* -1; xSpeed = xSpeed* -1; } } void easyE(){ float targetX = mouseX; float dx = targetX - xPos; xPos += dx * easing; float targetY = mouseY; float dy = targetY - yPos; yPos += dy * easing; } void changeFill(){ fillBub = color(random(127, 255), random(127, 255), random(127, 255)); } void changeSpeed() { xSpeed = random(-3.0, 5.0); ySpeed = random(-3.0, 5.0); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Timer { int time; float wait; boolean timer; Timer(){ time = millis(); smooth(); wait = random(1000.0, 5000.0); } void Timer(){ if(millis() - time >= wait){ timer =! timer; b.changeSpeed(); time = millis(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
int fsrAnalogPin = A0; int fsrReading; void setup() { Serial.begin(9600); pinMode(fsrAnalogPin, INPUT); } void loop() { // read the input on analog pin 0: fsrReading = analogRead(fsrAnalogPin); Serial.print("Analog reading = "); Serial.println(fsrReading); delay(100); if(fsrReading < 400) { Serial.write(0); } if(fsrReading >= 400) { Serial.write(1); } delay(100); } |
I was having serious issues with hooking the fsr up to processing. This was meant to be a “are you ready?!” kind of sensor. So, that the moment the user holds/presses the sensor the game begins (just because physical sensors are more fun than regular keys). But, I sort of failed because the game was just laggy without fsr reading but flowed nicely with fsr reading.