2023年10月5日 星期四

LS._. Week04

processing最新版下載

  • 先去 https://processing.org/ 下載最新版的processing

UNO牌框

  • 作卡牌外框
  • 程式碼
void setup(){
  size(400,400);
}
int [][] card=new int[8][11];
void draw(){
  background(255);
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      rect(j*32,i*50,32,50);
    }
  }
}

UNO卡牌填色

  • 挑色,先用小畫家吸色,再用顏色選擇器打上RGB
  • 將下列的 #FFFFFF 輸入到程式碼內

  • 加上顏色程式碼
  • 程式碼
void setup(){
  size(400,400);
}
int [][] card=new int[8][11];
color [] C={#FF5555,#FFAA00,#55AA55,#5555FF};
void draw(){
  background(255);
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      fill(C[i%4]);
      rect(j*32,i*50,32,50);
    }
  }
}

UNO卡牌邊緣變好看、圓弧

  • 更改及加上程式碼,使邊緣變好看
  • 程式碼
void setup(){
  size(400,400);
}
int [][] card=new int[8][11];
color [] C={#FF5555,#FFAA00,#55AA55,#5555FF};
void draw(){
  background(255);
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      drawCard(j*32,i*50,C[i%4]);
    }
  }
}
void drawCard(int x,int y,color c ){
  stroke(128);
  fill(255);
  rect(x,y,32,50,7);
  fill(c);
  rect(x+3,y+3,32-5,50-5,4);
}

UNO卡片加上中間白色橢圓

  • 更改及加上程式碼,做出白色橢圓圈圈
  • 程式碼
void setup(){
  size(400,400);
}
int [][] card=new int[8][11];
color [] C={#FF5555,#FFAA00,#55AA55,#5555FF};
void draw(){
  background(255);
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      drawCard(j*32,i*50,C[i%4]);
    }
  }
}
void drawCard(int x,int y,color c ){
  stroke(128);
  fill(255);
  rect(x,y,32,50,7);
  fill(c);
  rect(x+3,y+3,32-5,50-5,4);
  
  pushMatrix();
  fill(255);
  translate(x+16,y+25);
  rotate(radians(45));
  ellipse(0,0,20,30);
  popMatrix();
}

UNO卡片加上數字

  • 更改及加上程式碼,印出數字
  • 更改及加上程式碼,讓數字不一樣
  • 程式碼
void setup(){
  size(400,400);
}
int [][] card=new int[8][11];
color [] C={#FF5555,#FFAA00,#55AA55,#5555FF};
void draw(){
  background(255);
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      drawCard(j*32,i*50,C[i%4],j);
    }
  }
}
void drawCard(int x,int y,color c,int n){
  stroke(128);
  fill(255);
  rect(x,y,32,50,7);
  fill(c);
  rect(x+3,y+3,32-5,50-5,4);
  
  pushMatrix();
  fill(255);
  translate(x+16,y+25);
  rotate(radians(45));
  ellipse(0,0,20,30);
  popMatrix();
  
  textAlign(CENTER,CENTER);
  textSize(9);
  text(""+n,x+7,y+6);
  textSize(20);
  fill(c);
  text(""+n,x+16,y+22);
}

UNO洗牌

  • 更改程式碼,執行後變怪怪的
  • 加上myShuffle()讓程式變正常
  • 加上程式碼,執行後可洗一張牌
  • 程式碼r1.r2改a.b(較容易看)
  • 程式碼
void setup(){
  size(400,400);
  myShuffle();
}
void myShuffle(){
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      cardN[i*11+j]=j;
      cardC[i*11+j]=C[i%4];
    }
  }
  int a=int(random(88)), b=int(random(88));
  int temp=cardN[a];
  cardN[a]=cardN[b];
  cardN[b]=temp;
  temp=cardC[a];
  cardC[a]=cardC[b];
  cardC[b]=temp;
}
int []cardN=new int[88];
int []cardC=new int[88];
color [] C={#FF5555,#FFAA00,#55AA55,#5555FF};
void draw(){
  background(255);
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      drawCard(j*32,i*50,cardC[i*11+j],cardN[i*11+j]);
    }
  }
}
void drawCard(int x,int y,color c,int n){
  stroke(128);
  fill(255);
  rect(x,y,32,50,7);
  fill(c);
  rect(x+3,y+3,32-5,50-5,4);
  
  pushMatrix();
  fill(255);
  translate(x+16,y+25);
  rotate(radians(45));
  ellipse(0,0,20,30);
  popMatrix();
  
  textAlign(CENTER,CENTER);
  textSize(9);
  text(""+n,x+7,y+6);
  textSize(20);
  fill(c);
  text(""+n,x+16,y+22);
}

