2022年10月17日 星期一

Uc的week06互動技術筆記~

 # 2022/10/17

```

1. 複習象棋暗棋(缺亂數shuffle)
2. 記憶卡片遊戲
3. 打字遊戲、水果忍者/鍵盤忍者

```

## 完美的暗棋

```C

1.複製上週程式

2.亂數洗牌

=> for(int k=0;k<1000;k++){

    int i1=int(random(4)),j1=int(random(8));

    int i2=int(random(4)),j2=int(random(8));

    int temp=board[i1][j1];

    board[i1][j1]=board[i2][j2];

    board[i2][j2]=temp;

  }


3.更精進的亂數洗牌

=> 

=>

```

4.移動時可以看出移動的是哪個棋子

```

## FIRST PROGRAM

```C

int [][]show={

  { 0, 0, 0, 0, 0, 0, 0, 0},

  { 0, 0, 0, 0, 0, 0, 0, 0},

  { 0, 0, 0, 0, 0, 0, 0, 0},

  { 0, 0, 0, 0, 0, 0, 0, 0}

};//翻牌前, 0 都不會秀哦! 

int [][]board={

  { 1, 2, 2, 3, 3, 4, 4, 5},

  { 5, 6, 6, 7, 7, 7, 7, 7},

  {-1,-2,-2,-3,-3,-4,-4,-5},

  {-5,-6,-6,-7,-7,-7,-7,-7}

}; //暗棋的格子,比較少 4x8=32個棋子

void setup(){

  size(500,300);

  PFont font = createFont("標楷體", 30);

  textFont(font);

  textAlign(CENTER, CENTER);

  for(int k=0;k<1000;k++){

    int i1=int(random(4)),j1=int(random(8));

    int i2=int(random(4)),j2=int(random(8));

    int temp=board[i1][j1];

    board[i1][j1]=board[i2][j2];

    board[i2][j2]=temp;

  }

}

void draw(){

  background(#F0B82C);

  for (int x=50; x<=450; x+=50) {

    line( x, 50, x, 250);

  }

  for (int y=50; y<=250; y+=50) {

    line( 50, y, 450, y);

  }  

  for(int i=0; i<4; i++){

    for(int j=0; j<8; j++){

      if(show[i][j]==0){

        fill(255);

        ellipse( 50+25+j*50, 50+25+i*50, 40, 40);        

      }else{

        int id = board[i][j];

        drawChess(50+25+j*50, 50+25+i*50, id);

      }

    }

  }

  if(moving){

    fill(0,255,0,128);

    ellipse(50+25+moveJ*50,50+25+moveI*50,40,40);

    drawChess(mouseX,mouseY,moveID);

  }

}

int moveI=-1,moveJ=-1,moveID=-1;

boolean moving=false;

void mousePressed(){

  for(int i=0; i<4; i++){

    for(int j=0; j<8; j++){

      if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){

        if( show[i][j]==0 ) show[i][j] = 1; //沒秀? 秀它

        //之後再加棋子的移動

        else{

          moveI=i;

          moveJ=j;

          moveID=board[i][j];

          moving=true;

        }

      }

    }

  }

}

String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};

String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};

void drawChess(int x, int y, int id){

    fill(255);

    ellipse( x, y, 40, 40);

    //if(id==9){//帶移動中的棋子

    //  fill(0,255,0);

    //  ellipse(x,y,40,40);

    //}else 

     if(id>0){//黑

      fill(0);

      text( name[id-1], x, y-3);

    }else{//紅

      fill(255,0,0);

      text( name2[-id-1], x, y-3);

    }

}

```

## Final Program

>>> 可以吃掉棋子!!!

```C

int [][]show={

  { 0, 0, 0, 0, 0, 0, 0, 0},

  { 0, 0, 0, 0, 0, 0, 0, 0},

  { 0, 0, 0, 0, 0, 0, 0, 0},

  { 0, 0, 0, 0, 0, 0, 0, 0}

};//翻牌前, 0 都不會秀哦! 

int [][]board={

  { 1, 2, 2, 3, 3, 4, 4, 5},

  { 5, 6, 6, 7, 7, 7, 7, 7},

  {-1,-2,-2,-3,-3,-4,-4,-5},

  {-5,-6,-6,-7,-7,-7,-7,-7}

}; //暗棋的格子,比較少 4x8=32個棋子

void setup(){

  size(500,300);

  PFont font = createFont("標楷體", 30);

  textFont(font);

  textAlign(CENTER, CENTER);

  for(int k=0;k<1000;k++){

    int i1=int(random(4)),j1=int(random(8));

    int i2=int(random(4)),j2=int(random(8));

    int temp=board[i1][j1];

    board[i1][j1]=board[i2][j2];

    board[i2][j2]=temp;

  }

}

void draw(){

  background(#F0B82C);

  for (int x=50; x<=450; x+=50) {

    line( x, 50, x, 250);

  }

  for (int y=50; y<=250; y+=50) {

    line( 50, y, 450, y);

  }  

  for(int i=0; i<4; i++){

    for(int j=0; j<8; j++){

      if(show[i][j]==0){

        fill(255);

        ellipse( 50+25+j*50, 50+25+i*50, 40, 40);        

      }else{

        int id = board[i][j];

        drawChess(50+25+j*50, 50+25+i*50, id);

      }

    }

  }

  if(moving){

    fill(0,255,0,128);

    ellipse(50+25+moveJ*50,50+25+moveI*50,40,40);

    drawChess(mouseX,mouseY,moveID);

  }

}

int moveI=-1,moveJ=-1,moveID=-1;

boolean moving=false;

void mousePressed(){

  for(int i=0; i<4; i++){

    for(int j=0; j<8; j++){

      if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){

        if( show[i][j]==0 ) show[i][j] = 1; //沒秀? 秀它

        //之後再加棋子的移動

        else{

          moveI=i;

          moveJ=j;

          moveID=board[i][j];

          moving=true;

        }

      }

    }

  }

}

void mouseReleased(){

  for(int i=0;i<4;i++){

    for(int j=0;j<8;j++){

      if(dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){

        if(moving){

          board[moveI][moveJ]=0;

          board[i][j]=moveID;

          moving=false;

        }

      }

    }

  }

}

String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};

String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};

void drawChess(int x, int y, int id){

    if(id==0)return;

    fill(255);

    ellipse( x, y, 40, 40);

    //if(id==9){//帶移動中的棋子

    //  fill(0,255,0);

    //  ellipse(x,y,40,40);

    //}else 

     if(id>0){//黑

      fill(0);

      text( name[id-1], x, y-3);

    }else{//紅

      fill(255,0,0);

      text( name2[-id-1], x, y-3);

    }

}

```


## 水果忍者

```

1. 上moodle下載MP3檔

2.可以透過Ctrl+F 找到mp3們的檔名

2-1. 將找到的檔名複製起來後丟到第三個網址的後端

3.下載背景音樂

```



### 將音樂檔丟入程式碼

```

1.設定最一開始的音樂背景

=>void setup(){

  SoundFile file=new SoundFile(this,"Intro Song_Final.mp3");

  file.play();

}

2.設定滑鼠點擊後撥放的第二個音檔

=>void mousePressed(){

  SoundFile file2=new SoundFile(this,"In Game Music.mp3");

  file2.play();

}

```

## 播放好幾個音檔

```

1.宣告好幾個音檔

2.一次撥!!

```




沒有留言:

張貼留言