2013年1月14日 星期一

week19,final


上面是電腦畫面,不過沒有聲音,下面是配樂。


這是按鈕
我負責軟體,黃豊翔負責硬體
import ddf.minim.*;
import processing.serial.*;
PrintWriter output;
Minim minim;
AudioPlayer bgm;
int score=0,combo=000,ansSum=10,clock,s,ms,m=0,befos=0,befoms=0,befom=0,length;
int [] ansTime={1,2,3,4,5,6,7,8,9,10};//拍子的出現時間(秒)
char ansKey[]={'1','2','3','4','5','6','7','8','9','1'};//要按的紐
boolean[] anesd={false,false,false,false,false,false,false,false,false,false};//是否有按過
//int [] ansTime;
//char ansKey[];
char keyPressd='?',okeyPressd=' ';
BufferedReader reader;
BufferedReader rank;
PrintWriter Edit;
String line;
String[] Ans;
Serial myport;
boolean load,edit,playbeat=false;
String[] pieces;
boolean[] btn={false,false,false,false,false,false,false,false,false};//是否為要按的 按鍵0->1 1->2 其他類推
boolean T1,T2,T3,R1,R2,L1,L2;//要畫出的方塊
Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup(){
  //reader = createReader("gangnamhit.txt");  
  size(600,650);
  minim = new Minim(this);

  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  //myport=new Serial(this,"com7",9600);連接物理ardunio
}