UNO洗牌x10000

  • 加上迴圈讓洗牌動作執行10000次
  • 程式碼
void setup(){
  size(400,400);
  myShuffle();
}
void myShuffle(){
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      cardN[i*11+j]=j;
      cardC[i*11+j]=C[i%4];
    }
  }
  for(int i=0;i<10000;i++){
    int a=int(random(88)), b=int(random(88));
    int temp=cardN[a];
    cardN[a]=cardN[b];
    cardN[b]=temp;
    temp=cardC[a];
    cardC[a]=cardC[b];
    cardC[b]=temp;
  }
}
int []cardN=new int[88];
int []cardC=new int[88];
color [] C={#FF5555,#FFAA00,#55AA55,#5555FF};
void draw(){
  background(255);
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      drawCard(j*32,i*50,cardC[i*11+j],cardN[i*11+j]);
    }
  }
}
void drawCard(int x,int y,color c,int n){
  stroke(128);
  fill(255);
  rect(x,y,32,50,7);
  fill(c);
  rect(x+3,y+3,32-5,50-5,4);
  
  pushMatrix();
  fill(255);
  translate(x+16,y+25);
  rotate(radians(45));
  ellipse(0,0,20,30);
  popMatrix();
  
  textAlign(CENTER,CENTER);
  textSize(9);
  text(""+n,x+7,y+6);
  textSize(20);
  fill(c);
  text(""+n,x+16,y+22);
}

UNO互動

  • 加上大卡片
  • 將卡片放大 (*s)
  • 程式碼
void setup(){
  size(800,400);
  myShuffle();
}
void myShuffle(){
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      cardN[i*11+j]=j;
      cardC[i*11+j]=C[i%4];
    }
  }
  for(int i=0;i<10000;i++){
    int a=int(random(88)), b=int(random(88));
    int temp=cardN[a];
    cardN[a]=cardN[b];
    cardN[b]=temp;
    temp=cardC[a];
    cardC[a]=cardC[b];
    cardC[b]=temp;
  }
}
int []cardN=new int[88];
int []cardC=new int[88];
color [] C={#FF5555,#FFAA00,#55AA55,#5555FF};
void draw(){
  background(255);
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      drawCard(j*32,i*50,cardC[i*11+j],cardN[i*11+j],32);
    }
  }
  drawCard(450,50,cardC[7],cardN[77],128);
}
void drawCard(int x,int y,color c,int n,int w){
  float s=w/32.0;
  stroke(128);
  fill(255);
  rect(x,y,32*s,50*s,7*s);
  fill(c);
  rect(x+2.5*s,y+2.5*s,32*s-5*s,50*s-5*s,4*s);
  
  pushMatrix();
  fill(255);
  translate(x+16*s,y+25*s);
  rotate(radians(45));
  ellipse(0,0,20*s,30*s);
  popMatrix();
  
  textAlign(CENTER,CENTER);
  textSize(9*s);
  text(""+n,x+7*s,y+6*s);
  textSize(20*s);
  fill(c);
  text(""+n,x+16*s,y+22*s);
}

UNO poll get card

  • 加上程式碼,選出一張牌

  • 再加上程式碼,執行結果
  • 點擊滑鼠左鍵,可秀出一張牌,點一次秀一張
  • 點擊滑鼠右鍵,可秀出全部的牌
  • 牌出現的順序就是依照這個
  • 程式碼
