I decided to create a physical controller for my simple version of Flappy Bird, that I coded for the OOP assignment. I tried to code a button which will control the movement of the object in the game, and with some help from Rick, I was able FINALLY figure it out! I set up a button state and used it in the draw function, since I have three states for the game – pressing the button will change the button state from 0 to 1 then change the state of the game also from 0 to 1.
Arduino Code:
int buttonPin = 2; int button; void setup() { Serial.begin(9600); Serial.println('0'); pinMode(buttonPin, INPUT); } void loop() { char inByte=Serial.read(); button = digitalRead(buttonPin); delay(0); Serial.println(button); }
Processing Code:
import processing.serial.*; Serial myPort; int state = 0; int score=0; boolean currentState = false; boolean prevState = false; int arduinoButton = 0; bird b = new bird(); pillar[] p = new pillar[3]; void setup(){ size(500,700); printArray(Serial.list()); String portname=Serial.list()[7]; println(portname); myPort = new Serial(this,portname,9600); myPort.clear(); myPort.bufferUntil('\n'); int i; for (i = 0; i < 3; i++){ p[i]=new pillar(i); }; } void draw(){ background(0); if (arduinoButton == 1 && prevState == false) { currentState = true; prevState = true; state = 1; } if (arduinoButton == 1) { b.jump(); } if (state==0){ // intro state text("Click to Play",155,240); b.move(p); } else if (state==1){ // start state b.move(p); b.drag(); b.checkCollisions(); rect(20,20,100,50); fill(255); text(score,30,58); } else { // end state rect(150,100,200,50); rect(150,200,200,50); fill(255); text("game over",170,140); text("score",180,240); text(score,280,240); } b.drawBird(); for(int i = 0;i<3;i++){ p[i].drawPillar(); p[i].checkPosition(); } stroke(255); textSize(32); } void mousePressed(){ state = 1; } void serialEvent(Serial myPort){ String s=myPort.readStringUntil('\n'); s=trim(s); if (s!=null){ arduinoButton = int(s); } println(arduinoButton); }