2023年10月5日 星期四

jason1230 week04

 1-1

要製作像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);
    }
  }
}

1-2

新增4種顏色,輸入在陣列裡,讓顏色放在格子裡


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

1-3

讓邊線看起來有弧度

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);//gray stroke
  fill(255);//white
  rect(x,y,32,50,7);
  noStroke();
  fill(c);
  rect(x+3,y+3,32-5,50-5,4);
}

1-4

為了要讓卡片中間的橢圓出現,用PushMatrix、popMatrix、translate、rotate

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);//gray stroke
  fill(255);//white
  rect(x,y,32,50,7);
  noStroke();
  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();
}

2-1

新增卡片上的數字,用textAlign用來把字對準中心

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);//gray stroke
  fill(255);//white
  rect(x,y,32,50,7);
  noStroke();
  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("1",x+7,y+6);
  
  textSize(20);
  fill(c);
  text("1",x+16,y+22);
}

2-1-2

引號禮數字拿掉在外面寫+n,就可以照順序出現數字

2-2

讓卡牌新增洗牌功能shuffle,先宣告cardC,cardN

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 [][] card= new int[8][11];
int [] cardN=new int[88];//card number
int [] cardC=new int[88];//card color
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);
      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);//gray stroke
  fill(255);//white
  rect(x,y,32,50,7);
  noStroke();
  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);
}

2-3

增加隨機洗牌功能

  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;

2-4

使用迴圈,洗牌洗10000次,看起來更有趣

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

3-1

隨機出一張卡片然後變大運用scale

      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);//gray stroke
  fill(255);//white
  rect(x,y,32*s,50*s,7*s);
  noStroke();
  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);
}

3-2

用滑鼠點擊,牌會隨機發出,用myCard、mousePressed、mouseButton

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];//card number
int [] cardC=new int[88];//card color
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,C[i%4],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);//gray stroke
  fill(255);//white
  rect(x,y,32*s,50*s,7*s);
  noStroke();
  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);
}

3-3





沒有留言:

張貼留言