顯示具有 05160673_林敬諺 標籤的文章。 顯示所有文章
顯示具有 05160673_林敬諺 標籤的文章。 顯示所有文章

2019年1月3日 星期四

week17 期末作品

這次的作品也跟期中一樣是打磚塊
有processing+arduion做搭配
盒子


盒子

所以我有做了一個實體的箱子
這箱子上有三個東西
由左到右分別是搖桿、轉盤和按鈕

這次的程式 大多繼承上次打磚塊的元素
但這次有多加一個介面
就是設定
(共四個介面)
裡面有個數值可以讓玩家調控
1.音樂聲音
因為上次原本設定的聲音太小
最後手動在程式裡調整
2.發射球的速度
我一開始的介面的背景有換成當某球反彈,就會自動隨機產生圓環,自動消失

操作版(攝影)

操作版(2d)

要如何操作我的作品(遊戲)呢
1.把usb插到正確的插槽
2.啟動程式
3.當有黃色的顏色在某物體周圍,代表可以在目前的狀態下做某事
4_1.一開始是遊戲的首頁,有兩個選項,上面是設定,而下面是遊戲開始
4_2.搖桿的Y軸(動作是上下擺動),就能選擇要哪個選項
4_3.確定就按下按鈕,進入下個畫面
5_1.如果到了設定,裡面能設定音樂聲音和發射球的速度
5_2.音樂聲音初始是50,範圍0~100
5_3.發射球的速度初始是10,範圍5~15
5_4.這裡搖桿的Y軸是選擇哪一個選項,搖桿的X軸是調整各項的數值
5_5.用按鈕按下back,回到原本畫面
6_1.當進到玩遊戲介面,遊戲規則跟期中一樣,當磚塊碰到下面黃色的線就gameover
6_2.這個介面也能選擇設定
6_3.在發射的機台上多了一個瞄準線,能知道球往哪射出
6_4.當旋轉轉盤,就能讓玩家想射哪就射哪,但有個限制,就是不會有角度低的方向,避免無線循環
6_5.搖動搖桿,搖桿的X軸是調整發射的機台X軸的位置
6_6.當選項是在發射的機台上,按下按鈕就能發射出球
7_1.當gameover,最後的介面還是有top 5
7_2.如果想離開,就搖桿的X軸往左,並按下按鈕,就遊戲結束
7_3.如果想在玩,就搖桿的X軸往右,並按下按鈕,就遊戲重新開始

2018年12月6日 星期四

2018/11/22 課堂筆記 week13

先安裝 驅動
設定版子COM6
這次使用到 程式 Arduino  processing 兩個作互動

Arduino

int switchPin = 3;                       // Switch connected to pin 4

void setup() {
  pinMode(switchPin, INPUT_PULLUP);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
    Serial.write(1);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(0);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}

processing

/**
 * Simple Read
 * 
 * Read data from the serial port and change the color of a rectangle
 * when a switch connected to a Wiring or Arduino board is pressed and released.
 * This example works with the Wiring / Arduino program that follows below.
 */


import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup() 
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, "COM6", 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  } 
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
}



/*

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4;                       // Switch connected to pin 4

void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
    Serial.write(1);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.write(0);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}

*/

2018/11/29 課堂筆記 week12

製作光劍

2018年11月22日 星期四

2018/11/22 課堂筆記 week011

先根據之前的步驟 將 驅動程式裝好
https://makeruno.com.my/playing-melody-using-only-maker-uno/?fbclid=IwAR1Rj-A5MI94dSqnHSPBY8qYf_U1jicjZTHxvPpB0_vTG7RCw9PmaDwCVao
將這個網址的程式碼複製 使用

讓LED逐一亮起的程式

void setup() {
  for(int i=2;i<=13;i++){
    pinMode(i, OUTPUT);
  }
}

void loop() {
  for(int i=2;i<=13;i++){
    digitalWrite(i, HIGH);
    delay(300);
  }
  for(int i=2;i<=13;i++){
    digitalWrite(i, LOW);
  }
}



