import processing.sound.*;
SoundFile sound;
import processing.serial.*;
Serial serial;
int handX=500, handY=500;
float handVX=0, handVY=0;//add
int score=0;
float[] eggX=new float[200];
float[] eggY=new float[200];
boolean[] eggDie=new boolean[200];
void setup() {
sound = new SoundFile(this, "m.mp3");
sound.play();
size(1200, 600);
serial = new Serial(this, "COM7", 9600);
for (int i=0; i<200; i++) {
eggX[i]=random(100, 1100);
eggY[i]=-random(800);
eggDie[i]= false;
}
}
void draw() {
background(255, 255, 191);
stroke(255);
fill(230,26,100);
rect(handX-200/2, handY-10/2, 200,10);
fill(255);
handX += handVX;
getData();//add
for (int i=0; i<200; i++) {
if(i%10 == 0){
if (eggDie[i]==true)
continue;
ellipse(eggX[i], eggY[i], 70, 100);
eggY[i]+=15;
if (eggY[i]>700)
eggY[i]=-400;
if (dist(handX, handY, eggX[i], eggY[i])<70) {
eggDie[i]=true;
score+=500;
}
}
fill(230,26,100);
if(score == 10000){
textMode(CENTER);
text("well done",490,200);}
else{text("Score "+score, 450, 200);}
}
textSize(50);
noFill();
}
void getData(){
if(serial.available()>0){
int val = serial.read();
println(val);
handVX = (val-63)/8.0;
}
//if(keyPressed && keyCode==LEFT) handVX= -2;
//if(keyPressed && keyCode==RIGHT)handVX= +2;
}
沒有留言:
張貼留言