顯示具有 99161171黃慶華 標籤的文章。 顯示所有文章
顯示具有 99161171黃慶華 標籤的文章。 顯示所有文章

2013年1月18日 星期五

秒咪記憶遊戲
一,遊戲構思和介紹
動機:
由於現在人使用大量的電子產品,很多事情都賴於墊子產品的紀錄功能,而使用到腦袋來記憶的機會越來越少,因此我們想設計出一個記憶遊戲,除了富含娛樂功能,有能激活大家的記憶細胞:D
介紹;
就跟大部分的記憶遊戲玩法差不多。主遊戲畫面上共排列不同的五張貓咪圖片,當遊戲開始時,會依照所選擇的難度,隨機亮起不同的圖片,而玩家須要記得圖片的排列順序,依照順序按下按鈕後,再按下確認鍵確認答案,接著遊戲會給予回饋,看是答對或失敗,也可在選擇難度繼續遊戲,或是離開。

二,畫面介紹
↓ 剛進入的遊戲起始畫面:
↓ 按下確認鍵後,會進入到選擇難度的畫面:


↓ 選擇難度後,會進入到遊戲主畫面:
 
↓ 會隨機出現
 
 ↓ 遊戲過關畫面:
↓ 遊戲失敗畫面: 
 
↓ 遊戲離開畫面:



↓ 成功破最難關卡的畫面: 
  
三,硬體
遊戲裝置上有六個按鈕,其中五個白色保麗龍按鈕是用來回答題目順序的,而右下角的紅色按鈕則是確認紐。


四,程式碼
Arduino:
//大的圓形鈕
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
const int buttonPin5 = 6;
const int buttonPin6 = 7;  //確認鈕

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int input=0, oldInput=0, a=0;

void setup(){
  Serial.begin(9600);   
  
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
}

void loop(){
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);  
  buttonState3 = digitalRead(buttonPin3); 
  buttonState4 = digitalRead(buttonPin4); 
  buttonState5 = digitalRead(buttonPin5); 
  buttonState6 = digitalRead(buttonPin6); 
  
  if(buttonState1== HIGH) input=1;
  else if(buttonState2== HIGH) input=2;
  else if(buttonState3== HIGH) input=3;
  else if(buttonState4== HIGH) input=4;
  else if(buttonState5== HIGH) input=5;
  else if(buttonState6== HIGH) input=6;
  else input=0;
  
  if(input!=oldInput){
    if(input==1){
      Serial.print(1);
      oldInput=input;
      a=1;
    }else if(input==2){
      Serial.print(2);
      oldInput=input;
      a=2;
    }else if(input==3){
      Serial.print(3);
      oldInput=input;
      a=3;
    }else if(input==4){
      Serial.print(4);
      oldInput=input;
      a=4;
    }else if(input==5){
      Serial.print(5);
      oldInput=input;
      a=5;
    }else if(input==6){
      Serial.print(6);
      oldInput=input;
      a=6;
    }
  }
}
Processing:
import processing.serial.*;
Serial myport;

PImage rule, start;  //開始
PImage easy, nor, hard, bEasy, bNormal, bHard;  //選單
PImage gmioumi1, gmioumi2, gmioumi3, gmioumi4, gmioumi5;  //底圖
PImage mioumi1, mioumi2, mioumi3, mioumi4, mioumi5;  //亮圖
PImage pass, over, win, End;//結果
float x=0, y=0, z=0, oldx=0, nowx=0;
float xTime=3;
String str="", str2="",oldstr2="", a="6",b="6", c="", oldc="";
int inByte=0, choose=0, end=3, e=0, f=0, g=0;