void keyPressed(){
   keyPressd=key;
  // judge(s);
   //print(keyPressd);
   switch(keyPressd){
   /* case '1':  case '2': case '3':  case '4': case '5':  case '6': case '7': case '8':  case '9':
      print(int(keyPressd)-48);
        if(btn[int(keyPressd)-49]==true){
          btn[int(keyPressd)-49]=false;
            score++;
            combo++;    
          }
          break;*/
     case 's':
       bgm = minim.loadFile("Elec.mp3");//我爸剛
       bgm.play();//放音樂
       befos=s;
       befoms=ms;
       befom=m;
       break;
     case 'l':
       reader = createReader("songset.txt");
       bgm = minim.loadFile("Elec.mp3");//我爸剛
       load=true;
       break;
     case 'e':
      output = createWriter("songset.txt");
      bgm = minim.loadFile("Elec.mp3");//我爸剛
      bgm.play();
      edit=true;
      break;
  }
  if(edit)edit();
}
void loadsong(){
  try {
    line = reader.readLine();
  } catch (IOException e) {
    e.printStackTrace();
    line = null;
 
  }
  if (line == null) {
    // Stop reading because of an error or file is empty
   // noLoop();
   print("/ndone1");
   bgm.play();//放音樂
   befos=s;
   befoms=ms;
   befom=m;
   print(" and start");
   load=false;
   playbeat=true;
   } else {
     pieces = split(line, " ");
     length=pieces.length;
  for(int i=0;i<pieces.length;i++){
  //*
    if(i%10==1)//id
    print("id:"+pieces[i]);
    if(i%10==3)//beat
    print("beat:"+pieces[i]);
    if(i%10==5)//min
    print("min:"+pieces[i]);
    if(i%10==7)//s
    print("s:"+pieces[i]);
    if(i%10==9)//ms
    print("ms:"+pieces[i]);
    //*/
    //print(pieces[i]);
 }

    //print(pieces[(pieces.length-1)]);
 
  }

}
int i=1;
void edit(){
  if(keyPressd=='q'){
    output.flush();  // Writes the remaining data to the file
    output.close();  // Finishes the file
    exit();  // Stops the program
  }else{
    // case 6: case 7: case 8: case 9:
    output.print("<ID> "+i+" ");
    output.print("<BEAT> "+keyPressd+" ");
    output.println("true "+m+" min "+s+" s "+ms+" ms");
    i++;
  }
}
void beating(int btnl,int NoAnsednber){
 boolean NoAnsed =boolean(pieces[NoAnsednber]);
 if(NoAnsed)
  btn[btnl-1]=true;
  for(int by=0;by<=2;by++)//畫出方形
    for(int bx=0;bx<=2;bx++){
    if(btn[7-3*by+bx-1]) fill(0);
       else  fill(200);
       rect(10+bx*200, 50+by*200, 180, 180);
     }
   if(int(keyPressd)-48==btnl && NoAnsed)//(int(keyPressd)-49)==btnl-1
     {
       btn[btnl-1]=false;
       pieces[NoAnsednber]=str(false);
       score++;
       combo++;
     }
   
}
/*void judge(){
        if(int(keyPressd)>48&&int(keyPressd)<58){
        if(btn[int(keyPressd)-49]==true&&int(okeyPressd)!=int(keyPressd)){
          btn[int(keyPressd)-49]=false;
            score++;
            combo++;
         
         }
         okeyPressd=keyPressd;
       }
   
   }
*/
void draw(){
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
   // if(val-49<0)val=58;
    //println(val-49);
    switch(val){
      case 50: keyPressd='1';    break;
      case 51: keyPressd='2';    break;
      case 52: keyPressd='3';    break;
      case 53: keyPressd='4';    break;
      case 54: keyPressd='5';    break;
      case 55: keyPressd='6';    break;
      case 56: keyPressd='7';    break;
      case 57: keyPressd='8';    break;
      case 49:
      case 48: keyPressd='9';    break;
    }
   // keyPressd=char(val-1);
  }
  if(load)loadsong();
 // print("  "+s+"秒");
   //方格
   ///////////////
   //00_7 10_8 20_9
   //01_4 11_5 21_6
   //02_1 12_2 22_3
   ///////////////
 // int  pA=myport.read();
   background(255);
   int black=999;
   clock= millis();
   s = clock/1000-m*60-befos;//SECOND s = millis()/1000;
   ms = clock%1000-befoms;//mili-SECOND
   if(s>=60){
     m++;
     s-=60;
 }
 //  m= clock/(60*1000)-befom;
   if(s<=0)s=0;
  char currentI='+';
 if(playbeat)
   for(int i=0;i<length/10;i++){
  // print("進入迴圈"+pieces[i*10+5]+pieces[5]+int(pieces[i*10+5]));
   //if()
   if(int(pieces[i*10+5])<m)continue;
   else if(int(pieces[i*10+5])==m)//分鐘
     {//出來一秒
     
       if(int(pieces[i*10+7])<=s-2)//second
       {  if(boolean(pieces[i*10+4])==true){
           combo=0;
           pieces[i*10+4]=str(false);
         }
           continue;
       }
   
       else if(int(pieces[i*10+7])==s-1)//second
       { // print("進入迴圈分鐘前");
          if(ms<=int(pieces[i*10+9])-250)//ms//ms+(1000-pieces[i*10+9])<=1000間隔一秒
         {
            print("進入迴圈分鐘"+"ID:"+int(pieces[i*10+1])+"<"+int(pieces[i*10+5])+"分"+int(pieces[i*10+7])+"秒");
           beating(int(pieces[i*10+3])/*按鍵*/,i*10+4);//顯示出來
           // print("進入迴圈分鐘前顯示");
         }
       }
       else if(int(pieces[i*10+7])==s)//second
       {
         //print("進入迴圈分鐘後");
         if(ms-int(pieces[i*10+9])>=250)//ms間隔一秒之內
         {
           print("進入迴圈分鐘"+"ID:"+int(pieces[i*10+1])+"<"+int(pieces[i*10+5])+"分"+int(pieces[i*10+7])+"秒");
           beating(int(pieces[i*10+3])/*按鍵*/,i*10+4);
          //  print("進入迴圈分鐘後顯示");
         }else break;//超過一秒跳出
       }
     }
     
   }
    ///////////////
   //00_7 10_8 20_9
   //01_4 11_5 21_6
   //02_1 12_2 22_3
   ///////////////
   for(int i=0;i<9;i++){
    if(btn[i])break;
    else if(i<=7)continue;
    else if(i==8){
         fill(200);
         for(int by=0;by<=2;by++)//畫出方形
          for(int bx=0;bx<=2;bx++)
           rect(10+bx*200, 50+by*200, 180, 180);
        }
     }

  for(int i=0;i<9;i++){
     btn[i]=false;
  }
   //分數
   fill(0,0,0,120);
   for(int digit=0;digit<=2;digit++){
   int drawNumber=(int)(score/pow(10,digit))%10;//找出要畫的數字
   T1=T2=T3=R1=R2=L1=L2=false;
   switch(drawNumber){
   case 0:T1=T3=R1=R2=L1=L2=true;   break;
   case 1:R1=R2=true;               break;
   case 2:T1=T2=T3=R1=L2=true;      break;
   case 3:T1=T2=T3=R1=R2=true;      break;
   case 4:T2=R1=R2=L1=true;         break;
   case 5:T1=T2=T3=R2=L1=true;      break;
   case 6:T1=T2=T3=R2=L1=L2=true;   break;
   case 7:T1=R1=R2=true;            break;
   case 8:T1=T2=T3=R1=R2=L1=L2=true;break;
   case 9:T1=T2=T3=R1=R2=L1=true;   break;
 }
   if(T1)rect(350-150*digit, 260, 60, 20);//橫一Transverse  T1
   if(R1)rect(410-150*digit, 280, 20, 60);//R1
   if(T2)rect(350-150*digit ,340, 60, 20);//T2
   if(R2)rect(410-150*digit, 360, 20, 60);//R2
   if(T3)rect(350-150*digit, 420, 60, 20);//T3
   if(L2)rect(330-150*digit, 360, 20, 60);//L1
   if(L1)rect(330-150*digit, 280, 20, 60);//L2
  }
 for(int digit=0;digit<=2;digit++){
   int drawNumber=(int)(combo/pow(10,digit))%10;//找出要畫的數字
   T1=T2=T3=R1=R2=L1=L2=false;
   switch(drawNumber){
   case 0:T1=T3=R1=R2=L1=L2=true;   break;
   case 1:R1=R2=true;               break;
   case 2:T1=T2=T3=R1=L2=true;      break;
   case 3:T1=T2=T3=R1=R2=true;      break;
   case 4:T2=R1=R2=L1=true;         break;
   case 5:T1=T2=T3=R2=L1=true;      break;
   case 6:T1=T2=T3=R2=L1=L2=true;   break;
   case 7:T1=R1=R2=true;            break;
   case 8:T1=T2=T3=R1=R2=L1=L2=true;break;
   case 9:T1=T2=T3=R1=R2=L1=true;   break;
 }
   if(T1)rect(350-150*digit, 460, 60, 20);//橫一Transverse  T1
   if(R1)rect(410-150*digit, 480, 20, 60);//R1
   if(T2)rect(350-150*digit ,540, 60, 20);//T2
   if(R2)rect(410-150*digit, 560, 20, 60);//R2
   if(T3)rect(350-150*digit, 620, 60, 20);//T3
   if(L2)rect(330-150*digit, 560, 20, 60);//L1
   if(L1)rect(330-150*digit, 480, 20, 60);//L2
  }  

}
ARDUNIO

/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor

 This example code is in the public domain.
 */

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton[] = {2,3,4,5,6,7,8,9,10};
boolean checkstat[] = {false,false,false,false,false,false,false,false,false};
int buttonState=0,otarg=0;
//int targ;
int ans[]= {1,1};
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  for(int i=0;i<9;i++)
  pinMode(pushButton[i], INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:

  // print out the state of the button
  for(int targ=0;targ<9;++targ )
 if( digitalRead(targ+2)==1){
 
    //buttonState = digitalRead(targ);
 
   if(otarg!=targ) {
     otarg=targ;
     Serial.print(targ+2);
   }
     //if((targ+1)==otarg)
   
     //Serial.print(otarg);
    // if((otarg+1)==targ)
     //Serial.print(otarg);
    }
  delay(1);        // delay in between reads for stability
}





