2023年10月5日 星期四

Jia的互動技術日誌 Week04

🃟 🃟 🃟

今天的第一個程式是想寫一個 uno 的卡牌遊戲

冷知識🧊: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);
    }
  }
}
先把基礎的形狀畫出來

給卡片上色
用 小畫家,可以吸取圖片顏色 得到RGB值
💾 程式碼存檔區
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);
    }
  }
}
drawCard 函式
不用每次都畫,用函式裝他的內容
再來要讓卡片更完美!
rect(x座標,y座標,長,寬,圓角)
💾 程式碼存檔區
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);
  noStroke(); //不要筆觸(不要描邊)
  fill(c);
  rect(x+3,y+3,32-5,50-5,4);
}
所有的數值都是一次次調出來的
現在很漂亮啦

把中間白色橢圓加上去
但是橢圓式斜的!?
運用上學期圖學教的 push pop!
ellipse(0,0,20,30);多一個值從圓變成橢圓
💾 程式碼存檔區
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);
  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();
}
更好看啦
我們要印卡片上的數字了
💾 程式碼存檔區
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);
  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();
  
  textSize(9);
  textAlign(CENTER,CENTER);
  text("1",x+7,y+8);
  
  textSize(20);
  fill(c);
  text("1",x+16,y+25);
  
}
textSize(字體大小);
textAlign(x對齊位置,y對齊位置);
text("顯示數字",位置,位置);
數字沒有都是 1 
怎麼讓數字有變化呢
💾 程式碼存檔區
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);
  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();
  
  textSize(9);
  textAlign(CENTER,CENTER);
  text(""+n,x+7,y+7);
  
  textSize(20);
  fill(c);
  text(""+n,x+16,y+25);
  
}

要來洗牌囉
💾 程式碼存檔區
void setup(){
  size(400,400);
}
//int [][] card = new int[8][11];
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,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); // 灰色筆觸
  fill(255); //填充白色
  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();
  
  textSize(9);
  textAlign(CENTER,CENTER);
  text(""+n,x+7,y+7);
  
  textSize(20);
  fill(c);
  text(""+n,x+16,y+25);
  
}
不過顏色不正確

洗牌程式碼來囉
把顏色和數字都塞好
💾 程式碼存檔區
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]; // 卡片的數字
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,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); // 灰色筆觸
  fill(255); //填充白色
  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();
  
  textSize(9);
  textAlign(CENTER,CENTER);
  text(""+n,x+7,y+9);
  
  textSize(20);
  fill(c);
  text(""+n,x+16,y+25);
  
}
丟進去啦

簡單的洗牌
兩張對調,顏色數字都交換
💾 程式碼存檔區
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 [][] card = new int[8][11];
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,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); // 灰色筆觸
  fill(255); //填充白色
  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();
  
  textSize(9);
  textAlign(CENTER,CENTER);
  text(""+n,x+7,y+9);
  
  textSize(20);
  fill(c);
  text(""+n,x+16,y+25);
  
}

兩張對調太遜了
想要洗一萬次!
用迴圈包起來就好啦
💾 程式碼存檔區
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 [][] card = new int[8][11];
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,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); // 灰色筆觸
  fill(255); //填充白色
  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();
  
  textSize(9);
  textAlign(CENTER,CENTER);
  text(""+n,x+7,y+9);
  
  textSize(20);
  fill(c);
  text(""+n,x+16,y+25);
  
}
洗好啦

想要加互動囉
💾 程式碼存檔區
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 [][] card = new int[8][11];
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,c[i%4],j);
      drawCard(j*32,i*50,cardC[i*11+j],cardN[i*11+j],32);
    }
  }
  drawCard(500,100,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);
  noStroke(); //不要筆觸(不要描邊)
  fill(c);
  rect(x+2.6*s,y+2.6*s,32*s-5*s,50*s-5*s,5*s);
  
  pushMatrix();
  fill(255);
  translate(x+16*s,y+25*s);
  rotate(radians(45));
  ellipse(0,0,20*s,30*s);
  popMatrix();
  
  textSize(9*s);
  textAlign(CENTER,CENTER);
  text(""+n,x+7*s,y+9*s);
  
  textSize(20*s);
  fill(c);
  text(""+n,x+16*s,y+25*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 [][] card = new int[8][11];
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);
  noStroke(); //不要筆觸(不要描邊)
  fill(c);
  rect(x+2.6*s,y+2.6*s,32*s-5*s,50*s-5*s,5*s);
  
  pushMatrix();
  fill(255);
  translate(x+16*s,y+25*s);
  rotate(radians(45));
  ellipse(0,0,20*s,30*s);
  popMatrix();
  
  textSize(9*s);
  textAlign(CENTER,CENTER);
  text(""+n,x+7*s,y+9*s);
  
  textSize(20*s);
  fill(c);
  text(""+n,x+16*s,y+25*s);
  
}
如果我按右鍵可以偷看牌
按左鍵抽一張牌
跟其他人一起玩!
💾 程式碼存檔區
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 [][] card = new int[8][11];
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,0,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-10  +40*i/4,350,cardC[i],cardN[i],128);
    if(i%4==2)drawCard(430-20  +40*i/4,650,cardC[i],cardN[i],128);
    if(i%4==3)drawCard(230-30  +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);
  noStroke(); //不要筆觸(不要描邊)
  fill(c);
  rect(x+2.6*s,y+2.6*s,32*s-5*s,50*s-5*s,5*s);
  
  pushMatrix();
  fill(255);
  translate(x+16*s,y+25*s);
  rotate(radians(45));
  ellipse(0,0,20*s,30*s);
  popMatrix();
  
  textSize(9*s);
  textAlign(CENTER,CENTER);
  text(""+n,x+7*s,y+9*s);
  
  textSize(20*s);
  fill(c);
  text(""+n,x+16*s,y+25*s);
  
}




沒有留言:

張貼留言