import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
int high;
int blockWidth;
int tunelHeight = 100;
int radius = 10;
int mapList[] = {70,70,90,80,100,80,70,50,70,100,150,100,70};
void setup() {
size(640,360);
frameRate(24);
stroke(255);
background(50);
high =130;
blockWidth = width / mapList.length + 1;
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 115200);
}
void draw() {
background(50);
drawMap();
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
println(val);
if(val>10){
high-=2;
}
else{
high+=2;
}
}
int x = (millis()/20) % width;
ellipse(x,high,radius*2,radius*2);
if (isCollapsed(x))
{
background(50);
noLoop();
}
}
boolean isCollapsed(int x){
int index = (x-1) / blockWidth;
int min = mapList[index] + radius;
int max = tunelHeight + mapList[index] - radius;
if (high < min || high > max) return true;
return false;
}
void keyPressed(){
if (keyCode == UP){
high -= 5;
}
}
void drawMap()
{
beginShape();
vertex(0,0);
for (int i = 0; i < mapList.length; i++){
vertex(i * blockWidth,mapList[i]);
vertex((i+1)*blockWidth,mapList[i]);
}
vertex(width,0);
endShape();
beginShape();
vertex(0,height);
for (int i = 0; i < mapList.length; i++){
vertex(i * blockWidth, tunelHeight+mapList[i]);
vertex((i+1) * blockWidth, tunelHeight+mapList[i]);
}
vertex(width,height);
endShape();
}