Week16,HW12

將期末作品的進度寫出來~

目前進度:

processing程式碼:

import processing.serial.*;
Serial myPort;

PImage bg1,Do,Re,Mi,Fa,So,La,Si;

void setup() {
  size(800, 600);
  bg1 = loadImage("piano.jpg");  //載入圖檔
  Do = loadImage("piano-Do1.jpg");
  Re = loadImage("piano-Re1.jpg");
  Mi = loadImage("piano-Mi1.jpg");
  Fa = loadImage("piano-Fa1.jpg");
  So = loadImage("piano-So1.jpg");
  La = loadImage("piano-La1.jpg");
  Si = loadImage("piano-Si1.jpg"); 
  myPort = new Serial(this, "COM15", 9600);
}

void draw() {
  image(bg1, 0, 0, 800, 600);
  image(Do, 86, 82, 89, 333);
  image(Re, 169, 80, 89, 333);   
  image(Mi, 253, 81, 89, 333);;
  image(Fa, 338.5, 84, 89, 333);
  image(So, 425, 81, 89, 333);;
  image(La, 509, 82, 89, 333);
  image(Si, 593, 82, 89, 333);
}

----------------------------------------------------------------------------------------

arduino程式碼:

const int inPin1 = 1;
const int inPin2 = 2;
const int inPin3 = 3;
const int inPin4 = 4;
const int inPin5 = 5;
const int inPin6 = 6;
const int inPin7 = 7;

int val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0, val7 = 0;

void setup() {
   Serial.begin(9600);
  
   pinMode(inPin1, INPUT);
   pinMode(inPin2, INPUT);
   pinMode(inPin3, INPUT);
   pinMode(inPin4, INPUT);
   pinMode(inPin5, INPUT);
   pinMode(inPin6, INPUT);
   pinMode(inPin7, INPUT);
}

void loop() {
   val1 = digitalRead(inPin1);
   val2 = digitalRead(inPin2);
   val3 = digitalRead(inPin3);
   val4 = digitalRead(inPin4);
   val5 = digitalRead(inPin5);
   val6 = digitalRead(inPin6);
   val7 = digitalRead(inPin7);

      if(val1==HIGH){
         tone(8,524,8);
      }
      if(val2==HIGH){
         tone(8,588,8);
      }
      if(val3==HIGH){
         tone(8,660,8);
      }
      if(val4==HIGH){
         tone(8,698,8);
      }
      if(val5==HIGH){
         tone(8,784,8);
      }
       if(val6==HIGH){
         tone(8,880,8);
      }
       if(val7==HIGH){
         tone(8,988,8);
      }
}


----------------------------------------------------------------------------------------

找素材(圖片):




素材:一包冰棍、黑白色膠帶各一綑、一片珍珠板、
            七個方形按鈕、七個電阻、一個喇叭等。

----------------------------------------------------------------------------------------

心得:

  這次的期末作業,我們想要用冰棍來製作小型的鋼琴鍵。
  可以發出:Do、Re、Mi、Fa、So、La、Si的音階,並
  可以錄音由玩家彈奏的一段旋律,並有儲存、播放的功能。




2013年1月13日 星期日

Week19,Final project

期末作業
遊戲名稱:角子老虎


影片介紹&課堂DEMO




<程式部分 後面有全部的程式>

遊戲方法:
<遊戲登入畫面一  進入畫面>
1. 按任意鍵進入遊戲

<遊戲畫面二 介紹畫面>
2. 配錢方式:7--100$,其他水果--10$

<遊戲畫面三 開始拉罷>
3. 首先滑鼠左鍵點任意位置,下注金額Bet(左下角)

<遊戲畫面四 停止>
4.  接著按Arduino箱子Start按鈕--開始,1按鈕--第一格停止,2按鈕--第二格停止




先使用 processing 建置遊戲畫面


Processing  程式碼


PImage logo; // 登入畫面
PImage logo2; // 登入畫面
PImage gamebg;  //遊戲畫面
PImage bingo; int bingo_show=0; //當bingo時 顯示時間
int value = 0,login=0; int a1=1,a2=1,a3=1;
int sx=125,sy=150,sp=0,sp_2=0,sp_3=0,stop_1,stop_2,stop_3; //sx,sy水果大小
int h=-590; //h-&rt;小於h回到bp點
int x1=70,x2=200,x3=337;
int y11=160,y12=y11+150,y13=y12+150,y14=y13+150,y15=y14+150,y16=y15+150;
int y21=160,y22=y21+150,y23=y22+150,y24=y23+150,y25=y24+150,y26=y25+150;
int y31=160,y32=y31+150,y33=y32+150,y34=y33+150,y35=y34+150,y36=y35+150;
int total=1000,bet,bet_t,money_state=0;
int userlogin=1,login_y=0,login_h=560;//登入畫面
int[] sum = new int[3]; //計分 sum[0]=第一區,sump[1]=第二區...
PImage[] img1 = new PImage[6];//  載入圖片
PImage[] img2 = new PImage[6];//  載入圖片
PImage[] img3 = new PImage[6];//  載入圖片

int state1=0,state2=0,state3=0;

void setup(){
  size(560,520);
  logo = loadImage("login.jpg");  //背景圖
  logo2 = loadImage("fruit.png");  //背景圖
  gamebg = loadImage("gamebg.png"); //遊戲畫面
  bingo = loadImage("bingo.png");
  for(int i=0,j=1;i<6;i++,j++) //載注水果圖
    img1[i] = loadImage(j+".png");
  for(int i=0,j=1;i<6;i++,j++) //載注水果圖
    img2[i] = loadImage(j+".png");
  for(int i=0,j=1;i<6;i++,j++) //載注水果圖
    img3[i] = loadImage(j+".png");
  
  for(int i=0,j=1;i<3;i++,j++) //初始為0;
    sum[i]=0;
}