void setup(){
  myport=new Serial(this,"COM3",9600);
  //開始
  rule = loadImage("rule.jpg");
  start = loadImage("start.png");
  //選單
  easy = loadImage("easy.png");
  nor = loadImage("normal.png");
  hard = loadImage("hard.png");
  bEasy = loadImage("bEasy.jpg");
  bNormal = loadImage("bNormal.jpg");
  bHard = loadImage("bHard.jpg");
  //底圖
  gmioumi1 = loadImage("g1.jpg");
  gmioumi2 = loadImage("g2.jpg");
  gmioumi3 = loadImage("g3.jpg");
  gmioumi4 = loadImage("g4.jpg");
  gmioumi5 = loadImage("g5.jpg");
  //亮圖
  mioumi1 = loadImage("c1.png");
  mioumi2 = loadImage("c2.png");
  mioumi3 = loadImage("c3.png");
  mioumi4 = loadImage("c4.png");
  mioumi5 = loadImage("c5.png"); 
  //結果
  pass = loadImage("pass.jpg");
  over = loadImage("over.jpg");
  win = loadImage("win.jpg");
  End = loadImage("end.jpg");
  size(600,400);
  background(0,0,0);
}

void draw(){
  if(myport.available()>0){  //測試有無訊號來
    inByte=myport.read();  //如果有來就read
    if(inByte==1+48) c="1";
    else if(inByte==2+48) c="2";
    else if(inByte==3+48) c="3";
    else if(inByte==4+48) c="4";
    else if(inByte==5+48) c="5";
    else if(inByte==6+48) c="6";
  }
  if(c!=oldc){
    println(c);
    oldc=c;
  }
  if(c==""){
    image(rule,50,50,500,250);
    image(start,200,320,200,50);
    e=1;
  }else if(c=="6" && e==1){
    fill(0,0,0);
    rect(0,0,600,400);
    image(easy,50,50,160,50);
    image(nor,220,50,160,50);
    image(hard,390,50,160,50);
    image(bEasy,50,100,160,250);
    image(bNormal,220,100,160,250);
    image(bHard,390,100,160,250);
    c="0";
  }  
  if(c=="1" && e==1){
    choose=1;
    e=2;
    c="0";
  }else if(c=="3" && e==1){ 
    choose=3;
    e=2;
    c="0";
  }else if(c=="5" && e==1){
    choose=5;
    e=2;
    c="0";
  }  
  if(choose==1){
    //easy
    if(y<6){
      if(millis()-xTime>1000){
        x=random(5); 
        b=a;
        nowx=(int)x/1;
        if(nowx==oldx){
          if(nowx==4){
            x=1;
            oldx=(int)x/1;
          }else if(nowx==5){
            x=2;
            oldx=(int)x/1;
          }else{ 
            x=x+1;
            oldx=(int)x/1;
          }  
        }else oldx=(int)x/1;
        if(x>0 && x<=1){
          fill(0,0,0);
          rect(0,0,600,400); 
          image(mioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="1";
        }else if(x>1 && x<=2){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi2,150,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="2";
        }else if(x>2 && x<=3){  
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi3,250,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="3";
        }else if(x>3 && x<=4){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi4,350,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="4";
        }else if(x>4 && x<=5){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi5,450,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          a="5";
        } 
        xTime=millis();
        y=y+1;
        if(y<6 && b!=a){ 
          str=str+a;
          println("*"+str);
          z=0;
        }else if(y<6 && z==0 && b==a){
          str=str+a;
          println("*"+str);
          z=1;
        } 
      }else if(millis()-xTime>600 && millis()-xTime<800){
        fill(0,0,0);
        rect(0,0,600,400);
        image(gmioumi1,50,100,100,100);
        image(gmioumi2,150,100,100,100);
        image(gmioumi3,250,100,100,100);
        image(gmioumi4,350,100,100,100);
        image(gmioumi5,450,100,100,100);
      }
    }else if(e==2){
      fill(0,0,0);
      rect(0,0,600,400);
      image(gmioumi1,50,100,100,100);
      image(gmioumi2,150,100,100,100);
      image(gmioumi3,250,100,100,100);
      image(gmioumi4,350,100,100,100);
      image(gmioumi5,450,100,100,100);
    }
    if(c!=oldstr2){
      if( c=="1" || c=="2" || c=="3" || c=="4" || c=="5"){ 
        oldstr2=c;
        str2=str2+c;
        println("**"+str2+"**");
      }
    } 
  }else if(choose==3){
    //normal
    if(y<8){
      if(millis()-xTime>1000){
        x=random(5); 
        b=a;
        nowx=(int)x/1;
        if(nowx==oldx){
          if(nowx==4){
            x=1;
            oldx=(int)x/1;
          }else if(nowx==5){
            x=2;
            oldx=(int)x/1;
          }else{ 
            x=x+1;
            oldx=(int)x/1;
          }  
        }else oldx=(int)x/1;
        if(x>0 && x<=1){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="1";
        }else if(x>1 && x<=2){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi2,150,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="2";
        }else if(x>2 && x<=3){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi3,250,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="3";
        }else if(x>3 && x<=4){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi4,350,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="4";
        }else if(x>4 && x<=5){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi5,450,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          a="5";
        } 
        xTime=millis();
        y=y+1;
        if(y<8 && b!=a){ 
          str=str+a;
          println(str);
          z=0;
        }else if(y<8 && z==0 && b==a){
          str=str+a;
          println(str);
          z=1;
        } 
      }else if(millis()-xTime>600 && millis()-xTime<800){
        fill(0,0,0);
        rect(0,0,600,400);
        image(gmioumi1,50,100,100,100);
        image(gmioumi2,150,100,100,100);
        image(gmioumi3,250,100,100,100);
        image(gmioumi4,350,100,100,100);
        image(gmioumi5,450,100,100,100);
      }
    }else if(e==2){
      fill(0,0,0);
      rect(0,0,600,400);
      image(gmioumi1,50,100,100,100);
      image(gmioumi2,150,100,100,100);
      image(gmioumi3,250,100,100,100);
      image(gmioumi4,350,100,100,100);
      image(gmioumi5,450,100,100,100);
    }
    if(c!=oldstr2){
      if( c=="1" || c=="2" || c=="3" || c=="4" || c=="5"){ 
        oldstr2=c;
        str2=str2+c;
        println("**"+str2+"**");
      }
    }
  }else if(choose==5){
    //hard
    if(y<9){
      if(millis()-xTime>500){
        x=random(5); 
        b=a;
        nowx=(int)x/1;
        if(nowx==oldx){
          if(nowx==4){
            x=1;
            oldx=(int)x/1;
          }else if(nowx==5){
            x=2;
            oldx=(int)x/1;
          }else{ 
            x=x+1;
            oldx=(int)x/1;
          }  
        }else oldx=(int)x/1;
        if(x>0 && x<=1){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="1";
        }else if(x>1 && x<=2){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi2,150,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="2";
        }else if(x>2 && x<=3){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi3,250,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi4,350,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="3";
        }else if(x>3 && x<=4){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi4,350,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi5,450,100,100,100);
          a="4";
        }else if(x>4 && x<=5){ 
          fill(0,0,0);
          rect(0,0,600,400);
          image(mioumi5,450,100,100,100);
          image(gmioumi1,50,100,100,100);
          image(gmioumi2,150,100,100,100);
          image(gmioumi3,250,100,100,100);
          image(gmioumi4,350,100,100,100);
          a="5";
        } 
        xTime=millis();
        y=y+1;
        if(b!=a){ 
          str=str+a;
          println(str);
          z=0;
        }else if(z==0 && b==a){
          str=str+a;
          println(str);
          z=1;
        } 
      }else if(millis()-xTime>200 && millis()-xTime<400){
        fill(0,0,0);
        rect(0,0,600,400);
        image(gmioumi1,50,100,100,100);
        image(gmioumi2,150,100,100,100);
        image(gmioumi3,250,100,100,100);
        image(gmioumi4,350,100,100,100);
        image(gmioumi5,450,100,100,100);
      }
    }else if(e==2){
      fill(0,0,0);
      rect(0,0,600,400);
      image(gmioumi1,50,100,100,100);
      image(gmioumi2,150,100,100,100);
      image(gmioumi3,250,100,100,100);
      image(gmioumi4,350,100,100,100);
      image(gmioumi5,450,100,100,100);
    }
    if(c!=oldstr2){
      if( c=="1" || c=="2" || c=="3" || c=="4" || c=="5"){ 
        oldstr2=c;
        str2=str2+c;
        println("**"+str2+"**");
        e=3;
      }
    }
  }
  
  if(str=="" || str2==""){
    str="1";
    str2="1";
  }
  
  f=Integer.parseInt(str);
  g=Integer.parseInt(str2);

  //f= new Integer(str).intValue();
  //g= new Integer(str2).intValue(); 
  
  if(c=="6" && e==2){
    if(f==g){
      image(pass,0,0,600,400);
    }else if(f!=g){
      image(over,0,0,600,400);
    }
  }else if(c=="6" && e==3){
    if(f==g){
      image(win,0,0,600,400);
    }else{
      image(over,0,0,600,400);
    }
  }
  if(mousePressed && mouseX>500 && mouseY>300){
    if(choose==1){ 
      choose=3;
      str="";
      str2="";
    }else if(choose==3){ 
      choose=5;
      str="";
      str2="";
    }
    e=1;
  }else if(mousePressed && mouseX<100 && mouseY>300){
    image(End,0,0,600,400);
    c="0";
    e=4;
  }  
}