void setup(){
  size(800,400);
  myShuffle();
}
void myShuffle(){
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      cardN[i*11+j]=j;
      cardC[i*11+j]=C[i%4];
    }
  }
  for(int i=0;i<10000;i++){
    int a=int(random(88)), b=int(random(88));
    int temp=cardN[a];
    cardN[a]=cardN[b];
    cardN[b]=temp;
    temp=cardC[a];
    cardC[a]=cardC[b];
    cardC[b]=temp;
  }
}
int []cardN=new int[88];
int []cardC=new int[88];
color [] C={#FF5555,#FFAA00,#55AA55,#5555FF};
void draw(){
  background(255);
  if(mousePressed && mouseButton==RIGHT){
    for(int i=0;i<8;i++){
      for(int j=0;j<11;j++){
        drawCard(j*32,i*50,cardC[i*11+j],cardN[i*11+j],32);
      }
    }
  }else drawCard(50,50,#000000,99,128);
  for(int i=0;i<myCard;i++){
    drawCard(430+40*i,50,cardC[i],cardN[i],128);
  }
}
int myCard=0;
void mousePressed(){
  if(mouseButton==LEFT)myCard++;
}
void drawCard(int x,int y,color c,int n,int w){
  float s=w/32.0;
  stroke(128);
  fill(255);
  rect(x,y,32*s,50*s,7*s);
  fill(c);
  rect(x+2.5*s,y+2.5*s,32*s-5*s,50*s-5*s,4*s);
  
  pushMatrix();
  fill(255);
  translate(x+16*s,y+25*s);
  rotate(radians(45));
  ellipse(0,0,20*s,30*s);
  popMatrix();
  
  textAlign(CENTER,CENTER);
  textSize(9*s);
  text(""+n,x+7*s,y+6*s);
  textSize(20*s);
  fill(c);
  text(""+n,x+16*s,y+22*s);
}

UNO將牌分給四人

  • 修改程式碼,將牌分成四份
  • 程式碼
void setup(){
  size(850,850);
  myShuffle();
}
void myShuffle(){
  for(int i=0;i<8;i++){
    for(int j=0;j<11;j++){
      cardN[i*11+j]=j;
      cardC[i*11+j]=C[i%4];
    }
  }
  for(int i=0;i<10000;i++){
    int a=int(random(88)), b=int(random(88));
    int temp=cardN[a];
    cardN[a]=cardN[b];
    cardN[b]=temp;
    temp=cardC[a];
    cardC[a]=cardC[b];
    cardC[b]=temp;
  }
}
int []cardN=new int[88];
int []cardC=new int[88];
color [] C={#FF5555,#FFAA00,#55AA55,#5555FF};
void draw(){
  background(255);
  if(mousePressed && mouseButton==RIGHT){
    for(int i=0;i<8;i++){
      for(int j=0;j<11;j++){
        drawCard(j*32,i*50,cardC[i*11+j],cardN[i*11+j],32);
      }
    }
  }else drawCard(50,50,#000000,99,128);
  for(int i=0;i<myCard;i++){
    if(i%4==0)drawCard(430+40*i/4,50,cardC[i],cardN[i],128);
    if(i%4==1)drawCard(630+40*i/4,350,cardC[i],cardN[i],128);
    if(i%4==2)drawCard(430+40*i/4,650,cardC[i],cardN[i],128);
    if(i%4==3)drawCard(230+40*i/4,350,cardC[i],cardN[i],128);
  }
}
int myCard=0;
void mousePressed(){
  if(mouseButton==LEFT)myCard++;
}
void drawCard(int x,int y,color c,int n,int w){
  float s=w/32.0;
  stroke(128);
  fill(255);
  rect(x,y,32*s,50*s,7*s);
  fill(c);
  rect(x+2.5*s,y+2.5*s,32*s-5*s,50*s-5*s,4*s);
  
  pushMatrix();
  fill(255);
  translate(x+16*s,y+25*s);
  rotate(radians(45));
  ellipse(0,0,20*s,30*s);
  popMatrix();
  
  textAlign(CENTER,CENTER);
  textSize(9*s);
  text(""+n,x+7*s,y+6*s);
  textSize(20*s);
  fill(c);
  text(""+n,x+16*s,y+22*s);
}

將程式碼上傳至GitHub



沒有留言:

張貼留言