void draw(){
  
  background(255);
  keyPressed();
  
  if(userlogin==0)  
  {
    sp_rect1(x1,sp);
    sp_rect2(x2,sp_2);
    sp_rect3(x3,sp_3);
  }
  image(gamebg,0,0);
  
  if(sum[0]&rt;0&&sum[1]&rt;0&&sum[2]&rt;0){ //比較停格後的圖是否為相同
    println(sum[0]+" "+sum[1]+" "+sum[2]);
    if(sum[0]==sum[1]&&sum[1]==sum[2]){ 
      money(sum[0]);
      println("bingo");
      sum[0]=0;
      bingo_show=100;      
    }
    sum[0]=0;
    bet=0;
  }
  if(bingo_show&rt;0){  //bingo 圖 出現 , 顯示時間 bingo_show --
    image(bingo,180,300,174,48);
    bingo_show--;
  }
  fill(0, 102, 153);
  String mm = String.valueOf(total);  //顯示金額
  String b = String.valueOf(bet);    //顯示下注金額
  textSize(30);
  text(b, 165, 460,110,50);
  text(mm,445,460,110,50);
  money();
  
  loginimage();
  //println(userlogin);void loginimage(){
  if(userlogin==1) image(logo,0,0);
  if(mousePressed&&userlogin==1)
  {
    userlogin=2; image(logo2,0,login_y);
  }
  if(userlogin==2&&login_h&rt;=0){
    image(logo2,0,login_y--);
    login_h--;
  }
  if(userlogin==2&&login_h==0){
    userlogin=0; println("-----start game!!-----");
  }
}
void money(){  //下注 下注金額 不得超過100元
  if(bet<100){
    if(mousePressed&&userlogin==0){
        bet+=10;
        total-=10;
      }
    if (keyPressed == true&&userlogin==0){
      if(key=='.'){
        bet+=10;
        total-=10;
      }
    }
  }
}

void money(int sum){  //計算獎金
  switch(sum) {
    case 1: 
      print("total="+total+"+"+bet*100);
      total=total+bet*100;
      //print("total:"+bet_t);
      break;
    case 2:
      print("total="+total+"+"+bet*10); 
      total=total+bet*10;
      break;
    case 3: 
      print("total="+total+"+"+bet*10); 
      total=total+bet*10;
      break;
    case 4: 
      print("total="+total+"+"+bet*10); 
      total=total+bet*10;
      break;  
    case 5: 
      print("total="+total+"+"+bet*10); 
      total=total+bet*10;
      break;
    case 6: 
      print("total="+total+"+"+bet*10); 
      total=total+bet*10;
      break;
  }
}

void keyPressed() {
  if (key == ' '&&bingo_show==0&&bet&rt;=10) {  //start
    sp=5;
    sp_2=5;
    sp_3=5;
    state1=0;
    state2=0;
    state3=0;
    sum[0]=0;
    sum[1]=0;
    sum[2]=0;
    bet_t=bet;
    //print("bet:"+bet_t);
  }
  if (key == '1') {  //stop
    sp=0;
    state1=1;
    photo_up1(x1);
    //print("state:"+state);
  }
  if (key == '2') {  //stop
    sp_2=0;
    state2=1;
    photo_up2(x2);
    //print("state:"+state);
  }
  if (key == '3') {  //stop
    sp_3=0;
    state3=1;
    photo_up3(x3);
    //print("state:"+state);
  }

}

void sp_rect1(int x1,int sp_y1){  //讓水果轉動
       // 每張圖大小為 150*125 開始位置 70,160
      image(img1[0],x1,y11=y11-sp_y1,sx,sy); //sp=speed, sx,sy=scale
      image(img1[1],x1,y12=y12-sp_y1,sx,sy);
      image(img1[2],x1,y13=y13-sp_y1,sx,sy);
      image(img1[3],x1,y14=y14-sp_y1,sx,sy);
      image(img1[4],x1,y15=y15-sp_y1,sx,sy);
      image(img1[5],x1,y16=y16-sp_y1,sx,sy);
      if(y11<h){  //當圖y1小於h 就 貼到最後一張
        y11=y16+sy;
      }
      if(y12<h){
        y12=y11+sy;
      }
      if(y13<h){
        y13=y12+sy;
      }
      if(y14<h){
        y14=y13+sy;
      }
      if(y15<h){
        y15=y14+sy;
      }
      if(y16<h){
        y16=y15+sy;
      }
}

void sp_rect2(int x2,int sp_y2){  //讓水果轉動
       // 每張圖大小為 150*125 開始位置 70,160
      image(img2[0],x2,y21=y21-sp_y2,sx,sy); //sp=speed, sx,sy=scale
      image(img2[1],x2,y22=y22-sp_y2,sx,sy);
      image(img2[2],x2,y23=y23-sp_y2,sx,sy);
      image(img2[3],x2,y24=y24-sp_y2,sx,sy);
      image(img2[4],x2,y25=y25-sp_y2,sx,sy);
      image(img2[5],x2,y26=y26-sp_y2,sx,sy);
      if(y21<h){  //當圖y1小於h 就 貼到最後一張
        y21=y26+sy;
      }
      if(y22<h){
        y22=y21+sy;
      }
      if(y23<h){
        y23=y22+sy;
      }
      if(y24<h){
        y24=y23+sy;
      }
      if(y25<h){
        y25=y24+sy;
      }
      if(y26<h){
        y26=y25+sy;
      }
}
void sp_rect3(int x2,int sp_y2){  //讓水果轉動
       // 每張圖大小為 150*125 開始位置 70,160
      image(img3[0],x2,y31=y31-sp_y2,sx,sy); //sp=speed, sx,sy=scale
      image(img3[1],x2,y32=y32-sp_y2,sx,sy);
      image(img3[2],x2,y33=y33-sp_y2,sx,sy);
      image(img3[3],x2,y34=y34-sp_y2,sx,sy);
      image(img3[4],x2,y35=y35-sp_y2,sx,sy);
      image(img3[5],x2,y36=y36-sp_y2,sx,sy);
      if(y31<h){  //當圖y1小於h 就 貼到最後一張
        y31=y36+sy;
      }
      if(y32<h){
        y32=y31+sy;
      }
      if(y33<h){
        y33=y32+sy;
      }
      if(y34<h){
        y34=y33+sy;
      }
      if(y35<h){
        y35=y34+sy;
      }
      if(y36<h){
        y36=y35+sy;
      }
}
//--------------------------------------------------------------------

