這週我們把Arduino和processing的部份結合起來
再做一些修改,例如加了分數計算和時間限制...等等
然後這週也討論了外型要怎麼做
/////processing////
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer song;
PImage bg;
PImage Aircraft;
float Px=500;
float Py=470;
PImage boom;
int off=0;
PImage die;
int score=-80, lastTime=60;
import processing.serial.*;
Serial serial;
void setup()
{
minim = new Minim(this);
song = minim.loadFile("1.mp3", 2048);
serial=new Serial(this, "COM4", 9600);
frameRate(25);
size (1100, 600);
song.play();
bg = loadImage("sky.jpg");
Aircraft = loadImage("1.jpg");
Random();
}
float [] x= new float [8];
float y;
char c=' ';
void draw()
{
if (serial.available()>0)
{
c=(char)serial.read();
print(c);
if (c=='3') {
Px-=10;
}
if (c=='4') {
Px+=10;
}
}
background(bg);
image(Aircraft, Px, Py);
boom=loadImage("boom.jpg");
int m=millis();
textSize(30);
fill(m/10%10,m/10%10,m/10%255);
text("Time:"+lastTime, 10,40);
text("Score:"+score, 330,40);
lastTime=60-millis()/1000;
if (off==0) {
for (int i=0;i<8;i++) {
image(boom, x[i], y++);
if (dist(Px+35, Py+42.5, x[i]+45, y+45)<90.6) {
off=1;
print("boom");
song.close();//音樂停止
minim.stop();
super.stop();
}
}
if (y>525)
{
Random();
y=0;
}
}
if (off==1) {
die=loadImage("booms.jpg");
image(die, 0, 0);
textSize(70);
fill(255,0,0);
text("Your score:"+score,500,100);
}
if(lastTime==0)
{
stop();
textSize(75);
fill(255,0,0);
text("Times up!", 150,140);
text("You Win!", 150,220);
text("Your score:"+score,150,300);
}
}
void Random()
{
for (int i=0;i<8;i++)
x[i]=random(1100);
score+=80;
}
/*void keyPressed()
{
if (keyCode==RIGHT)
Px+=10;
else if (keyCode==LEFT)
Px-=10;
}
*/
////Arduino////
int switchStateRed = 0;
int switchStateBlue = 0;
int switchStateW = 0;
int switchStateA = 0;
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 100;
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
Serial.begin(9600);
}
//int a=0,b=0;
void loop(){
sensorReading = analogRead(knockSensor);
switchStateW = digitalRead(4);
switchStateA = digitalRead(5);
if (sensorReading >= threshold) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
}
if(switchStateW == HIGH) {Serial.write('3');}
if(switchStateA == HIGH) {Serial.write('4');}
delay(33);
}
沒有留言:
張貼留言