processing
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
int bgY=0;
int count=0;
int N=4;
int CoinX=110, CoinY=520;
int BadX=110, BadY=520;
int i=1;
int S=0;
int score=0;
int tmp=0;
PImage imgBall, bg2, bg, ooo;
float [] ballX=new float[50], ballY=new float[50];
int die=0;
int touch=0;
float [] thingX=new float[4], thingY= new float[4], thingType=new float[4];
float [] thingX1=new float[4], thingY1= new float[4], thingType1=new float[4];
void setup() {
String [] all = Serial.list();
//String portName = all[all.length-1];
//String portName = Serial.list()[0];
//myPort = new Serial(this, portName, 9600);
bg2 = loadImage("bg.jpg");
bg=loadImage("bad.jpg");
imgBall=loadImage("can.jpg");
ooo=loadImage("coin.jpg");
size(400, 600);
for (int i=0;i<N;i++) {
newThing(i);
}
}
int countDown=10*30;
int downDown=10*155;
void draw()
{
//if ( myPort.available() > 0) { // If data is available,
//val = myPort.read();
//if(val=='2') CoinX+=50;
//if (CoinX<150) CoinX=150;
//if(val=='3')CoinX-=50;
//if (CoinX>250) CoinX=250; // read it and store it in val
// }
background(255); // Set background to white
if (val == '0')
{ // If the serial value is 0,
fill(0, 0, 0); // set fill to black
rect(150, 50, 100, 100);
}
if (countDown>0) {
background(255, 255, 0);
countDown--;
text("Count Down "+countDown/50, 170, 300);
fill(0);
return;
}
image(bg2, 0, bgY, 400, 600);
fill(255);
text("Score "+score, 50, 300);
if (downDown>0) {
downDown--;
text("Count Down "+downDown/50, 170, 200);
}
image(imgBall, CoinX, CoinY, 50, 50);
for (int i=0;i<N;i++) {
if (thingType[i]==1) {
drawGood(i);
thingY[i]+=5;
thingY1[i]+=5;
}
else if (thingType[i]==2) {
drawBad(i);
thingY[i]+=5;
thingY1[i]+=5;
}
if (thingY[i]>600) newThing(i);
//if(thingY1[i]>600) newThing(i);
if (touch==1) {
score+=10;
touch=0;
}
if (die==1) {
score-=10;
die=0;
}
if (downDown==0) {
background(255, 0, 0);
textSize(32);
fill(#030000);
text(" End", 122, 300);
tmp=score;
textSize(16);
text("Score "+tmp, 50, 200);
return;
}
if (dist(CoinX, CoinY, thingX[i], thingY[i])<20) touch=1;
else if (dist(CoinX, CoinY, thingX1[i], thingY1[i])<20) die=1;
}
}
void newThing(int i) {
thingY[i]=int(-random(0, 600));
thingX[i]=int(random(110, 110)+50*random(3));
thingY1[i]=int(-random(0, 600));
thingX1[i]=int(random(110, 110)+50*random(3));
thingType[i]=int(random(2))+1;
}
void drawGood(int i) {
image(ooo, thingX[i], thingY[i], 50, 50);
print("Y="+thingY[i]);
}
void drawBad(int i) {
image(bg, thingX1[i], thingY1[i], 50, 50);
}
void keyPressed() {
if (keyCode==RIGHT) CoinX+=50;
if (CoinX<150) CoinX=150;
if (keyCode==LEFT) CoinX-=50;
if (CoinX>250) CoinX=250;
}
arduino
int switchStatea = 0;
int switchStateb = 0;
const int ledPin = 13;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(2,INPUT);
pinMode(3,INPUT);
;
Serial.begin(9600);
}
void loop(){
switchStatea = digitalRead(2);
switchStateb = digitalRead(3);
if(switchStatea == HIGH) {Serial.write('2');}
if(switchStateb == HIGH) {Serial.write('3');}
delay(100);
}
沒有留言:
張貼留言