五,影片
課堂Demo影片:


這是成功最難的關卡之後的影片和畫面:
是顛倒的,抱歉QQ



六,心得
這次的遊戲,在規則上和設計上其實不難,但是我們寫程式的經驗依然不多,導致在過程中遇到了不少有關判斷式與環境不熟悉的問題,也因為這學期實在太多大型作業擠在一起的關係,看到我們自己與各組的作品完成度都沒有很高,感覺有點失望。
我們也是在考完期末考後的幾天才開始趕工、拚到凌晨,雖然很辛苦,但是也學到不少東西,是個難忘的回憶。對我們來說,這作業不只是學到技術層面的東西,也讓我們學到人與人之間的互先幫助與默契。對我們而言,這才是最可貴的。
程式方面,還是有一些bug未修復,但是,我們想說還是有機會的話能把它解決,讓作品呈現最完美的狀態。最後,還是感謝老師、助教和所以同學對我們作品的幫忙與支持。
謝謝 : D

2012年12月4日 星期二

Week13 , HW11

作業一:
Tutorial Melody
程式碼 ↓

int speakerPin = 9;

int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 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); 
  }
}
影片 ↓


作業二:
Tone Melody
程式碼↓
/*
  Melody
 
 Plays a melody 
 
 circuit:
 * 8-ohm speaker on digital pin 8
 
 created 21 Jan 2010
 modified 30 Aug 2011
 by Tom Igoe 

This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/Tone
 
 */
 #include "pitches.h"

