2022年10月17日 星期一

晚安

 暗棋(shuffle)

```
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}
};
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);
}
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);
      }
    }
  }
}
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;
      }
    }
  }
}
String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};
String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};
void drawChess(int x, int y, int id) {
  fill(255);
  ellipse( x, y, 40, 40);
  if (id>0) {//黑
    fill(0);
    text( name[id-1], x, y-3);
  } else {//紅
    fill(255, 0, 0);
    text( name2[-id-1], x, y-3);
  }
}
```
上上週暗棋程式碼


for迴圈執行1000次交換動作達成洗牌













```
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}
};
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);
      }
    }
  }
}
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;
      }
    }
  }
}
String [] name = {"將", "士", "象", "車", "馬", "包", "卒"};
String [] name2 = {"帥", "仕", "相", "俥", "傌", "炮", "兵"};
void drawChess(int x, int y, int id) {
  fill(255);
  ellipse( x, y, 40, 40);
  if (id>0) {//黑
    fill(0);
    text( name[id-1], x, y-3);
  } else {//紅
    fill(255, 0, 0);
    text( name2[-id-1], x, y-3);
  }
}
```

移動棋子

點擊欲移動的棋子,用綠色的遮罩顯示













```
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}
};
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){
      drawChess(50+25+moveJ*50, 50+25+moveI*50, 9);
    }
  }
}
int moveI =-1 , moveJ=-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; //移動棋子的i座標
          moveJ =j;   //移動棋子的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);
  }
}
```

棋子移動中跟隨滑鼠













```
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}
};
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; //移動棋子的i座標
          moveJ =j; //移動棋子的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);
  }
}
```



滑鼠鬆開後完成吃掉棋子













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

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; //移動棋子的i座標
          moveJ =j;   //移動棋子的j座標
          moveID = board[i][j];  //移動的棋子
          moving =true; //棋子移動中
        }
      }
    }
  }
}
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>0) {//黑
    fill(0);
    text( name[id-1], x, y-3);
  } else {//紅
    fill(255, 0, 0);
    text( name2[-id-1], x, y-3);
  }
}

```

音樂播放

程式碼存檔後,將mp3拉近視窗中
在存檔的資料夾內即可看到存入的mp3














**安裝外掛**

執行時播放音樂














```
//存檔,mp3拉進來,ctrl+K查看檔案即可看到音樂
import processing.sound.*;  //匯入音樂函式庫
//要安裝外掛功能


void setup(){
  SoundFile file =new SoundFile(this, "Intro Song_Final.mp3");
  file.play();
}
void draw(){

}
void mousePressed(){
  SoundFile file2= new SoundFile(this, "In Game Music.mp3");
  file2.play();    ///滑鼠點擊後播放另一首歌
}
```

改寫一下,先宣告變數再將音檔存入














```
//存檔,mp3拉進來,ctrl+K查看檔案即可看到音樂
import processing.sound.*;  //匯入音樂函式庫
//要安裝外掛功能

SoundFile file1, file2, file3, file4; ///宣告變數
void setup(){ 
  file1 =new SoundFile(this, "Intro Song_Final.mp3");
  file2 =new SoundFile(this, "In Game Music.mp3");
  file3 =new SoundFile(this, "Fruit Missed.mp3");
  file4 =new SoundFile(this, "Cannon Blast.mp3");
  
  file1.play();
}
void draw(){

}
void mousePressed(){
  file2.play();
}
void keyPressed(){ 
  file3.play();
}```

沒有留言:

張貼留言