void photo_up1(int ux1)
{
  //-----第一張圖--------------------------
  if(state1==1) {  //偵測Y停點 ,儲存y數值
      //println("start");
        if(160<=y11&&y11<310){
          stop_1=y11;
          //print("y1");
          sum[0]=1;
        }
        if(160<=y12&&y12<310){
          stop_1=y12;
          //print("y2");
          sum[0]=2;
        }
        if(160<=y13&&y13<310){
          stop_1=y13;
          //print("y3");
          sum[0]=3;
        }
        if(160<=y14&&y14<310){
          stop_1=y14;
          //print("y4");
          sum[0]=4;
        }
        if(160<=y15&&y15<310){
          stop_1=y15;
          //print("y5");
          sum[0]=5;
        }
        if(160<=y16&&y16<310){
          stop_1=y16;
          //print("y6");
          sum[0]=6;
        }
        
    }
    

  if(stop_1&rt;160) //圖片往上到偵測區塊定點
  {
    //println(stop_y);
    //testsp(stop_y);
    image(img1[0],ux1,y11=y11-1,sx,sy); 
    image(img1[1],ux1,y12=y12-1,sx,sy);
    image(img1[2],ux1,y13=y13-1,sx,sy);
    image(img1[3],ux1,y14=y14-1,sx,sy);
    image(img1[4],ux1,y15=y15-1,sx,sy);
    image(img1[5],ux1,y16=y16-1,sx,sy);
    stop_1-=1;
  }
  
}


void photo_up2(int ux2)
{
  //--------第二張圖-----------------------------

  if(state2==1) {  //偵測Y停點 ,儲存y數值
      //println("start");
        if(160<=y21&&y21<310){
          stop_2=y21;
          //print("y1");
          sum[1]=1;
        }
        if(160<=y22&&y22<310){
          stop_2=y22;
          //print("y2");
          sum[1]=2;
        }
        if(160<=y23&&y23<310){
          stop_2=y23;
          //print("y3");
          sum[1]=3;
        }
        if(160<=y24&&y24<310){
          stop_2=y24;
          //print("y4");
          sum[1]=4;
        }
        if(160<=y25&&y25<310){
          stop_2=y25;
          //print("y5");
          sum[1]=5;
        }
        if(160<=y26&&y26<310){
          stop_2=y26;
          //print("y6");
          sum[1]=6;
        }
        
    }
  if(stop_2&rt;160) //圖片往上到偵測區塊定點
  {
    //println(stop_y);
    //testsp(stop_y);
    image(img2[0],ux2,y21=y21-1,sx,sy); 
    image(img2[1],ux2,y22=y22-1,sx,sy);
    image(img2[2],ux2,y23=y23-1,sx,sy);
    image(img2[3],ux2,y24=y24-1,sx,sy);
    image(img2[4],ux2,y25=y25-1,sx,sy);
    image(img2[5],ux2,y26=y26-1,sx,sy);
    stop_2-=1;
  }
} 
  
void photo_up3(int ux3)
{
  //---------第三張圖---------------------------
  
  if(state3==1) {  //偵測Y停點 ,儲存y數值
      //println("start");
        if(160<=y31&&y31<310){
          stop_3=y31;
          //println("y1");
          sum[2]=1;
        }
        if(160<=y32&&y32<310){
          stop_3=y32;
          //println("y2");
          sum[2]=2;
        }
        if(160<=y33&&y33<310){
          stop_3=y33;
          //println("y3");
          sum[2]=3;
        }
        if(160<=y34&&y34<310){
          stop_3=y34;
          //println("y4");
          sum[2]=3;
        }
        if(160<=y35&&y35<310){
          stop_3=y35;
          //println("y5");
          sum[2]=5;
        }
        if(160<=y36&&y36<310){
          stop_3=y36;
          //println("y6");
          sum[2]=6;
        }
    }
    

  if(stop_3&rt;160) //圖片往上到偵測區塊定點
  {
    //println(stop_y);
    //testsp(stop_y);
    image(img3[0],ux3,y31=y31-1,sx,sy); 
    image(img3[1],ux3,y32=y32-1,sx,sy);
    image(img3[2],ux3,y33=y33-1,sx,sy);
    image(img3[3],ux3,y34=y34-1,sx,sy);
    image(img3[4],ux3,y35=y35-1,sx,sy);
    image(img3[5],ux3,y36=y36-1,sx,sy);
    stop_3-=1;
  }
} 
 左邊第一顆按鈕為開始(Start),右邊第一顆按鈕為第一格停止鍵,
第二顆按鈕為第二格停止鍵,第三顆按鈕為第三格停止鍵。
 
Arduino 程式碼

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton1 = 7;
int pushButton2 = 6;
int pushButton3 = 8;
int pushButton4 = 9;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton1, INPUT);
  pinMode(pushButton2, INPUT);
}

