✎期末作品展示
👀程式碼
Arduino
const int finish = A0;
const int hole1 = A1;
const int hole2 = A2;
const int hole3 = A3;
const int hole4 = A4;
const int hole5 = A5;
const int start = A6;
const int hole1 = A1;
const int hole2 = A2;
const int hole3 = A3;
const int hole4 = A4;
const int hole5 = A5;
const int start = A6;
int finishValue = 0;
int hole1Value = 0;
int hole2Value = 0;
int hole3Value = 0;
int hole4Value = 0;
int hole5Value = 0;
int startValue = 0;
int hole1Value = 0;
int hole2Value = 0;
int hole3Value = 0;
int hole4Value = 0;
int hole5Value = 0;
int startValue = 0;
void setup() {
Serial.begin(9600);
}
Serial.begin(9600);
}
void loop() {
// Read the sensors first:
finishValue = analogRead(finish)/50;
hole1Value = analogRead(hole1)/50;
hole2Value = analogRead(hole2)/50;
hole3Value = analogRead(hole3)/50;
hole4Value = analogRead(hole4)/50;
hole5Value = analogRead(hole5)/50;
startValue = analogRead(start)/50;
// Read the sensors first:
finishValue = analogRead(finish)/50;
hole1Value = analogRead(hole1)/50;
hole2Value = analogRead(hole2)/50;
hole3Value = analogRead(hole3)/50;
hole4Value = analogRead(hole4)/50;
hole5Value = analogRead(hole5)/50;
startValue = analogRead(start)/50;
//用來判斷光敏電阻的門檻值
/*Serial.println(finishValue);
Serial.println(hole1Value);
Serial.println(hole2Value);
Serial.println(hole3Value);
Serial.println(hole4Value);
Serial.println(hole5Value);
Serial.println(startValue);
Serial.println(" ");*/
if(finishValue<4) Serial.println("f");
if(hole1Value<4) Serial.println("a");
if(hole2Value<4) Serial.println("b");
if(hole3Value<4) Serial.println("c");
if(hole4Value<4) Serial.println("d");
if(hole5Value<4) Serial.println("e");
if(startValue>10) Serial.println("s");
if(startValue<6) Serial.println("r");
delay(50);
/*Serial.println(finishValue);
Serial.println(hole1Value);
Serial.println(hole2Value);
Serial.println(hole3Value);
Serial.println(hole4Value);
Serial.println(hole5Value);
Serial.println(startValue);
Serial.println(" ");*/
if(finishValue<4) Serial.println("f");
if(hole1Value<4) Serial.println("a");
if(hole2Value<4) Serial.println("b");
if(hole3Value<4) Serial.println("c");
if(hole4Value<4) Serial.println("d");
if(hole5Value<4) Serial.println("e");
if(startValue>10) Serial.println("s");
if(startValue<6) Serial.println("r");
delay(50);
}
processing
//music
import ddf.minim.*;
Minim minim;
AudioPlayer player;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
//devices
import processing.serial.*;
Serial myPort;
int val;
import processing.serial.*;
Serial myPort;
int val;
PImage h1,h2,h3,h4,h5,finish;
int state=0,count=0;
float time=0;
boolean game=false;
void setup(){
size(600,800);
size(600,800);
//播放音樂
minim=new Minim(this);
player=minim.loadFile("music.mp3",2048);
player.play();
player.loop();
minim=new Minim(this);
player=minim.loadFile("music.mp3",2048);
player.play();
player.loop();
myPort = new Serial(this,"COM9",9600); //讀取裝置的變數
h1=loadImage("1.png");
h2=loadImage("2.png");
h3=loadImage("3.png");
h4=loadImage("4.png");
h5=loadImage("5.png");
finish=loadImage("F.png");
}
void draw(){
//Game Start/Over
if(myPort.available()>0){
val=myPort.read();
if(val==115){ //如果起點接收到光,遊戲開始
game=true;
}
else if(val==114) game=false; //如果起點沒有接收到光,遊戲重置
}
back(); //繪出場景
if(game==true){ //遊戲開始
count++;
if(count==60 && state==0){ //計時器
time++;
count=0;
}
//判斷球掉進哪一個洞裡
if(myPort.available()>0 && state==0){
val=myPort.read();
if(val==97){
state=5;
println("Hole5");
}
else if(val==98){
state=4;
println("Hole4");
}
else if(val==99){
state=3;
println("Hole3");
}
else if(val==100){
state=2;
println("Hole2");
}
else if(val==101){
state=1;
println("Hole1");
}
else if(val==102){
state=6;
println("Finish");
}
}
textSize(25);text("Time:"+time,450,40);
if(myPort.available()>0 && state==0){
val=myPort.read();
if(val==97){
state=5;
println("Hole5");
}
else if(val==98){
state=4;
println("Hole4");
}
else if(val==99){
state=3;
println("Hole3");
}
else if(val==100){
state=2;
println("Hole2");
}
else if(val==101){
state=1;
println("Hole1");
}
else if(val==102){
state=6;
println("Finish");
}
}
textSize(25);text("Time:"+time,450,40);
//秀出結果畫面
if(state==1) image(h1,100,200,400,400);
else if(state==2) image(h2,100,200,400,400);
else if(state==3) image(h3,100,200,400,400);
else if(state==4) image(h4,100,200,400,400);
else if(state==5) image(h5,100,200,400,400);
else if(state==6) image(finish,70,200,450,400);
}
else if(game==false){
//reset
state=0;
time=0;
count=0;
back();
}
}
if(state==1) image(h1,100,200,400,400);
else if(state==2) image(h2,100,200,400,400);
else if(state==3) image(h3,100,200,400,400);
else if(state==4) image(h4,100,200,400,400);
else if(state==5) image(h5,100,200,400,400);
else if(state==6) image(finish,70,200,450,400);
}
else if(game==false){
//reset
state=0;
time=0;
count=0;
back();
}
}
//Scene
void back(){
background(#F7AAAA);
strokeWeight(5); fill(255); rect(50,50,500,700);
strokeWeight(5); stroke(0);
//row1
line(335,680,550,680);
line(100,680,265,680);
//row2
line(120,610,265,610);
//row3
line(50,540,215,540);
line(265,540,450,540);
//row4
line(120,470,480,470);
//row5
line(450,400,550,400);
line(190,400,380,400);
//row6
line(380,330,480,330);
//row8
line(50,190,380,190);
//row9
line(140,120,480,120);
//cou1
line(335,680,335,610);
//cou2
line(450,610,450,540);
line(265,610,265,540);
//cou3
line(120,470,120,260);
//cou5
line(190,400,190,260);
line(380,400,380,330);
//cou6
line(280,190,280,330);
//cou7
line(380,190,380,260);
line(480,120,480,260);
//hole
fill(0,0,255);
ellipse(90,90,60,60); //finish
fill(0);
ellipse(415,575,40,40); //hole1
ellipse(415,365,40,40); //hole2
ellipse(85,295,40,40); //hole3
ellipse(445,155,40,40); //hole4
ellipse(410,95,35,35); //hole5
//start
fill(200);ellipse(510,715,60,60);
fill(0);textSize(20);text("Start",490,720);
//timer
textSize(25);text("Time:"+time,450,40);
}
👀實體裝置
👀展示結果
https://youtu.be/kFIuPNTb-sw
沒有留言:
張貼留言