2022年10月31日 星期一

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

為什麼我換手機就可以登GOOGLE了?????????


step1-1

 先下載音樂,並播放


import processing.sound.*;

SoundFile sound1, sound2, sound3;


void setup() {

  size(400, 300);

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

  sound1.play();

}

void draw() {

}

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

step1-2

把音樂下載好,放在資料夾裡面,點滑鼠可以切換音樂

import processing.sound.*;

SoundFile sound1, sound2, sound3;


void setup() {

  size(400, 300);

  textSize(50);

  fill(255,0,0);

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

  sound2 = new SoundFile(this, "Monkey 1.mp3");

  sound1.play();

}

int stage=1;

void draw() {

  background(255);

  if(stage==1){//舞台1

    text("stage 1", 100, 100);

  }else if(stage==2){//舞台2

    text("stage 2", 100, 100);

  }

}

void mousePressed(){

  if(stage==1){

    stage=2;

    sound1.stop();

    sound2.play();

  }else if(stage==2){

    stage=2;

    sound1.stop();

    sound2.play();

  }

}


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

step1-3

簡化剛剛的程式

void setup() {
  size(400, 300);
}
int stage=1;
void draw() {
  background(255,255,0);
  fill(255,0,0);
  textSize(80);
  if(stage==1){//舞台1
    text("stage 1", 100, 100);
  }else if(stage==2){//舞台2
    text("stage 2", 100, 100);
  }
}
void mousePressed(){
  if(stage==1) stage=2;
  else if(stage==2) stage=1;
}

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

step2-1
物體沒有重力會漂走

void setup(){
  size(400,300);
}
float fruitX=200, fruitY=300;//水果的位置
float fruitVX=2, fruitVY=-13;//水果的速度
boolean flying=true;
void draw(){
  background(255,255,0);
  
  ellipse(fruitX, fruitY, 50,50);
  if(flying){//如果在飛,水果的位置會改變
    fruitX += fruitVX;
    fruitY += fruitVY;
    fruitVY += 0.98/3;//重力加速度
  }
}
void keyPressed(){
  flying=false;
}


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

step2-2
給物體重力

void setup(){
  size(400,300);
}
float fruitX=200, fruitY=300;//水果的位置
float fruitVX=2, fruitVY=-13;//水果的速度
boolean flying=true;
void draw(){
  background(255,255,0);
  
  ellipse(fruitX, fruitY, 50,50);
  if(flying){//如果在飛,水果的位置會改變
    fruitX += fruitVX;
    fruitY += fruitVY;
    fruitVY += 0.98/3;//重力加速度
  }
}
void keyPressed(){
  flying=false;
  fruitReset();///重新準備另一發水果
}
void fruitReset(){
  fruitX=random(100,300);
  fruitY=300;//固定高度
  fruitVX=random(-2,+2);
  fruitVY=-13;
  flying=true;
}

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

step2-3

修改一下


//目標: class物件: 每個水果都可以用物件做出來(值、函式)
class Fruit{
  float x,y,vx,vy;
  boolean flying;
  PApplet sketch;//為了讓random可以用,修改一下
  Fruit(PApplet _sketch){
    sketch = _sketch;
    reset();
  }
  void reset(){
    x = sketch.random(100.0, 300.0);
    y = 300;
    vx = sketch.random(-2,+2);
    vy = -13;
    flying = true;
  }
  void update(){
    x += vx;
    y += vy;
    vy += 0.98/3;//重力加速度
  }
}
Fruit fruit;
void setup(){
  size(400,300);
  fruit = new Fruit(this);//為了讓random可以用,修改一下
}
void draw(){
  background(255,255,0);
  ellipse(fruit.x, fruit.y, 50, 50);
  fruit.update();
}
void keyPressed(){
  fruit.reset();
}

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

step2-4-1
水果的程式

//目標: class物件: 每個水果都可以用物件做出來(值、函式)
String line="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
class Fruit {
  float x, y, vx, vy;
  boolean flying;
  char c;
  PApplet sketch;
  Fruit(PApplet _sketch) {
    sketch = _sketch;
    reset();
  }
  void reset() {
    x = sketch.random(100.0, 300.0);
    y = 300;
    vx = sketch.random(-2, +2);
    vy = -13;
    flying = true;
    int i=int(random(26));
    c = line.charAt(i);
  }
  void update() {
    x += vx;
    y += vy;
    vy += 0.98/3;
  }
}


step2-4-2
主程式

