process
import processing.serial.*;////
Serial myPort;////
PImage pic;
PImage car;
PImage bg;
PImage die;
PImage heart;
float [] picX=new float[10];
float [] picY=new float[10];
float carX=235, carY=450;
int score=50;
int h=0;
void setup() {
size(513, 615);
myPort = new Serial(this, "COM5",9600);////
for (int i=0; i<10; i++) {
picX[i] = random(70, 295);
picY[i] = -i*100;
}
pic = loadImage("pic.png");///三角椎
car = loadImage("car.png");
bg = loadImage("bg.png");
die =loadImage("die.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(pic, picX[i], picY[i], 60, 60);
picY[i]+=2;
if (picY[i]>615)
{
picY[i]=-183;
picX[i]=random(70, 295);///三角錐重生
}
if (picY[i]+60>carY) {
if (carX<picX[i]+58 && picX[i]<carX+48) //2cm
{///車碰到三角錐
score-=10;
picY[i]=-150;
}
}
}
textSize(50);
text(score,50, 100);
}
if (score==0)
{
h=1;
image(die, -35, 100, 600, 350);
}
}
void keyPressed() {
if (keyCode == ENTER)
{
h=0;
score=50;
}
}
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,變慢了
}
processing
import processing.serial.*;////
Serial myPort;////
int count=1800;
PImage candy;
PImage monster;
PImage bg;
PImage die;
PImage heart;
float [] candyX=new float[10];
float [] candyY=new float[10];
float monsterX=235, monsterY=450;
int score=150;
int h=0;
void setup() {
size(513, 615);
myPort = new Serial(this, "COM3", 9600);////
for (int i=0; i<10; i++) {
candyX[i] = random(70, 295);
candyY[i] = -i*100;
}
candy = loadImage("candy.png");///三角椎
monster = loadImage("monster.png");
bg = loadImage("bg.jpg");
die =loadImage("die.png");
}
float vx=0;
//float vy=0;
void draw() {
if (h==0) {
image(bg, 0, 0, 513, 615);
//background(255);
//monsterX=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);
//monsterY=y;
}
monsterX += vx;
// monsterY += vy;
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>monsterY) {
if (monsterX<candyX[i]+58 && candyX[i]<monsterX+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<1000)
{
h=1;
image(die, -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,變慢了
}

