int oldState1=0;
int oldState2=2;
int oldState3=4;
int oldState4=6;
// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState1 = digitalRead(pushButton1);
  int buttonState2 = digitalRead(pushButton2);
  int buttonState3 = digitalRead(pushButton3);
  int buttonState4 = digitalRead(pushButton4);
  //----按鈕1----
  if(buttonState1==LOW && oldState1==0){  //按鈕1不按,訊號為0
  // print out the state of the button:
    Serial.print(0);
    oldState1=1;
  }
   if(buttonState1==HIGH && oldState1==1){  //按鈕1按下,訊號為1
  // print out the state of the button:
    Serial.print(1);
    oldState1=0;
  }
  //----按鈕2----
  if(buttonState2==LOW && oldState2==2){  //按鈕2不按,訊號為2
  // print out the state of the button:
    Serial.print(2);
    oldState2=3;
  }
  if(buttonState2==HIGH && oldState2==3){  //按鈕2按下,訊號為3
  // print out the state of the button:
    Serial.print(3);
    oldState2=2;
  }
  //----按鈕3----
  if(buttonState3==LOW && oldState3==4){  //按鈕2不按,訊號為2
  // print out the state of the button:
    Serial.print(4);
    oldState3=5;
  }
  if(buttonState3==HIGH && oldState3==5){  //按鈕2按下,訊號為3
  // print out the state of the button:
    Serial.print(5);
    oldState3=4;
  }
  //----按鈕4----
  if(buttonState4==LOW && oldState4==6){  //按鈕2不按,訊號為2
  // print out the state of the button:
    Serial.print(6);
    oldState4=7;
  }
  if(buttonState4==HIGH && oldState4==5){  //按鈕2按下,訊號為3
  // print out the state of the button:
    Serial.print(7);
    oldState4=6;
  }
  delay(10);        // delay in between reads for stability
}
心得介紹/組員分工情況 

由於這學期十分忙碌,許多課程都需要繳交期末作品,我們星期四考完期末考後便把時間都獻給互動期末作品了,星期五也繼續來在學校研究。主要分為兩大部分:Processing和Arduino,而工作分配由鍾毅軍負責Processing和吳姿儀負責Arduino。
Processing部分程式需要花時間Debug,這種方法不行就換別種方法。
Arduino部分線路常常接觸不良或者不牢固,導致在arduino接收訊號時會有問題,不是沒接收到就是一次跳太多訊號。以及按鈕的選擇似乎不恰當。
最後雖然還有些不足要修改,但還是謝謝和辛苦了!

2012年12月27日 星期四

Week15.HW12

本周將銜接上禮拜的繼續增加按鈕操控的部分
本已大致安排好按鈕位置與功能鍵

但好像跟其他組類似 所以我們有意改做其他的
因為計畫敢不上變化 重新會導致整個作業將從零開始
所以我們也將斟酌情況 製作出遊戲
因為時間緊迫 若成果未如預期 敬請包含
我們將繼續加油 。

2012年12月26日 星期三

Week15,HW12

期末作品進度

作品名稱: 划船遊戲


1、遊戲規則初步想法

利用兩個按鍵不斷輪替按著,讓船有加速度不斷前進,
接著計算其到達終點的時間,讓玩家挑戰最佳時間最短的紀錄。



2、確認作品操控以及進行的方向

我們的想法是由google和逆向思考的小遊戲所結合的,
這周我們討論遊戲的前進控制和玩法,
我們打算利用兩個按鈕控制他的前進方向,
小葉老師有建議我們利用旋鈕來做類似雙渦輪的設定,
但是後來發現,我們利用按鈕簡單的控制物體前進的加速度,
在配合他轉彎X值做加減以及背景地圖的不斷延伸,
就可以讓他有往左前和右前的方向以及速度控制。


3、素材貼圖





























4、心得

在規劃這作品前,我們最擔心的是按鈕的配合,
也就是arduino和processing的連接,因為沒有試過的經驗,
在小葉老師上課的範例中,我們有嘗試著做修改,
arduino是不是可以傳兩個不一樣的值,然後processing可以分別,
但是我們試了很久,還是搞不懂為甚麼他只給我們讀到一個按鈕的訊號,
這是我們這一周所遇到的問題,我們雖然只需要兩個按鍵,
但是這個問題會是我們期末作品最大的一個瓶頸,
所以我想可以馬上解決問題,往完整的作品邁向一大步。















2012年12月25日 星期二

期末

PImage img;
int rx=28,ry=5;
boolean move = true;
void setup() {
  size(400, 400);
  img = loadImage("path.jpg");
}
void draw() {
  image(img, 0, 0);
  loadPixels();
 
  fill(225, 0, 0);//點點
  ellipse(rx, ry, 10, 10);
 
  if (move==true) {
    if(key==97) rx=rx-1;
    if(key==119) ry=ry-1;
    if(key==100) rx=rx+1;
    if(key==115) ry=ry+1;
  }
  color now_color = pixels[ry*400+rx];
 
  if (rx>398){
    rx=398;
    fill(0, 0, 0);
    rect(0, 0, 400, 400);
    textSize(50);
    fill(255, 255, 0);
    text("Congratulation", 25, 220);
  }
 
  if (red(now_color)<100){
    move = false;
    fill(0, 0, 0);
    rect(0, 0, 400, 400);
    textSize(50);
    fill(255, 255, 0);
    text("GAME OVER", 60, 220);
  }
 
  else move = true; 

}

Week15, HW12,

本周進度為成功時做了兩個按鈕,分別控制讓小人不掉落和重新開始遊戲的部分

本周電路板圖片



以下為本周程式碼

processing

import processing.serial.*;
Serial myPort;
int now;
int now2;
int testState=0;
//int testState2=0;
int cfg_width =450;
int cfg_height = 450;
int cfg_maxchange = 5;
float cfg_space_mod = 0.9994;
int cfg_playersize = 5;
float cfg_gravity = 1.0002;
PImage body;
float dir = 1;  // current direction of the player (gravity vs pushing)
long score = 0; //分數
long maxscore = 0; //最高分紀錄
int push = 0; //按空白鍵增加
int mode = 1; // 遊戲狀態, 1=running, 0=lost, -1=lost and score printed
float cur_space = 450;  // space to play
float factor = 0.5;     // how far the cave can move max in the y axis for one x step
// the cave borders
int[] ylinetop = new int[cfg_width];
int[] ylinebottom = new int[cfg_width];
// the player (snake)遊戲的線
float[] yplayer = new float[cfg_width/2];
PFont myFont;
import ddf.minim.*;
Minim minim;
AudioPlayer player;