修改成會亮對應LED程式

int piezoPin = 8;
int length = 15;
char notes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc ";      // 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(piezoPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(piezoPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) 
{
  int LED[] =    {  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  9 , 10  };  ///加入陣列
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  for (int i = 0; i < 8; i++) 
  {
    if (names[i] == note) 
    {
      digitalWrite( LED[i], HIGH ); ///在播聲音之前,把對應的LED亮起
      playTone(tones[i], duration);
      digitalWrite( LED[i], LOW ); ///在播聲音之後,把對應的LED暗掉
    }
  }
}

void setup() 
{
  //pinMode(piezoPin, OUTPUT);
  for(int i=2;i<=13;i++)  pinMode( i, OUTPUT ); ///在執行之前,把每個腳位,都設成OUTPUT,才會亮、發聲音
}

void loop() 
{
  for (int i = 0; i < length; i++) 
  {
    if (notes[i] == ' ') 
    {
      delay(beats[i] * tempo);
    } else 
      playNote(notes[i], beats[i] * tempo);
    delay(tempo / 2); 
  }
}



透過按鈕改音樂(需要按著等到前個音樂結束)

int piezoPin = 8;
int button = 2; ///把Button切換到pin2
int lengths = 26;
char jbnotes[] = "eeeeeeegcde fffffeeeeddedg";
int jbbeats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2}; ///另一首歌
int length = 15;
char ttlsnotes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc ";
int ttlsbeats[] = { 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(piezoPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(piezoPin, 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 };
  for (int i = 0; i < 8; i++) 
  {
    if (names[i] == note) 
    {
      playTone(tones[i], duration);
    }
  }
}

void setup() 
{
  pinMode(piezoPin, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop() 
{
  int sensorVal = digitalRead(button);
  if (sensorVal == HIGH)
  {
    for (int i = 0; i < length; i++) 
    {
      if (ttlsnotes[i] == ' ') 
      {
        delay(ttlsbeats[i] * tempo);
      } 
      else 
        playNote(ttlsnotes[i], ttlsbeats[i] * tempo);
      delay(tempo / 2); 
    }
  }
  else
  {
    for (int i = 0; i < lengths; i++) 
    {
      if (jbnotes[i] == ' ') 
      {
        delay(jbbeats[i] * tempo);
      } 
      else 
        playNote(jbnotes[i], jbbeats[i] * tempo);
      delay(tempo / 2); 
    }
  }
}



一按切換

int piezoPin = 8;
int button = 2;
int lengths = 26;
char jbnotes[] = "eeeeeeegcde fffffeeeeddedg";
int jbbeats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};
int length = 15;
char ttlsnotes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc ";
int ttlsbeats[] = { 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(piezoPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(piezoPin, 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 };
  for (int i = 0; i < 8; i++) 
  {
    if (names[i] == note) 
    {
      playTone(tones[i], duration);
    }
  }
}

void setup() 
{
  pinMode(piezoPin, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}
int now=1;  ///更改歌的變數
void loop() 
{
  delay(tempo/2); //int sensorVal = digitalRead(button);  把舊的註解掉,改成延遲幾秒再執行
  if (now==1)
  {
    for (int i = 0; i < length; i++) 
    {
      if (ttlsnotes[i] == ' ') 
      {
        delay(ttlsbeats[i] * tempo);
      } 
      else 
        playNote(ttlsnotes[i], ttlsbeats[i] * tempo);
        if( digitalRead(button) == LOW) {now = 2; break;} ///卡歌,換下一首
      delay(tempo / 2); 
    }
  }
  else
  {
    for (int i = 0; i < lengths; i++) 
    {
      if (jbnotes[i] == ' ') 
      {
        delay(jbbeats[i] * tempo);
      } 
      else 
        playNote(jbnotes[i], jbbeats[i] * tempo);
        if( digitalRead(button) == LOW) {now = 1; break;} ///卡歌,換前一首
      delay(tempo / 2); 
    }
  }
}

2018年11月15日 星期四

2018/11/15 課堂筆記 week010

網站 https://makeruno.com.my/getting-started/



下載驅動



下載成功可以至裝置管理員查看

再來利用範例程式測試   安裝是否成功

測試程式使用小蜜蜂
int speakerPin = 8;   

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); 
  }
}

2018/11/8 課堂筆記 week09 期中考

期中考

2018年10月31日 星期三

week08 期中作品

我們這組的組題是打磚塊
這個遊戲共有三個頁面



一開啟遊戲會出現遊戲的首頁,背景音樂就會撥放
畫面有三個五顏六色的球,球反彈時會發出聲音
按下箭頭圖案,就遊戲開始

第一個畫面


之後就會出現第二個畫面,就要開始打磚塊了
規則:
1.每個磚塊都有自己個生命值,磚塊生命值小於1時,磚塊才會消失
2.當任一個磚塊碰到下面那條黃線時,遊戲就會結束
3.每層都會自動多一顆球
操作:
當滑鼠點擊時,球會就會發射,會有個聲音
當球撞到磚塊時,磚塊生命值減一,在磚塊消失時,磚塊會發出爆裂聲

如果玩家不小心或沒注意,導致某磚塊觸碰到警戒線,遊戲就會結束,跳至最後一個畫面

第二個畫面

最後一個畫面
上方 顯示你最後到第幾Level
中間 有個排行榜,會紀錄TOP 5玩家玩的層數,關起能記住資料
下方 有兩個圖案
1.按下左邊圖案是離開這個遊戲
2.按下右邊圖案是會回到遊戲的首頁

最後一個畫面

賣點:
1.有開始遊戲、玩遊戲和遊戲結束三個不同的畫面
2.幾乎都有互動的聲音
3.磚塊都有自己的生命值
4.有紀錄成績的功能
5.這程式有用到class

困難點:
1.磚塊的邊界處理

2018年10月18日 星期四

2018/10/25 課堂筆記 week07

int life=10;
boolean gameOver=false;
void setup()
{
  size(500,500);
}
void draw()
{
  background(0,0,205);
  fill(255);rect(50,50,100,20);
  fill(255,0,0);rect(50,50,life*10,20);
  if(gameOver)
  {
    textSize(40);
    text("Game Over",150,150);
  }
}
void mousePressed()
{
  life--;
  if(life<=0) gameOver=true;
}


int state=0;
int remainTime=0;
void setup()
{
  size(800,600);
}
void draw()
{
  if(state==0)
  {
    drawOP();
    if(key=='1')
    {
      state=1; remainTime=5*60;
    }
  }
  else if(state==1)
  {
    drawPlayer();
  }
  else if(state==2)
  {
    drawED();
  }
}
void drawED()
{
  background(0);
  fill(#F0D90C);text("Game Over",100,100);
}
void drawPlayer()
{
  background(0);
  fill(#161FF2);text("Playing... You can press 2 to End",10,100);
  text("remain:"+remainTime,10,200);
  remainTime--;
  if(remainTime<=0) state=2;
}
void drawOP()
{
  background(0);
  fill(#F5DE07);
  textSize(30);text("Opening... You can press 1 to play",10,100);
}




2018年10月11日 星期四

2018/10/18 課堂筆記 week06

float fruitX, fruitY, fruitVX, fruitVY;
void setup()
{
  size(800,600);
  fruitX=30;  fruitVX=3;
  fruitY=700;  fruitVY=-35;
}
void draw()
{
  background(0);
  ellipse(fruitX,fruitY,100,100);
  fruitX+=fruitVX;
  fruitY+=fruitVY;
  fruitVY+=0.98;
}

float []fruitX=new float[20];
float []fruitY=new float[20];
float []fruitVX=new float[20];
float []fruitVY=new float[20];
void setup()
{
  size(800,600);
  for(int i=0;i<20;i++)
  {
  fruitX[i]=random(800);  fruitVX[i]=3;
  fruitY[i]=700;  fruitVY[i]=-35;
  }
}
void draw()
{
  background(0);
  for(int i=0;i<20;i++)
  {
  ellipse(fruitX[i],fruitY[i],100,100);
  fruitX[i]+=fruitVX[i];
  fruitY[i]+=fruitVY[i];
  fruitVY[i]+=0.98;
  }
}

float []fruitX=new float[20];
float []fruitY=new float[20];
float []fruitVX=new float[20];
float []fruitVY=new float[20];
void reborn(int i)
{
  fruitX[i]=random(800); fruitVX[i]=random(6)-3;
  fruitY[i]=700;  fruitVY[i]=-35;
}
void setup()
{
  size(800,600);
  for(int i=0;i<20;i++)
  {
    reborn(i);
  }
}
void draw()
{
  background(0);
  for(int i=0;i<20;i++)
  {
    if(dist(mouseX, mouseY,fruitX[i],fruitY[i])<=50)
    {
      reborn(i);continue;
    }
  ellipse(fruitX[i],fruitY[i],100,100);
  fruitX[i]+=fruitVX[i];
  fruitY[i]+=fruitVY[i];
  fruitVY[i]+=0.98;
  if(fruitY[i]>700) reborn(i);
  }
}

2018年10月4日 星期四

2018/10/11 課堂筆記 week05

float[] eggX=new float[100];
float[] eggY=new float[100];
boolean[] eggDie=new boolean[100];
void setup()  
{
  size(800,600);
  for(int i=0;i<100;i++)
  {
    eggX[i]=random(100,700);
    eggY[i]=-random(2000);
    eggDie[i]=false;
  }
}
void draw()
{
  background(255);
  rect(mouseX-50,mouseY-25,100,50);
  for(int i=0;i<100;i++)
  {
    if(eggDie[i])continue;
    ellipse(eggX[i],eggY[i],80,100);
    eggY[i]+=5;
    if(eggY[i]>700)eggY[i]=-2000+600;
    if(dist(mouseX-50,mouseY-25,eggX[i],eggY[i])<50)
    {
      eggDie[i]=true;
    }
  }
}


PImage [] imgN=new PImage[10];
float[] eggX=new float[100];
float[] eggY=new float[100];
boolean[] eggDie=new boolean[100];
void setup()  
{
  size(800,600);
  for(int i=0;i<=9;i++) imgN[i]=loadImage(i+".png");
  for(int i=0;i<100;i++) 
  {
    eggX[i]=random(100,700);
    eggY[i]=-random(2000);
    eggDie[i]=false;
  }
}
int score=0;
void draw()
{
  background(255);
  fill(255);rect(mouseX-50,mouseY-25,100,50);
  for(int i=0;i<100;i++)
  {
    if(eggDie[i])continue;
    ellipse(eggX[i],eggY[i],80,100);
    eggY[i]+=5;
    if(eggY[i]>700)eggY[i]=-2000+600;
    if(dist(mouseX-50,mouseY-25,eggX[i],eggY[i])<50)
    {
      eggDie[i]=true;
      score+=100;
    }
  }
  int now=score;
  for(int i=0; i<7;i++)
  {
    image(imgN[(now%10)],800-i*100,100,100,150);
    now/=10;
  }

}


PImage img;  //(未完成)
float cherryX=0, cherryY=700;
float cherryVX=6, cherryVY=-40;
void setup()
{
  size(800,600);
  for(int i=0;i<8;i++) img[i]=loadImage("cherry.jpg");
  for(int i=0;i<10;i++) sword[i]=new PVector();
}
PVector [] sword = new PVector[10];
void draw()
{
  background(255);
  image(img,cherryX,cherryY,100,150);
  cherryX +=cherryVX;
  cherryY +=cherryVY;
  cherryY +=0.98;
  
  
  for(int i=9;i>0;i--)
  {
    sword[i].x=sword[i-1].x;sword[i].y=sword[i-1].y;
  }
  sword[0].x=mouseX; sword[0].y=mouseY;
  for(int i=1;i<10;i++)
  {
    line(sword[i].x, sword[i].y, sword[i-1].x, sword[i-1].y);
  }
}