https://www.youtube.com/watch?v=1FxdWV-X5fI&feature=youtu.be
互動裝置截圖:
![]() |
| 左方為藍鼓,右方為紅鼓 |
程式碼:
於Arduino中
void setup()
{
Serial.begin(9600);
pinMode (2,INPUT_PULLUP);
pinMode (3,INPUT_PULLUP);
}
void loop()
{
int a = digitalRead(2);
int b = digitalRead(3);
//Serial.println(digitalRead(3));
//Serial.println(digitalRead(4));
Serial.write(digitalRead(2));
Serial.write(digitalRead(3));
delay(220);
}
於Processing中
import processing.video.*;
Movie movie;
import ddf.minim.*;
Minim minim;
import processing.serial.*;
Serial myPort;
AudioPlayer song;
PImage A;
PImage Q;
PImage z;
PImage x;
PImage red;
PImage summer;
PImage center;
PImage End;
float rx=800, ry=160;
int bx=800, by=160;
PVector v1, v2;
float []array=new float [560];
PVector []newarray=new PVector[560];
float []array2=new float [560];
byte[] drum3=new byte[560];
byte[] bluedrum=new byte[560];
int []dy=new int [100];
int Score=0;
int shift=200;
int state=0;
int sta=0;
int rs=0;
int bs=0;
PImage home;
PImage good;
PImage setting;
PImage drum;
PImage blue;
PImage song1;
PImage mm;
PImage nn;
PImage bluebird;
int user=120;
void setup()
{
size(1200, 900);
A=loadImage("A.png");
home= loadImage("home.png");
setting=loadImage("setting.png");
summer=loadImage("summer2.png");
drum=loadImage("drum.png");
Q=loadImage("Q.jpg");
song1=loadImage("song1.png");
good=loadImage("good.png");
center=loadImage("center.png");
red=loadImage("red.png");
blue=loadImage("blue.png");
mm=loadImage("mm.png");
nn=loadImage("nn.png");
z=loadImage("z.png");
x=loadImage("x.png");
bluebird=loadImage("bluebird.png");
drum3=loadBytes("drum.bin");
bluedrum=loadBytes("bluedrum.bin");
movie = new Movie(this, "123.mp4");
movie.loop();
myPort=new Serial(this, "COM3", 9600);
}
int k, f, g;
void movieEvent(Movie movie)
{
movie.read();
}
int bgShift=0;
void draw()
{
int rs=0;
int bs=0;
bgShift+=15;
if (state==0)
{
image(home, 0, 0, 1200, 900);
fill(255, 255, 255);
rect(355, 803, 487, 80);
fill(255, 0, 0);
textSize(80);
text("Press:P", 454, 870);
}
if (state==4)
{
image(bluebird, 100, 100, 1000, 200);
image(mm, 600, 100, 1000, 200);
image(song1, 100, 400, 1000, 200);
image(nn, 600, 400, 1000, 200);
image(z, 100, 600, 1000, 200);
image(x, 600, 600, 1000, 200);
}
if (state==1)
{
image(A, 0, 0, 1200, 900);
image(movie, 0, 498, 1200, 400);
image(center, 470, 370, 240, 180);
image(Q, 0, 415, 300, 80);
fill(255, 0, 0);
rect(690, 153, 510, 58);
noStroke();
textSize(50);
fill(255);
text(Score*100, 800, 200);
for (int i=0; i<array.length; i++)
{
if (drum3[i]!=0)
{
array[i]=i*100+600-bgShift;
if (array[i]>shift)
{
image(red, array[i], 253, 80, 80);
}
}
if (bluedrum[i]!=0)
{
array2[i]=i*100+600-bgShift;
if (array2[i]>shift)
{
image(blue, array2[i], 253, 80, 80);
}
}
{
if (myPort.available()>0) {
int pin2=myPort.read();
if (pin2==1) rs=1;
println(pin2);
}
if (myPort.available()>0) {
int pin3=myPort.read();
if (pin3==1) bs=1;
println(pin3);
}
if (abs(array[i]-shift-25)<20 && array[i]!=0&&rs==1)
{
fill(255, 0, 0, 200);
ellipse(110, 285, 140, 110);
Score++;
textSize(30);
fill(255, 227, 132);
text("GOOD", 250, 222);
noFill();
song=minim.loadFile("r.mp3", 2048);
song.play();
rs=0;
}
if (abs(array2[i]-shift-25)<20 && array2[i]!=0&&bs==1)
{
ellipseMode(RADIUS);
fill(0, 0, 255, 200);
ellipse(110, 285, 95, 80);
ellipseMode(CENTER);
fill(255, 255, 255, 200);
ellipse(110, 285, 140, 110);
Score++;
textSize(30);
fill(255, 227, 132);
song=minim.loadFile("b.mp3", 2048);
song.play();
bs=0;
text("GOOD", 250, 222);
noFill();
}
}
}
k=Score*2;
f=30;
g=440;
rect(g, 58, k, f, 255);
if (sta==0)
{
fill(255, 0, 0);
textSize(80);
text(Score, 85, 310);
}
}
if (state==2)
{
image(A, 0, 0, 1200, 900);
image(Q, 0, 415, 300, 80);
fill(0);
rect(695, 454, 470, 36);
image(movie, 0, 498, 1200, 400);
image(summer, 840, 420, 150, 70);
image(center, 470, 370, 240, 180);
fill(255, 0, 0);
rect(690, 153, 510, 58);
noStroke();
textSize(50);
fill(255);
text(Score*100, 800, 200);
for (int i=0; i<array.length; i++)
{
if (drum3[i]!=0)
{
array[i]=i*100+600-bgShift;
if (array[i]>shift)
{
image(red, array[i], 253, 80, 80);
}
}
if (bluedrum[i]!=0)
{
array2[i]=i*100+600-bgShift;
if (array2[i]>shift)
{
image(blue, array2[i], 253, 80, 80);
}
}
{
if (myPort.available()>0) {
int pin2=myPort.read();
if (pin2==1) rs=1;
println(pin2);
}
if (myPort.available()>0) {
int pin3=myPort.read();
if (pin3==1) bs=1;
println(pin3);
}
if (abs(array[i]-shift-25)<20 && array[i]!=0&&rs==1)
{
fill(255, 0, 0, 200);
ellipse(110, 285, 140, 110);
Score++;
textSize(30);
fill(255, 227, 132);
text("GOOD", 250, 222);
noFill();
song=minim.loadFile("r.mp3", 2048);
song.play();
rs=0;
}
if (abs(array2[i]-shift-25)<20 && array2[i]!=0&&bs==1)
{
ellipseMode(RADIUS);
fill(0, 0, 255, 200);
ellipse(110, 285, 95, 80);
ellipseMode(CENTER);
fill(255, 255, 255, 200);
ellipse(110, 285, 140, 110);
Score++;
textSize(30);
fill(255, 227, 132);
song=minim.loadFile("b.mp3", 2048);
song.play();
bs=0;
text("GOOD", 250, 222);
noFill();
}
}
if (sta==0)
{
fill(255, 0, 0);
textSize(80);
text(Score, 85, 310);
}
}
k=Score*2;
f=30;
g=440;
fill(255, 0, 0);
rect(g, 58, k, f, 255);
if (key=='q'||key=='Q')
{
sta=1;
fill(255, 255, 255);
rect(218, 208, 1000, 200);
textSize(100);
fill(255, 0, 0);
text("Score:"+Score*100, 300, 310);
song.close();
minim.stop();
super.stop();
}
}
}
void keyReleased()
{
if (key=='p'||key=='P')
{
state=4;
}
if (key=='m'||key=='M')
{
state=1;
minim=new Minim(this);
song=minim.loadFile("blue bird.mp3", 2048);
song.play();
}
if (key=='n'||key=='N')
{
state=2;
minim=new Minim(this);
song=minim.loadFile("summer.mp3", 2048);
song.play();
}
if (state==4)
{
background(#FFE153);
}
}

沒有留言:
張貼留言