2022年10月17日 星期一

TC*(˙Ⱉ˙ฅ)​-week07

本周上課會用到的網址:https://www.typing.com/dist/student/extras/game_files/keyboard-ninja/sounds/In%20Game%20Music.mp3

放在sound/:Bomb Explode.mp3、Cannon Blast.mp3、Freezing.mp3、Fruit Missed.mp3、Gong.mp3、In Game Music.mp3、Intro Song_Final.mp3、Monkey 1.mp3、Monkey 2.mp3、Monkey 3.mp3、Monkey 4.mp3、SparkSFX.mp3、sword slash.mp3

延續上周把暗棋亂數洗牌寫完,修改部分程式錯誤

第一個程式的名稱:week07_1_dark_chess_shuffle

mousePressed(),上面改4下面改8,不然會出錯

在setup()裡洗牌,用for迴圈洗1000次,宣告兩個變數,隨機取0~3 or 0~7,最後用temp互換

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++){//很多次洗牌1000次

  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++){//10這是錯的,要改成4

    for(int j=0;j<8;j++){//9這是錯的,要改成8

      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);//name2錯了,改成name

    }else{//紅

      fill(255,0,0);

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

    }

}


第二個程式:week07_2_dark_chess_move_eat
按住棋子可以進行翻,再按一次同樣的位置就代表現在要移動這顆棋子(會變成綠色)
drawChess() 設定我們點選要移動的棋子後,那顆棋子要變色
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++){//很多次洗牌1000次
  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){//如果moving畫棋子
    drawChess(50+25+moveJ*50,50+25+moveI*50,9);//9很怪,放到外面第9個
  }
}
int moveI = -1 , moveJ = -1;//用來記錄要移動的棋子位置,-1表示目前沒有
boolean moving = false;//不是移動中,沒有移動
void mousePressed(){
  for(int  i=0;i<4;i++){//10這是錯的,要改成4
    for(int j=0;j<8;j++){//9這是錯的,要改成8
      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){//待移動中的棋子,若id==9就是印綠色
      fill(0,255,0);//綠色
      ellipse( x, y, 40, 40);
    }else if(id>0){//黑
      fill(0);
      text( name[id-1], x, y-3);//name2錯了,改成name
    }else{//紅
      fill(255,0,0);
      text( name2[-id-1], x, y-3);
    }
}

將第二個程式修改一下,移動棋子時可以看見字的顏色,也能知道是要移動哪一個棋子,因此將顏色變成綠色半透明( fill第四個值為透明度)
先註解drawChess(),在全域新增moveID=0,拿來記錄我們拿起來的棋子
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++){//很多次洗牌1000次
  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);//綠色,半透明, fill第四個值為透明度
    ellipse(50+25+moveJ*50,50+25+moveI*50,40,40);//9原來的位置
    drawChess(mouseX,mouseY,moveID);//正在飛行、移動的棋子
  }
}
int moveI = -1 , moveJ = -1 , moveID = -1;
boolean moving = false;//不是移動中
void mousePressed(){
  for(int  i=0;i<4;i++){//10這是錯的,要改成4
    for(int j=0;j<8;j++){//9這是錯的,要改成8
      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);}
    if(id>0){//黑
      fill(0);
      text( name[id-1], x, y-3);//name2錯了,改成name
    }else{//紅
      fill(255,0,0);
      text( name2[-id-1], x, y-3);
    }
}



mouseReleased函式放置棋子,drawChess()函式裡要新增if(id==0) return;
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++){//很多次洗牌1000次
  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);//9原來的位置
    
    drawChess(mouseX,mouseY,moveID);//正在飛行、移動的棋子
  }
}
int moveI = -1 , moveJ = -1 , moveID = -1;
boolean moving = false;//不是移動中
void mousePressed(){
  for(int  i=0;i<4;i++){//10這是錯的,要改成4
    for(int j=0;j<8;j++){//9這是錯的,要改成8
      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;//現在移動中
       }//現在要加棋子的移動
      }
    }
  }
}
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);}
    if(id>0){//黑
      fill(0);
      text( name[id-1], x, y-3);//name2錯了,改成name
    }else{//紅
      fill(255,0,0);
      text( name2[-id-1], x, y-3);
    }
}





學習播放背景音樂

第三個程式:week07_3_keyboard_ninja_mp3

//存檔,mp3拉進來(In Game Music和Intro Song_Final)

//ctrl-k看檔案(或開啟程式素描簿-開啟程式目錄)

import processing.sound.*;//音樂功能

//使用外掛,要先把外掛裝起來!!!

//程式素描本(Sketh)-使用函式庫(Library)-Manage Libraries,找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拉進來(In Game Music和Intro Song_Final和Monkey 1和Fruit Missed)

//ctrl-k看檔案(或開啟程式素描簿-開啟程式目錄)

import processing.sound.*;//音樂功能

//使用外掛,要先把外掛裝起來!!!

//程式素描本(Sketh)-使用函式庫(Library)-Manage Libraries,找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,"Monkey 1.mp3");

  file4 = new SoundFile(this,"Fruit Missed.mp3");

  

  file1.play();

}

void draw(){

  

}

void mousePressed(){

  file2.play();

}

void keyPressed(){

  file3.play();

}




沒有留言:

張貼留言