Fruit [] fruits;
void setup(){
  size(400,300);
  fruits = new Fruit[3];
  for(int i=0; i<3; i++){
    fruits[i] = new Fruit(this);//修改一下
  }
}
void draw(){
  background(255,255,0);
  for(int i=0; i<3; i++){
    fill(255); ellipse(fruits[i].x, fruits[i].y, 50, 50);
    textSize(30);
    textAlign(CENTER,CENTER);
    fill(0); text(fruits[i].c, fruits[i].x, fruits[i].y);
    fruits[i].update();
  }
}
void keyPressed(){
  for(int i=0; i<3; i++){
    if( keyCode == fruits[i].c){
      fruits[i].reset();
    }
  }
}

( σ՞ਊ ՞)σ的互動技術筆記 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();
}

2022年10月30日 星期日

大拇指的啦

 暗棋亂數洗牌(延續week05)

mousePressed()內上迴圈改成4 下迴圈改成8 不然會出錯

在setup()裡 ,直、橫隨機取亂數做洗牌,並用temp交換彼此

void setup(){

  size(500,300);

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

  textFont(font);

  textAlign(CENTER, CENTER);

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

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

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

     int temp=board[i1][j1];

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

     board[i2][j2]=temp;

  }

}

設定變數來判斷棋子是否移動

int moveI=-1 , moveJ=-1;

boolean moving = false;///不是移動中

在mousePressed()裡再新增else 判斷移動棋子的位置

else{

           moveI=i;///我們想移動ㄉ旗子i座標

           moveJ=j;///

           moveID= board[i][j];

           moving=true;///現在移動中

        }


如果移動棋子 就畫出棋子的樣子

drawChess ( 50+25+moveJ*50 , 50+25+moveI*50,9)///放到外面第9個

點選要移動的棋子時,點選的棋子要變色

if (id==9){

    fill(0 ,255 ,0 ,128);///第4個值為透明度

ellipse(x,y,40,40);

}


修正

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;///

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

    }

}

在drawChess()裡新增if (id==0) return;

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

    }

}

最終成品!!

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, 400);
  PFont font = createFont("標楷體", 30);//設定文字
  textFont(font);
  textAlign(CENTER, CENTER);
  for(int k=0;k<1000;k++){
     shuffle_one();
  }
}
void shuffle_one(){
  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(#FFDAA2);
  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); //line(x1,y1,x2,y2);
  }
  for (int i=0; i<4; i++) {///印出象棋
    for (int j=0; j<8; j++) {
      if(show[i][j]==0){
         fill(#289D48);
         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,255, 128);//半透明藍色
    ellipse(50+25+moveJ*50, 50+25+moveI*50, 40, 40);//原來的位置
    
    drawChess(mouseX,mouseY,moveID); //手上移動中的棋子
  }
}
int moveI = -1 ,moveJ = -1, moveID=0;
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;///正在移動
         }
      }
    }
  }
}
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>0) {
    fill(0);
    text(name[id-1], x, y-3);
  } else {
    fill(255, 0, 0);
    text(name2[-id-1], x, y-3);
  }
}
放音樂
開新專案,先存檔->程式素描本->使用程式函式庫->Manage libraries->安裝sound函式庫


Ctrl+k可以打開程式目錄 裡面可以看丟過的檔案
讀入音樂檔 
SoundFile File = new SoundFile(this , "檔名.mp3");
撥放音樂
File.play()

大拇指的啦

象棋棋盤

用兩個迴圈來畫棋盤 一個負責x 一個負責y

void setup(){

   size(500,700); 

}

void draw(){

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

    line(x,50,x,500);

  }

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

      line(50,y,450,y);

  }

}

劃分楚河漢界

void draw(){

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

      line(x,50,x,250);///x的g;上半段

      line(x,300,x,500);///x的下半段

   }

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

      line(50,y,450,y); 

   }

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

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

         text(board[i][j],50+j*50,50+i*50);   

      }

   }

}

上半段放棋子

int [][]board={

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

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

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

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

};


設定陣列
name[] = {"將","士","象","馬","車","包","卒"};


設定字型

   size(500,700); 
   PFont font = createFont("標楷體",30);
   textFont(font);
   textAlign(CENTER,CENTER);


設定id為board陣列,當id=0時需跳過他

text內的name[ id-1 ],確保兩個不一樣才不會出錯

for (int i=0;i<4;i++){
      for (int j=0;j<9;j++){
        int id=board[i][j];
        if (id==0) continue;
        text(name[id-1],50+j*50,50+i*50);   
      }
   }

象棋棋子
用ellipse畫圓形 但因為字體不會在格子正中間 所以要偏移(-3)

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

因為有上下兩層 所以下層編號為負

