process
import processing.serial.*;////
Serial myPort;////
int count=1800;//60*30
PImage candy;
PImage car;
PImage bg;
PImage die;
PImage heart;
PImage win;
float [] candyX=new float[10];
float [] candyY=new float[10];
float carX=235, carY=450;
int score=150;
int h=0;
void setup() {
size(513, 615);
myPort = new Serial(this, "COM5", 9600);////
for (int i=0; i<10; i++) {
candyX[i] = random(70, 295);
candyY[i] = -i*100;
}
candy = loadImage("candy.png");///糖果
car = loadImage("car.png");
bg = loadImage("bg.jpg");
die =loadImage("die.png");
win =loadImage("win.png");
}
float vx=0;
//float vy=0;
void draw() {
if (h==0) {
image(bg, 0, 0, 513, 615);
//background(255);
//carX=mouseX-47;
while (myPort.available()>=1)
{
int x = myPort.read();
///int y = myPort.read();
//int sw = myPort.read();
vx= (x-128)/32;
// vy=(y-128)/32;
println(vx);
//carY=y;
}
carX += vx;
// carY += vy;
image(car, carX, carY, 50, 100);
for (int i=0; i<7; i++) {
image(candy, candyX[i], candyY[i], 60, 60);
candyY[i]+=2;
if (candyY[i]>615)
{
candyY[i]=-300; //-183
candyX[i]=random(70, 295);///糖果重生
}
if (candyY[i]+60>carY) {
if (carX<candyX[i]+58 && candyX[i]<carX+48) //2cm
{///車碰到糖果
score+=10;
candyY[i]=-150;
}
}
}
if (count<0) {
background(0, 0, 0);
h=1;
} else {
count--;
}
textSize(50);
fill(25, 25, 250, 250);
text("Time: "+int(count/60), 250, 50);
textSize(50);
text(score, 50, 100);
}
if (count<0 && score<600)
{
h=1;
image(die, -35, 100, 600, 350);
}
if (count<0 && score>=600)//超過六百分才贏
{
h=1;
image(win, -35, 100, 600, 350);
}
}
void keyPressed() {
//count=1800;
if (keyCode == ENTER)
{
h=0;
score=150;
}
}
arduino
void setup()
{
pinMode(2, INPUT_PULLUP);//拉高的Input
Serial.begin(9600);//開始設定USB的傳輸速度
}
void loop() {//1000Hz (1000 fps)
int x = analogRead(A0);//0...1023
//int y = analogRead(A1);//0...1023
// int sw= digitalRead(2);//HIGH(放開)
Serial.write (x/4);//第1個byte
//Serial.write(y/4);//第2個byte
//Serial.write (sw); //第3個byte
delay(20);//1000ms/20ms => 50fps,變慢了
}







沒有留言:
張貼留言