1.DEMO影片
2.操作界面
3.程式碼(Arduino)
int sensorPin = A0; int sensorValue = 0; int oldValue =0; int val[6]; int oldval[6]; void setup() { Serial.begin(9600); for(int i=2;i<7;i++) pinMode(i, INPUT); } void loop() { sensorValue = analogRead(sensorPin); if(sensorValue-oldValue>100){ Serial.print(8); //Serial.write(sensorValue); oldValue = sensorValue; } else if(sensorValue-oldValue<-10){ Serial.print(9); //Serial.write(sensorValue); oldValue = sensorValue; } for(int i=2;i<7;i++) val[i-2] = digitalRead(i); if (val[0] != oldval[0]) { Serial.print(0); //Serial.write(0); oldval[0] = val[0]; } if (val[1] != oldval[1]) { Serial.print(1); //Serial.write(1); oldval[1] = val[1]; } if (val[2] != oldval[2]) { Serial.print(2); //Serial.write(2); oldval[2] = val[2]; } if (val[3] != oldval[3]) { Serial.print(3); //Serial.write(3); oldval[3] = val[3]; } if (val[4] != oldval[4]) { Serial.print(4); //Serial.write(4); oldval[4] = val[4]; } }4.程式碼(Processing)
import ddf.minim.*; Minim minim; AudioPlayer bgm0;//背景音樂1 AudioPlayer bgm1;//背景音樂2 AudioPlayer bgm2;//背景音樂3 import processing.serial.*; Serial myPort; int val; int mute,nowbgm;//靜音,現正播放 int m,savedtime,stage=0; int gameover=0; int character,count; int score,time;//現在時間,現在分數 float characterX=50,characterY=0;//人物位置 float vx=1,vy;//人物速度 float g=0.1;//場景重力 float stageX,startX;//場景位移,怪物位移 float[] monsterX = new float[20];//怪物位置 int nowbtn=1; PImage Pstage,Pstop,help,title; PImage[][] Pcharacter = new PImage[20][6];//人物 PImage[][] Pmonster = new PImage[20][6];//怪物 PImage[][] Pbutton = new PImage[2][2];//選單按鈕 PImage Pscore,Ptime,dotImage,xx;//分數,時間,冒號,乘號 PImage[] num = new PImage[10]; PImage[] sound = new PImage[2];//音樂開關 PImage[] playing = new PImage[3];//播放中 void setup(){ String portName = Serial.list()[0]; myPort = new Serial(this, "COM3", 9600); size(1280,720); Pstage=loadImage("stage.png"); Pstop=loadImage("stop.png"); help=loadImage("help.png"); title=loadImage("title.png"); for(int i=0;i<20;i++){ for(int j=0;j<6;j++){ Pcharacter[i][j] = loadImage("/character/Frame"+i+"_"+j+".png");//人物 Pmonster[i][j] = loadImage("/monster/Frame"+i+"_"+j+".png");//怪物 if(i<2&&j<2) Pbutton[i][j] = loadImage("button"+i+"_"+j+".png");//選單按鈕 } monsterX[i]=monsterX[i]=320+random(6400);//怪物位置 if(i<10) num[i] = loadImage("/count/"+i+".png");//場景 if(i<2) sound[i] = loadImage("sound"+i+".png");//音樂開關 if(i<3) playing[i] = loadImage((i+1)+".png");//播放中 } Pscore = loadImage("/count/score.png");//分數 Ptime = loadImage("/count/time.png");//時間 dotImage = loadImage("/count/dot.png");//冒號 xx = loadImage("/count/x.png"); minim = new Minim(this); bgm0 = minim.loadFile("/bgm/bgm0.mp3");//背景音樂1 bgm1 = minim.loadFile("/bgm/bgm1.mp3");//背景音樂2 bgm2 = minim.loadFile("/bgm/bgm2.mp3");//背景音樂3 } void draw(){ if ( myPort.available() > 0) { // If data is available, val = myPort.read(); control(val); } m = millis(); showstage(stage); showsound(mute,nowbgm);//音樂控制 } void showmonster(int x,float y){ image(Pmonster[x][m/100%5],y,500); if(characterX>y&&characterX<y+44&&characterY>433.5&&characterY<566.5){ gameover=1; } } void showstage(int x){ switch(x){ case 0: image(Pstage,0,0); image(title,0,0); image(Pbutton[0][0],440,300);//選單按鈕1 image(Pbutton[1][0],440,450);//選單按鈕2 if(nowbtn==1) image(Pbutton[0][1],440,300);//選單按鈕1 else if(nowbtn==2) image(Pbutton[1][1],440,450);//選單按鈕2 break; case 1: if(stageX<-1280) stageX+=1280; image(Pstage,0,0); image(Pstage,stageX+1280,0); image(Pcharacter[character][m/100%5],characterX,characterY); for(int i=0;i<20;i++){ if(monsterX[i]+startX<-100) monsterX[i]+=1280+random(6400); showmonster(i,monsterX[i]+startX); } vy-=g; if(abs(vy)<0.1) vy=0; characterY-=vy; if(characterY>=500){ vy=0; characterY=500; } if(characterY<=0) characterY=0; vx*=0.999; characterX+=vx; if(characterX>640){ characterX=640; stageX-=vx; startX-=vx; } if(gameover==1){ vx=0; vy=0; g=0; image(Pstop,0,0); } if(vx!=0){ score++; int passedtime = millis() - savedtime; time=passedtime; } showScore(score); showTime(time/10); break; case 2: image(Pstage,0,0); image(title,0,0); image(help,320,180); break; } } void showScore(int x){ int a,b,c,d,e,f; f=x%10; x/=10; e=x%10; x/=10; d=x%10; x/=10; c=x%10; x/=10; b=x%10; x/=10; a=x%10; image(Pscore,920,10); image(num[a],1080,10); image(num[b],1110,10); image(num[c],1140,10); image(num[d],1170,10); image(num[e],1200,10); image(num[f],1230,10); } //顯示時間 void showTime(int x){ int a,b,c,d,e,f; f=x%10; x/=10; e=x%10; x/=10; d=x%10; x/=10; c=x%6; x/=6; b=x%10; x/=10; a=x; image(Ptime,10,10); image(num[a],120,10); image(num[b],150,10); image(dotImage,180,10); image(num[c],210,10); image(num[d],240,10); image(dotImage,270,10); image(num[e],300,10); image(num[f],330,10); } void keyPressed() { switch(stage){ case 0: if (key == CODED) { if (keyCode == UP) { nowbtn=1; } else if (keyCode == DOWN) { nowbtn=2; } } if(key == 'z'){ stage=nowbtn; } break; case 1: if(key == 'x'){ if(gameover==0) if(characterY>=500) vy+=10; } if(key == 'c'){ if(gameover==0) vx++; } if(key == 'z'){ if(gameover==1) exit(); } break; case 2: if(key == 'z'){ stage=0; } break; } } void control(int x){ if(x == '4'){ mute++; } if(x == '8'){ if(nowbgm<3) nowbgm++; } else if(x == '9'){ if(nowbgm>0)nowbgm--; } switch(stage){ case 0: if (x == '1') { nowbtn=1; } else if (x == '2') { nowbtn=2; } if(x == '0'){ stage=nowbtn; } break; case 1: if(x == '1'){ if(gameover==0) if(characterY>=500) vy+=10; } if(x == '2'){ if(gameover==0) vx++; } if(x == '0'){ if(gameover==1) exit(); } if(x == '3'){ if(gameover==0){ count++; character=count%20; } } break; case 2: if(x == '0'){ stage=0; } break; } } void showsound(int x,int y){ image(num[1],70,660); image(num[2],110,660); image(num[3],150,660); if(x%4==2){ image(sound[0],10,660); switch(y){ case 0: bgm0.play(); bgm1.pause(); bgm2.pause(); image(playing[0],70,660); break; case 1: bgm1.play(); bgm0.pause(); bgm2.pause(); image(playing[1],110,660); break; case 2: bgm2.play(); bgm1.pause(); bgm0.pause(); image(playing[2],150,660); break; } }else if(x%4==0){ image(sound[1],10,660); bgm0.pause(); bgm1.pause(); bgm2.pause(); } }
5.心得
這次製作其實沒有想像中的簡單,大概是因為我對電路不夠熟悉的緣故
同時在Processing跟Arduino的連結也很差勁
其實感覺還有很多可改進的部分。
沒有留言:
張貼留言