int [][]board={
  {4, 5, 3, 2, 1, 2, 3, 5, 4},
  {0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 6, 0, 0, 0, 0, 0, 6, 0},
  {7, 0, 7, 0, 7, 0, 7, 0, 7},
  {0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0},
  {-7, 0,-7, 0,-7, 0,-7, 0,-7},
  {0,-6, 0, 0, 0, 0, 0, -6, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0},
  {-4,-5,-3,-2,-1,-2,-3,-5,-4},

};

if else 來判斷id>0 黑字、id <0 紅字

for (int i=0; i<10; i++){
    for (int j=0; j<9; j++){
      int id=board[i][j];
      if (id==0) continue;
      if (id>0) {
        fill(255);
        ellipse( 50+j*50, 50+i*50, 40, 40);
        fill(0);///黑色
        text(name[id-1], 50+j*50, 50+i*50-3);
      } else if (id<0) {
        fill(255);
        ellipse( 50+j*50, 50+i*50, 40, 40);
        fill(255, 0, 0);///紅色
        text(name2[-id-1], 50+j*50, 50+i*50-3);
      }
    }
  }

滑鼠控制棋子
設定void mousePresssed()

用雙迴圈+dist來判定滑鼠位置
void mousePressed(){
   for (int i=0;i<10;i++){
      for (int j=0;j<0;j++){
         if ( dist(mouseX,mouseY,50+j*50,50+i*50)<20){
            board[i][j]=1; 
         }
      }
   }
}
設定滑鼠放開void mouseReleased()
void mouseReleased(){
   int i=(mouseY+25-50)/50;
   int j=(mouseX+25-50)/50;
   board[i][j]=handchess;
   handchess=0;
}

修改mousePressed() 
void mousePressed(){
   for (int i=0;i<10;i++){
      for (int j=0;j<9;j++){
         if ( dist(mouseX,mouseY,50+j*50,50+i*50)<20){
           handchess=board[i][j];///拿棋子 
           board[i][j]=0; ///清空棋子
         }
      }
   }
}

完整的棋盤
void setup() {
  size(500, 700);
  PFont font = createFont("標楷體", 30);
  textFont(font);
  textAlign(CENTER, CENTER);
}
int [][]board={
  {4, 5, 3, 2, 1, 2, 3, 5, 4},
  {0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 6, 0, 0, 0, 0, 0, 6, 0},
  {7, 0, 7, 0, 7, 0, 7, 0, 7},
  {0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0},
  {-7, 0, -7, 0, -7, 0, -7, 0, -7},
  {0, -6, 0, 0, 0, 0, 0, -6, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0},
  {-4, -5, -3, -2, -1, -2, -3, -5, -4},
};
String []name={"將", "士", "象", "車", "馬", "包", "卒"};
String []name2={"帥", "仕", "相","俥" ,"傌", "炮", "兵"};
void draw() {
  background(#FFDAA2);
  for (int x=50; x<=450; x+=50) {
    line( x, 50, x, 250);
    line( x, 300, x, 500);
  }
  for (int y=50; y<=500; y+=50) {
    line( 50, y, 450, y); //line(x1,y1,x2,y2);
  }
  for (int i=0; i<10; i++) {
    for (int j=0; j<9; j++) {
      int id= board[i][j];。
      if (id==0) continue;
      if (id>0) {
        fill(255);
        ellipse(50+j*50, 50+i*50, 40, 40);
        fill(0);
        text( name[id-1], 50+j*50, 50+i*50-3);
      }
      if (id<0) {
        fill(255);
        ellipse(50+j*50, 50+i*50, 40, 40);
        fill(255, 0, 0);
        text( name2[-id-1], 50+j*50, 50+i*50-3);
      }
      if(hand!=0) ellipse(mouseX,mouseY,40,40);
    }
  }
  
}
int hand=0;
void mousePressed(){
  for (int i=0; i<10; i++) {
    for (int j=0; j<9; j++) {
       if(dist(mouseX,mouseY,50+j*50, 50+i*50)<20){
           hand=board[i][j];
           board[i][j]=0; 
       }
    }
  }
}
void mouseReleased(){
   int i= (mouseY+25-50)/50;
   int j= (mouseX+25-50)/50;
   board[i][j]=hand;
   hand=0;
}


暗棋 ( 修改自原棋盤 )
修改成4*8大小 並修改值
相同void setup() 
因為要將棋子放入格子中間 所以要在void mousePressed()內將值增加25
設定初始全為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}
};
設定if else 點即時如果是0就蓋牌 如果不是舊印象棋
f (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);
      }
完整暗棋
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);
      }
    }
  }
}
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);
    }
}
void mousePressed(){
   for (int i=0;i<10;i++){
      for (int j=0;j<9;j++){
         if ( dist(mouseX,mouseY,50+25+j*50,50+25+i*50)<20){
           show[i][j]=1;
         }
      }
   }
}