2.寫下:有了 Processing + Arduino 可以做出甚麼?
我們想到的是一樣可以做我們期中時的射擊遊戲,但是我們把它從滑鼠的射酒瓶改成直升機攔截飛彈可以搭配按鈕(搖桿)左右移動跟射擊的形式,來做互動遊戲
/*Arduino*/
int pushButton = 2;
int pushButton2 = 6;
int pushButton3 = 7;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT_PULLUP);
pinMode(pushButton2, INPUT_PULLUP);
pinMode(pushButton3, INPUT_PULLUP);
}
int a=0;
int a2=0;
int a3=0;
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
int buttonState2 = digitalRead(pushButton2);
int buttonState3 = digitalRead(pushButton3);
// print out the state of the button:
if (buttonState == HIGH && a==0 ) { // If switch is ON,
Serial.print(1);
a=1; // send 1 to Processing
}
if (buttonState == LOW && a==1) { // If the switch is not ON,
Serial.println(0);
a=0; // send 0 to Processing
}
if (buttonState2 == HIGH && a2==0 ) { // If switch is ON,
Serial.print(2);
a2=2; // send 1 to Processing
}
if (buttonState2 == LOW && a2==1) { // If the switch is not ON,
Serial.println(3);
a2=3; // send 0 to Processing
}
if (buttonState3 == HIGH && a3==0 ) { // If switch is ON,
Serial.print(4);
a3=4; // send 1 to Processing
}
if (buttonState3 == LOW && a3==1) { // If the switch is not ON,
Serial.println(5);
a2=5; // send 0 to Processing
}
// Serial.println(buttonState);
delay(10); // delay in between reads for stability
}
/*Processing*/ import processing.serial.*; Serial myPort; int val; PImage bg,helicopter,bomb; PImage [] life = new PImage[5]; float hx=350,hy=400; PImage [] missile = new PImage[5]; int[] mx = new int [5]; int[] my = new int [5]; int bx=0,by=0; void setup(){ size(840,525); String portName = Serial.list()[0]; myPort = new Serial(this, "COM5", 9600); bg =loadImage("sky.jpg"); helicopter= loadImage ("Helicopter.png"); for(int i=0;i<5;i++){ life[i]= loadImage("Life.png"); } for(int i=0;i<5;i++){ missile[i]= loadImage("Missile.png"); } for(int i=0;i<5;i++){ mx[i]=i*200; my[i]=(-300+i*40); } bomb= loadImage ("bomb.png"); } void draw(){ background(bg); life(); helicopterA(); missilefly(); myserial(); bomb(); } void helicopterA(){ image(helicopter,hx,hy); } void life(){ fill(180,180,180); rect(740,340,60,160); fill(255,0,0); rect(740,300,60,40); fill(255,255,255); textFont(createFont("Georgia", 25)); text("LIFE ",742,330); for(int i=0;i<5;i++){ image(life[i],690,320+(30*i),150,100); } } void missilefly(){ for(int i=0;i<5;i++){ image(missile[i],mx[i],my[i],50,100); } for(int i=0;i<5;i++){ my[i]+=2; } for(int i=0;i<5;i++){ if(my[i]==600){ my[i]=(-300+i*40); } } } void myserial(){ if ( myPort.available() &rt; 0) { // If data is available, val = myPort.read(); // read it and store it in val } if (val == 0) { // If the serial value is 0, translate(hx,hx+=10); } else if(val == 2){ // If the serial value is not 0, translate(hx,hx-=10); } } void bomb(){ int p=0; image(bomb,hx+90,hy+by,10,15); if(val != 4 && by!=0){ by-=10; } if (val == 4){ by-=10; } if((hy+by)<=0){ by=0; } }
int speakerPin = 9; int length = 24; // the number of notes char notes[] = "geefddcdefggggeefddceggc "; // a space represents a rest int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4}; int tempo = 300; void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(speakerPin, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW); delayMicroseconds(tone); } } void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void setup() { pinMode(speakerPin, OUTPUT); } void loop() { for (int i = 0; i < length; i++) { if (notes[i] == ' ') { delay(beats[i] * tempo); // rest } else { playNote(notes[i], beats[i] * tempo); } // pause between notes delay(tempo / 2); } }2.可用按鍵彈音階
const int buttonPin1 = 2; const int buttonPin2 = 3; const int buttonPin3 = 4; const int buttonPin4 = 5; const int buttonPin5 = 6; int speakerPin = 9; int buttonState1 = 0; // variable for reading the pushbutton status int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; void setup() { pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); } void loop() { buttonState1 = digitalRead(buttonPin1); buttonState2 = digitalRead(buttonPin2); buttonState3 = digitalRead(buttonPin3); buttonState4 = digitalRead(buttonPin4); buttonState5 = digitalRead(buttonPin5); int duration; if (buttonState1 == HIGH) tone(speakerPin, 261,duration); //tone(pin, frequency, duration) else if (buttonState2 == HIGH) tone(speakerPin, 294,duration); else if (buttonState3 == HIGH) tone(speakerPin, 329,duration); else if (buttonState4 == HIGH) tone(speakerPin, 349,duration); else if (buttonState5 == HIGH) tone(speakerPin, 392,duration); else { noTone(speakerPin); } }
3.心得
這節課教我們用 Arduino 撥放音樂,還有搭配按鈕彈音階, 讓我想到可以做鋼琴的小遊戲。
而且他的程式碼跟原裡沒有很難,董元李後就可以延伸很多不同的曲子。
可惜這節課當時沒有借到手機或相機,沒有把接出來的執行樣子拍攝下來,
有機會再用得獎的Arduino 再接一次~
/*Var 設定*/ import ddf.minim.*; Minim minim; AudioPlayer shoot,glass,shotgun,bgmusic,ccheer; PImage screenleft,screenright,people,bg; boolean ctl=true,ctl2=false; //control mouse press int m=1; //control ending music int siteX1=0,siteX2=350,siteX3=525; //Image site PImage gun,star; PImage [] bottle = new PImage[5];//酒瓶圖 float [] bx = new float [5];//位置 int savedTime,passedTime,gameTime; //計時器時間 int score=0;//分數 /*首頁畫面*/ void Start() { if(mousePressed&&mouseX<430&&mouseX&rt;270&&mouseY<430&&mouseY&rt;270&&ctl) ctl=false; if(ctl==false && ctl2==false){ image(screenleft, siteX1-=3, 0, width/2, height); image(screenright,siteX2+=3,0,width/2, height); image(people,siteX3+=1,150,width/4, height/1.5); if(siteX1<-350&&siteX2&rt;700&&siteX3&rt;700) ctl2=true; if(ctl2) ; } else if(ctl==true && ctl2 == false){ image(screenleft, siteX1, 0, width/2, height); image(screenright,siteX2,0,width/2, height); image(people,siteX3,150,width/4, height/1.5); fill(66,66,255); ellipse(350,350, 80,80); fill(255,255,0); textFont(createFont("Cooper Black", 20)); text("START",315,358); } } void setup() { //Size size(700,525); bg = loadImage("bg.jpg"); //Load Image screenleft = loadImage("screen_left.jpg"); screenright = loadImage("screen_right.jpg"); people = loadImage("people.png"); //Print Image image(screenleft, siteX1, 0, width/2, height); image(screenright,siteX2,0,width/2, height); image(people,siteX3,150,width/4, height/1.5); //code savedTime = millis(); //儲存遊戲開始的時間(只執行一次) gun=loadImage("gun.png"); star=loadImage("star.png"); for(int i=0;i<5;i++){ bottle[i]=loadImage("bottle.png"); } minim=new Minim(this); bgmusic=minim.loadFile("lion.wav", 2048); bgmusic.play(); } void draw() { background(bg); Start(); //code if(siteX1<-350 && siteX2&rt;700 && siteX3&rt;700){ bottle(); //酒瓶 gun(); //槍的準心 word(); //字 time(); End(); } /*結尾的拍手聲*/ if(m==1 && gameTime<0){ ccheer=minim.loadFile("ccheer.wav", 2048); ccheer.play(); m--; } } /*槍的準心設置*/ void gun(){ image(gun,530,420); image(star,mouseX-38,mouseY-35); } /*移動酒瓶的設置*/ void bottle(){ for(int i=0;i<5;i++){ image(bottle[i],i*50+bx[i],98,50,80); bx[i]+=1; if(bx[i]&rt;=700){ bx[i]=0; } } } /*畫面上的字*/ void word(){ textFont(createFont("Georgia", 40)); text("Score:",20,500); textFont(createFont("Georgia", 40));//得分數 text(score,150,500); } /*計時器*/ void time(){ //當滑鼠按下,計時器重新回到60秒 if(mousePressed&&mouseX<430&&mouseX&rt;270&&mouseY<430&&mouseY&rt;270&&ctl) ctl=false; if(ctl==false){ savedTime = millis(); if(siteX1<-350&&siteX2&rt;700&&siteX3&rt;700) ctl=true; } passedTime = millis() - savedTime; //經過的秒數 gameTime = 60-passedTime/1000; //剩下的秒數 textFont(createFont("Georgia", 40)); text("Time: "+gameTime,250, 500); } /*槍射擊 & 音效*/ void mouseClicked(){ minim=new Minim(this); shoot=minim.loadFile("BULLET.wav", 2048); shotgun=minim.loadFile("Shoutgun198.wav", 2048); glass=minim.loadFile("glass.wav",2048); //start的音效 if(siteX1<-350 && siteX2&rt;700 && siteX3&rt;700){ shoot.play(); } for(int i=0;i<5;i++){ if(mouseY&rt;100 && mouseY<175 && mouseX&rt;i*50+bx[i]+15 && mouseX<i*50+bx[i]+30){ score+=5; shoot.pause(); glass.play(); bx[i]=0; } } //槍射擊音效 if(mouseX<430&&mouseX&rt;270&&mouseY<430&&mouseY&rt;270){ if(siteX1<-350 && siteX2&rt;700 && siteX3&rt;700){ shotgun.pause(); } else shotgun.play(); } } /*結尾畫面*/ void End(){ if(gameTime<0){ background(0,0,0); fill(255,255,255); textFont(createFont("Georgia", 60)); text("Game Over ",180,200); text("Your score:"+score,150,300); fill(66,66,255); ellipse(200,400, 80,80); fill(255,255,0); textFont(createFont("Cooper Black", 30)); text("Exit",190,410); if(mousePressed && mouseX<280 && mouseX&rt;120 && mouseY<480 && mouseY&rt;320){ exit(); } } }

size(600,600);
for(int i=0;i<50;i++){
fill(random(255),random(255),random(255));rect(random(500),random(500),random(500),random(500));
size(600,400);
for(int i=0;i<60;i++){
for(int k=0;k<40;k++){
fill(k*255/40,i*255/60,100);
rect(i*10,k*10,10,10);
}
}
3.多個圓
程式碼:
size(600,400);
background(0, 0, 0);
for(int i=0;i<60;i++){
for(int k=0;k<40;k++){
fill(random(255),random(255),255);
ellipse(i*100+50,k*100+50,100,100);
fill(random(255),random(255),255);
ellipse(i*100+50,k*100+50,100,70);
fill(255,255,255);
ellipse(i*100+50,k*100+50,50,70);
}
}
4. 上課心得
今天的上課內容並沒有很難懂,圖形知道原理後也很好畫,只是要畫好看的圖形要多花一點時間而已~
size (500,500); PImage kitty; kitty=loadImage("http://www.wahouse.com.tw/blog/images/upimages/y1w3e2f8j2j003d8p876.jpg"); image(kitty, 5,50);