const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;
const int buttonPin6 = 6;
const int buttonPin7 = 7;

int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;


// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4,4,4,4,4 };

void setup() {
  
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(buttonPin6, INPUT);
  pinMode(buttonPin7, INPUT);  
   
  pinMode(8, OUTPUT);
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
    buttonState3 = digitalRead(buttonPin3); 
    buttonState4 = digitalRead(buttonPin4); 
    buttonState5 = digitalRead(buttonPin5); 
    buttonState6 = digitalRead(buttonPin6); 
    buttonState7 = digitalRead(buttonPin7); 
    
   if(buttonState3 == HIGH){
      tone(8, 261,4);
      digitalWrite(8, HIGH); 
    }
    if(buttonState4 == HIGH){
      tone(8,294,4);
      digitalWrite(8, HIGH); 
    }
    if(buttonState5 == HIGH){
      tone(8,329,4);0
      digitalWrite(8, HIGH); 
    }
    if(buttonState6 == HIGH){
      tone(8,349,4);
      digitalWrite(8, HIGH); 
    }
    if(buttonState7 == HIGH){
      tone(8,400,4);
      digitalWrite(8, HIGH); 
    }

  
  // no need to repeat the melody.
}


影片 ↓




2012年11月27日 星期二

Week12, HW10

用可變電阻來調整LED燈泡的閃爍頻率