void initcave() {

  score = 0;
  dir = 1;
  push = 0;
  cur_space = 400;
  body=loadImage("bodyknife.png");

  for (int i = 0; i < width; i++) {
    ylinetop[i] = height/2-int(cur_space)/2;
    ylinebottom[i] = ylinetop[i]+int(cur_space);
  }

  for (int i = 0; i < width/2; i++) {
    yplayer[i] = height/2;
  }
}

void drawframe () {
  drawframe2 ();//我寫的背景變色程式
  // draw cave
  stroke(255, 0, 0);
  for (int i=1; i<width; i++) {  
    line(i, 0, i, ylinetop[i]);
    line(i, ylinebottom[i], i, height);
  }

  if (mode < 1) {
    stroke(0, 255, 0);
    strokeWeight(5);
    fill(100, 255, 0);
    ellipse(width/2, yplayer[width/2 - 1], 150, 150);
  }
  people();
}
//背景變色程式在這邊~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void drawframe2 () {
  if (score/10<10) {//因為他計分我發現他是score有除以10 所以這邊也要把分數除10才會跟顯示的計分看起來一樣
    background(0, 0, 0);//小於10分
  }
  else if (score/10<=50) {
    background(0, 0, 255);//大於10分小於等於50分
  }
  else  if (score/10<100) {
    background(255, 255, 255);//大於等於50分小於100分
  }
  else  if (score/10>=100) {
    background(255, 255, 255);//大於等於100分
  } 
  //要把所有數值的情況考慮進去,不然畫面會閃一下。例如原本是小於100 和大於100 沒有寫道等於100的情況 那100分的時候遊戲畫面會閃一下
}
void people() {//小人改變大小
  for (int i=1; i<width/2; i++) {  
    if (score/10<50) {
      image(body, i, int(yplayer[i]), 80, 80);
    }
    else if (score/10<=100) {//大於50分 小於等於100分
      image(body, i, int(yplayer[i]), 60, 60);
    }
    else  if (score/10<150) {
      image(body, i, int(yplayer[i]), 30, 30);//大於100分小於150分
    }
    else  if (score/10>=150) {
      image(body, i, int(yplayer[i]), 30, 30);//大於等於150分
    }
  }
}

void setup()
{


  size(cfg_width, cfg_height);
  myPort= new Serial(this, "COM3", 9600);

  initcave();
  frameRate(100);
  minim = new Minim(this); //播音樂
  player = minim.loadFile("Kalimba.mp3");  //播音樂
   player.play();  //播音樂
    myFont = loadFont("SansSerif-12.vlw");
   textFont(myFont);
}

void drawscore()
{
  stroke(0);
  strokeWeight(3);
  fill(0);
  text("最高分紀錄: " + int(maxscore/10) + "  分數: " + int(score/10), 1, 12);//計分都有除10在這
  if (mode == -1) {
    text("Game Over!再玩一次.", 2, 26);
  }
}
void draw()
{

  while (myPort.available ()>0) {
    testState = myPort.read();
    println(testState);
    if (testState==49)
    {
      now = 1;
    }
    if (now==1)
      dir *= 0.997;
    if (testState=='a')
    {
      mode = 1;
      initcave();

    }

  if (mode == 1) {

    for (int i = 1; i < width; i++) {
      ylinetop[i-1] = ylinetop[i];
      ylinebottom[i-1] = ylinebottom[i];
    }
 
    for (int i = 1; i < width/2; i++) {
      yplayer[i-1] = yplayer[i];
    }
 
    ylinetop[width-1] =
      constrain( int( random(-cfg_maxchange, cfg_maxchange) + factor)
      + ylinetop[width-1]
      , 0, height-int(cur_space+1));
    ylinebottom[width-1] = ylinetop[width-1] + int(cur_space);

 
    dir = pow(cfg_gravity, dir) * dir;
 
    yplayer[width/2-1] = constrain(yplayer[width/2- 2] * dir, 1, height-2);


    if (yplayer[width/2-1] < ylinetop[width/2] ||
      yplayer[width/2-1] > ylinebottom[width/2]) {
      mode = 0;
    }
 
    cur_space *= cfg_space_mod;

    score += 1;

    if (ylinetop[width-1] < 2 || ylinebottom[width-1] > height-2) {
      factor *= -1;
    }
  }
  else {  // collision detected
    if (mode == 0) {
      // save new max score
      maxscore = maxscore > score ? maxscore : score;
      mode = -1;
    }
  }
  drawframe();
  drawscore();
}

void keyPressed() {

  if (mode == 1 && key == '2') {
    dir *= 0.997;
  }

  if (mode == -1 && (key == '0' )) { //按0重新開始
    mode = 1;
    initcave();
  }
}

ardino

/*
  DigitalReadSerial
 Reads a digital input on pin 2, prints the result to the serial monitor

 This example code is in the public domain.
 */
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
int pushButton2 = 3;
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
  pinMode(pushButton2, INPUT);
}
int a=0;
int a2=0;
// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  int buttonState2 = digitalRead(pushButton2);
  // 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('a');
    a2=1;      // send 1 to Processing
  }
  if (buttonState2 == LOW && a2==1) {                               // If the switch is not ON,
    Serial.println('b'); 
     a2=0;    // send 0 to Processing
  }
 // Serial.println(buttonState);
  delay(10);        // delay in between reads for stability
}


以下為我們的電路板




final


1.processing

import processing.serial.*;
Serial myPort;
int val;
void setup() {
  size(400, 200);
  background(255);
  noStroke();
 // frameRate(30);
  fill(102);
 myPort = new Serial(this, "COM4", 9600);
}
int d=0,r=0,m=0,f=0,s=0;
void draw() {
  if(myPort.available()>0){
    val = myPort.read();//第三個重點:如果讀到數字就拿出來用
    //rect(20, 20, 72, 160);
  }
 
  fill(255, 255, 255);
  stroke(0, 0, 0);
  strokeWeight(2);
  if(val=='G'){
   d=1;
  }
  else if(val=='B'){
   d=0;
  }
  if(val=='F'){
   r=1;
  }
  else if(val=='V'){
   r=0;
  }
  if(val=='D'){
   m=1;
  }
  else if(val=='C'){
   m=0;
  }
  if(val=='S'){
   f=1;
  }
  else if(val=='X'){
   f=0;
  }
  if(val=='A'){
   s=1;
  }
  else if(val=='Z'){
   s=0;
  }
  if(val=='H'){
    d=0;
    r=0;
    m=0;
    f=0;
    s=0;
  }
 
 //println(doo);
  if(s==1){
    fill(255,0,0);
   rect(20, 20, 72, 160);
  }
  else if(s==0){
    fill(255);
   rect(20, 20, 72, 160);
  }
 
   if(f==1){
    fill(255,0,0);
    rect(92, 20, 72, 160);
  }
  else if(f==0){
    fill(255);
   rect(92, 20, 72, 160);
  }
   if(m==1){
    fill(255,0,0);
     rect(164, 20, 72, 160);
  }
  else if(m==0){
    fill(255);
     rect(164, 20, 72, 160);
  }
   if(r==1){
    fill(255,0,0);
    rect(236, 20, 72, 160);
  }
  else if(r==0){
    fill(255);
    rect(236, 20, 72, 160);
  }
   if(d==1){
    fill(255,0,0);
    rect(308, 20, 72, 160);
  }
  else if(d==0){
    fill(255);
    rect(308, 20, 72, 160);
  }
  fill(0);
  rect(74, 20, 36, 80);
  rect(146, 20, 36, 80);
  rect(290, 20, 36, 80);
  rect(362, 20, 18, 80);
}

2.arduinno

int buttonPin = 2;
int buttonPin3 = 3;
int buttonPin4 = 4;
int buttonPin5 = 5;
int buttonPin6 = 6;
int buttonState = 0; 
int buttonState3 = 0; 
int buttonState4 = 0; 
int buttonState5 = 0; 
int buttonState6 = 0; 
void setup() {

  pinMode(buttonPin, INPUT);    
  pinMode(buttonPin3, INPUT);    
  pinMode(buttonPin4, INPUT);    
  pinMode(buttonPin5, INPUT);    
  pinMode(buttonPin6, INPUT);    
    Serial.begin(9600);

}
void loop() {
  buttonState = digitalRead(buttonPin);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  buttonState6 = digitalRead(buttonPin6);
 
  if (buttonState == HIGH) {    
   tone(8, 131, 300);
   Serial.write('A');
  } 
  else if (buttonState == LOW) {
   Serial.write('Z');
  }
 
  if (buttonState3== HIGH) {    
   tone(8, 147 , 300);
   Serial.print('S');
  }
  else if (buttonState3 == LOW) {
   Serial.write('X');
  }
  if (buttonState4 == HIGH) {    
   tone(8, 165 , 300);
   Serial.print('D');
  }
   else if (buttonState4 == LOW) {
   Serial.write('C');
  }
  if (buttonState5 == HIGH) {    
   tone(8, 175 , 300);
   Serial.print('F');
  }
   else if (buttonState5 == LOW) {
   Serial.write('V');
  }
  if (buttonState6 == HIGH) {    
   tone(8, 196, 300);
   Serial.print('G');
  }
   else if (buttonState6 == LOW) {
   Serial.write('B');
  }
 else {
   Serial.print('H');
  }
   delay(100);
}

3.展示歌曲 火車快飛

5531 5531 2344 3455

5353 231

4222 3111

2342111

week15,hw12

1.void setup() {
  size(400, 200);
  background(255);
  noStroke();
  fill(102);
}
void draw() {
  fill(255, 255, 255);
  stroke(0, 0, 0);
  strokeWeight(2);
  rect(20, 20, 72, 160);
  rect(92, 20, 72, 160);
  rect(164, 20, 72, 160);
  rect(236, 20, 72, 160);
  rect(308, 20, 72, 160);
  fill(0);
  rect(74, 20, 36, 80);
  rect(146, 20, 36, 80);
  rect(290, 20, 36, 80);
  rect(362, 20, 18, 80);
 
}



畫出一個鋼琴鍵盤,之後再跟arduino結合,按出一個音,鋼琴鍵盤就會變紅色。

Week15,HW11

我們這組目前期末作業的計劃是要延續期中作品:大方快吃小方塊,期末再加上Arduino來連接四顆按鈕,上下左右來控制紅色方塊。

              
    現在我們的進度是在思考和修正如何將其中程式碼的滑鼠事件,轉換成按鈕來控制,也因為之前的程式碼只輸入了方塊只要跟著滑鼠移動而已,所以簡單許多,而這次要在加上四顆按鈕對我們來說也相對較困難,再加上要將之前遊戲執行後的問題想要一併解決,也就是有時候會一開啟就碰到大方塊就馬上遊戲結束的問題,因此也讓期末作業困難度又增加了。

week15,hw12

我們是打算使用之前其中的程式,作出類似實體的jubeat的遊戲,可選歌並將透明鍵盤放螢幕上在進行遊戲。

Week15,HW12

期末構想:角子老虎機
 目前進度:
所需素材、登入畫面和遊戲畫面


       















登入畫面



















遊戲畫面







素材