2022年10月31日 星期一

( σ՞ਊ ՞)σ的互動技術筆記 week07

由於我的Gmail不能在學校開啟,我真的放棄圖檔了QQ

step1 - 1  

上上週的程式小葉老師的github裡面有,或是用自己的

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

---------------------------------------------------------------------------------------------------

step1 - 2  

自動洗牌程式
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}
};
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);
    }
}

---------------------------------------------------------------------------------------------------

step1 - 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}
};
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;
          moveJ = 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);
    }
}

---------------------------------------------------------------------------------------------------

step1 - 4  

修改程式用滑鼠移動象棋
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}
};
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);
    ///}
    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 - 5  

吃棋子
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}
};
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);
  ///}
  if(id>0)
  {
    fill(0);
    text( name[id-1], x, y-3);
  }
  else
  {
    fill(255,0,0);
    text( name2[-id-1], x, y-3);
  }
}
                                                                                      
---------------------------------------------------------------------------------------------------

step2 - 1   

下載音樂到資料夾裡,然後播放音樂
import processing.sound.*;
void setup()
{
  SoundFile file = new SoundFile(this, "音樂檔名");
}
void draw()
{
  
}
void mousePressed()
{
  SoundFile file2 = new SoundFile(this, "音樂檔名");
  file2.play();
}

---------------------------------------------------------------------------------------------------               
step 2 - 2   

可播放多首音樂,但要先下載起來
import processing.sound.*;
SoundFile file1, file2, file3, file4;
void setup()
{
  file1 = new SoundFile(this, "音樂檔名");
  file2 = new SoundFile(this, "音樂檔名");
  file3 = new SoundFile(this, "音樂檔名");
  file4 = new SoundFile(this, "音樂檔名");
  
  file1.play();
}
void draw()
{
  
}
void mousePressed()
{
  file2.play();
}
void keyPressed()
{
  file3.play();
}

沒有留言:

張貼留言