[亮一顆LED]
影片:

程式碼:
/*
  Analog Input
 Demonstrates analog input by reading an analog sensor on analog pin 0 and
 turning on and off a light emitting diode(LED)  connected to digital pin 13. 
 The amount of time the LED will be on and off depends on
 the value obtained by analogRead(). 
 
 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
 
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.
 
 
 Created by David Cuartielles
 modified 30 Aug 2011
 By Tom Igoe
 
 This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/AnalogInput
 
 */

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
int a=0;

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
 Serial.println(sensorValue);
 
  // turn the ledPin on
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  digitalWrite(9, LOW);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);
  //digitalWrite(ledPin, HIGH);
  // stop the program for <sensorValue> milliseconds:
  a=5;
  for(int i=0;i<13;i++)
  {
    if(i<7){
      delay(sensorValue);          
      // turn the ledPin off:        
      digitalWrite(i+7, HIGH);
      digitalWrite(i+6, LOW);  
      // stop the program for for <sensorValue> milliseconds:
      delay(sensorValue);
    }
    if(i>=7){
      delay(sensorValue);          
      // turn the ledPin off:        
      digitalWrite(i+a, HIGH);
      digitalWrite(i+a+1, LOW);  
      // stop the program for for <sensorValue> milliseconds:
      delay(sensorValue);
      a=a-2;
    }  
  }
   
}


[亮兩顆LED]
影片:
[亮三顆LED]
影片:
程式碼:
int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor int a=0; void setup() {   // declare the ledPin as an OUTPUT:   pinMode(13, OUTPUT);   pinMode(12, OUTPUT);   pinMode(11, OUTPUT);   pinMode(10, OUTPUT);   pinMode(9, OUTPUT);   pinMode(8, OUTPUT);   pinMode(7, OUTPUT);   Serial.begin(9600); } void loop() {   // read the value from the sensor:   sensorValue = analogRead(sensorPin);  Serial.println(sensorValue);     // turn the ledPin on   digitalWrite(13, LOW);   digitalWrite(12, LOW);   digitalWrite(11, LOW);   digitalWrite(10, LOW);   digitalWrite(9, LOW);   digitalWrite(8, LOW);   digitalWrite(7, LOW);   //digitalWrite(ledPin, HIGH);   // stop the program for <sensorValue> milliseconds:   a=5;   for(int i=0;i<13;i++)   {     if(i<7){       delay(sensorValue);       // turn the ledPin off:       digitalWrite(i+7, HIGH);       digitalWrite(i+8, HIGH);       digitalWrite(i+9, HIGH);       digitalWrite(i+6, LOW);       // stop the program for for <sensorValue> milliseconds:       delay(sensorValue);     }     if(i>=7){       delay(sensorValue);       // turn the ledPin off:       digitalWrite(i+a, HIGH);       digitalWrite(i+a-1, HIGH);       digitalWrite(i+a-2, HIGH);       digitalWrite(i+a+1, LOW);       // stop the program for for <sensorValue> milliseconds:       delay(sensorValue);       a=a-2;     }     } }

2012年11月26日 星期一

Week11,HW09

1.processing + Arduino能做出什麼?
     Arduino可以使人跟機器的互動更加密切,加上processing是開放式資源使得人們更容易分享彼此的成果,也能學到更多

2.Arduino的應用影片
    遠端連線西洋棋


2012年11月13日 星期二

期中小專題--拋雞蛋

1, 遊戲設計:
雞蛋拋拋樂
一開雞蛋在最下面的籃子,
只要按滑鼠右鍵,便能使雞蛋向上拋,
拋到籃子裡則得一分,到外面則直接結束遊戲 : D

一旦雞蛋在中間籃子準備往最上面的籃子拋時,
雞蛋進到最上面的籃子則籃子會自動往下移一格,
然後最上面產生新的籃子;
若是從中間的籃子往上拋到最上面的籃子失敗時,
則遊戲直接結束。

2, 程式碼:
import ddf.minim.*;
AudioPlayer player;
Minim ponyo;

int score1=0, a=0, b=0, c=0, d=0, s=0;
PImage Background;  //背景
PImage life;  //生命值小雞
PImage die;  //死小雞
PImage egg;  //雞蛋
PImage score;  //"分數"字樣
PImage num[] = new PImage[10];  //分數數字0-9
PImage over;

//籃子
PImage basket1;
PImage basket2;
PImage basket3;
float e=random(3)+1, f=random(3)+1, g=random(3)+1; //速度
float b1X=random(420)+90, b2X=random(420)+90, b3X=random(420)+90;  //籃子原X座標
float b1Y=650, b2Y=450, b3Y=250;  //籃子原Y座標
float eX=285, eY=640;  //雞蛋原座標

float w=0;
int sw=0;

void mouseClicked() {
  w=1;
  sw=1;
  if(eY&rt;250)
  eY-=200;
  score1+=1;
}

void setup() {
  ponyo = new Minim(this);
  player = ponyo.loadFile("Ponyo.mp3", 2048);
  player.play();
  
  size(600, 700);
  Background = loadImage("background_sky.png");
  life = loadImage("life.png");
  die = loadImage("die.png");
  score = loadImage("score.png");
  for (int i=0;i<10;i++)
  {
    num[i]=loadImage(i+".png");
  }
  egg = loadImage("egg.png");

  basket1 = loadImage("basket.png");
  basket2 = loadImage("basket.png");
  basket3 = loadImage("basket.png");
  
  over = loadImage("game-over.png");
}

void draw() {
  //背景
  image(Background, 0, 0, 600, 900);
  

  //雞蛋
  image(egg, eX, eY, 40, 50);
  //籃子
  image(basket1, b1X, b1Y, 90, 50);
  b1X+= e;
  if (sw==0) eX=b1X+25;
  if (b1X&rt;510) {b1X=510;e*=-1;}
  if (b1X<0) {b1X=0;e*=-1;}
  image(basket2, b2X, b2Y, 90, 50);
  b2X+= f;
  if (b2X&rt;510) {b2X=510;f*=-1;}
  if (b2X<0) {b2X=0;f*=-1;}
  image(basket3, b3X, b3Y, 90, 50);
  b3X+= g;
  if (b3X&rt;510) {b3X=510;g*=-1;}
  if (b3X<0) {b3X=0;g*=-1;}
//==============================
  if (b1Y==450 && eX<b1X+45 && eX&rt;b1X-45 && eY<475 && eY&rt;425) {
    //雞蛋位置
    eY=b1Y;
    eX=b1X+25;
  }else if (b2Y==450 && eX<b2X+45 && eX&rt;b2X-45 && eY<475 && eY&rt;425) {
    //雞蛋位置
    eY=b2Y;
    eX=b2X+25;
  }else if (b3Y==450 && eX<b3X+45 && eX&rt;b3X-45 && eY<475 && eY&rt;425) {
    //雞蛋位置
    eY=b3Y;
    eX=b3X+25;
  }else if(b1Y== 250 && eX<b1X+45 && eX&rt;b1X-45 && eY<275 && eY&rt;225){
    eY=b1Y;
    eX=b1X+25;
    w=b2Y;
    b2Y=b1Y;
    b1Y=b3Y;
    b3Y=w;
    eY+=200;
  }else if(b2Y== 250 && eX<b2X+45 && eX&rt;b2X-45 && eY<275 && eY&rt;225){
    eY=b2Y;
    eX=b2X+25;
    w=b2Y;
    b2Y=b1Y;
    b1Y=b3Y;
    b3Y=w;
    eY+=200;
  }else if(b3Y== 250 && eX<b3X+45 && eX&rt;b3X-45 && eY<275 && eY&rt;225){
    eY=b3Y;
    eX=b3X+25;
    w=b2Y;
    b2Y=b1Y;
    b1Y=b3Y;
    b3Y=w;
    eY+=200;
  }else if(eY==640){
    eY=640;
  }else if(eY==b1Y && eX<b1X-25 || eX&rt;b1X+25){ 
    for(int i=0;i<500;i++)eY--;
    image(over, 0, 0, 600, 700);
    /*for(int j=0;j<2;j++){
      if(score1&rt;0)score1-=1;
    }*/
  }else if(eY==b2Y && eX<b2X-25 || eX&rt;b2X+25){ 
    for(int i=0;i<500;i++)eY--;
    image(over, 0, 0, 600, 700);
    /*for(int j=0;j<2;j++){
      if(score1&rt;0)score1-=1;
    }*/
  }else if(eY==b3Y && eX<b3X-25 || eX&rt;b3X+25){ 
    for(int i=0;i<500;i++)eY--;
    image(over, 0, 0, 600, 700);
    /*for(int j=0;j<2;j++){
      if(score1&rt;0)score1-=1;
    }*/
  }
  
  //生命值(小雞)
  //image(life, 0, 30, 70, 50);
  //image(life, 80, 30, 70, 50);
  //image(life, 160, 30, 70, 50);
  //分數
  image(score, 350, 30, 100, 50);
  image(num[score1/1000], 455, 30, 30, 50);
  image(num[score1/100%10], 485, 30, 30, 50);
  image(num[score1/10%10], 515, 30, 30, 50);
  image(num[score1%10], 545, 30, 30, 50);
}
3, 圖片展示:



4, 影片展示:

5, 問題檢討和心得感想:
問題 ↓
最一開始碰到的問題是,要怎麼讓雞蛋往上拋,且有正常的動畫效果,後來的問題則是該如何使不同的籃子從不同的起始點開始移動,並且每個籃子都做不同的速度移動,好不容易都解決了,卻發現無法使籃子和雞蛋同時往下移以產生新的籃子,在這部分卡關了好久,最後終於想到解決辦法,但是雞蛋卻無法做動畫,很傷心 :(

改進 ↓
希望可以把雞蛋的動畫做好,然後關於計分的地方可以再作好一點,並且讓"生命值小雞"可以記錄,如此便能使每個玩家有三條命,不會一個不小心遊戲就結束了。
另外希望畫面可以在精緻豐富一些,例如在最一開始設計一個進入和可以選擇的介面,然後遊戲中的背景音樂能隨分數的增加而改變,也希望可以隨分數的增加來提高遊戲的難易度,最後希望結束畫面再漂亮一點~

心得 ↓
這個作業讓大家都好緊張,然後碰到困難就很慌亂,幸好有同學、學長姐和老師的幫助,才讓我們得以繼續下去,真的很感謝。
和小隊員一起完成一件作品的感覺好棒,過程中也好好玩,雖然目前沒有達到剛開始所設立的目標,但是我相信我們可以的 : D
然後發現任何事情真的都是要花時間和心力去完成,不可能有花少少時間就得到成效的事情。下次會再努力,並且提早時間開始準備!

謝謝大家:D

2012年10月30日 星期二

Week08, HW08

作業8-1.1:之前完成了甚麼?
    已解決了分數無法顯示的問題
    雞蛋上拋的效果(仍有小問題)

作業8-1.2:今天計畫要做甚麼?
     1.籃子移動的效果,隨機的速度和起始位置
     2.完全解決雞蛋上拋的問題
     3.判斷雞蛋是否正確落到籃子上
     4.分數增加的動作
     5.雞蛋落下使生命減少

作業8-1.3:可能會遇到的問題
     (1)籃子移動的運算式
     (2)問題2:雞蛋位置的判斷

作業8-2:今日進度: