2022年9月22日 星期四

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

  step1 - 1 

修改上次的程式碼,點擊滑鼠可以改變卡牌的花色


void setup()
{
  size(500,500);
  PFont font = createFont("標楷體", 40);
  textFont(font);
  myShuffle();
}
void myShuffle()
{
  String [] flower = {"黑桃", "紅心", "方塊", "梅花"};
  face1 = flower[int(random(4))] + int(random(13)+1);
  face2 = flower[int(random(4))] + int(random(13)+1);
  face3 = flower[int(random(4))] + int(random(13)+1);
  face4 = flower[int(random(4))] + int(random(13)+1);
}
void mousePressed()
{
  myShuffle();
}
String face1, face2, face3, face4;
void draw()
{
  drawPokerCard(100,100, face1);
  drawPokerCard(130,150, face2);
  drawPokerCard(160,200, face3);
  drawPokerCard(190,250, face4);
}
void drawPokerCard(int x, int y, String face)
{
  int w=20;
  fill(255);
  rect(x-w/2,y-w/2, 150+w, 250+w, 20);
  fill(#FF9090);
  rect(x,y, 150, 250, 20);
  ///fill(0);
  if(face.indexOf("黑桃") == -1 && face.indexOf("梅花") == -1) fill(#FF0000);
  else fill(0);
  textSize(40);
  text(face, x, y+40);
}




step1 - 2  
開一個新程式,顯示所有的牌

void setup()
{
  size(700,700);
}
void draw()
{
  background(#FFFFF2);
  for(int i=0 ; i<52 ; i++)
  {
    int x = (i%10)*60;
    int y = int(i/10)*120;
    rect(x, y, 60, 120);
  }
}


step1 - 3 
修改上一個程式,加上文字
void setup()
{
  size(700,700);
  PFont font = createFont("標楷體", 12);
  textFont(font);
}
String [] faces = {
  "黑桃A", "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K", 
  "黑桃A", "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K",
  "黑桃A", "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K",
  "黑桃A", "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K"
};
void draw()
{
  background(#FFFFF2);
  for(int i=0 ; i<52 ; i++)
  {
    int x = (i%10)*60;
    int y = int(i/10)*120;
    fill(255);
    rect(x, y, 60, 120);
    fill(0);
    text(faces[i], x+25, y+80);
  }
}






step1 - 4  
改變文字和顏色
void setup()
{
  size(700,700);
  PFont font = createFont("標楷體", 16);
  textFont(font);
}
String [] faces = {
  "黑桃A", "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K", 
  "紅心A", "紅心2", "紅心3", "紅心4", "紅心5", "紅心6", "紅心7", "紅心8", "紅心9", "紅心10", "紅心J", "紅心Q", "紅心K",
  "方塊A", "方塊2", "方塊3", "方塊4", "方塊5", "方塊6", "方塊7", "方塊8", "方塊9", "方塊10", "方塊J", "方塊Q", "方塊K",
  "梅花A", "梅花2", "梅花3", "梅花4", "梅花5", "梅花6", "梅花7", "梅花8", "梅花9", "梅花10", "梅花J", "梅花Q", "梅花K"
};
void draw()
{
  background(#FFFFF2);
  for(int i=0 ; i<52 ; i++)
  {
    int x = (i%10)*60;
    int y = int(i/10)*120;
    fill(255);
    rect(x, y, 60, 120);
    if(faces[i].indexOf("紅心") == -1 && faces[i].indexOf("方塊") == -1) fill(0);
    else fill(255,0,0);
    text(faces[i], x+10, y+60);
  }
}

step1 - 5  
點擊滑鼠可以將牌互換
void setup()
{
  size(700,700);
  PFont font = createFont("標楷體", 16);
  textFont(font);
}
String [] faces = {
  "黑桃A", "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K", 
  "紅心A", "紅心2", "紅心3", "紅心4", "紅心5", "紅心6", "紅心7", "紅心8", "紅心9", "紅心10", "紅心J", "紅心Q", "紅心K",
  "方塊A", "方塊2", "方塊3", "方塊4", "方塊5", "方塊6", "方塊7", "方塊8", "方塊9", "方塊10", "方塊J", "方塊Q", "方塊K",
  "梅花A", "梅花2", "梅花3", "梅花4", "梅花5", "梅花6", "梅花7", "梅花8", "梅花9", "梅花10", "梅花J", "梅花Q", "梅花K"
};
void draw()
{
  background(#FFFFF2);
  for(int i=0 ; i<52 ; i++)
  {
    int x = (i%10)*60;
    int y = int(i/10)*120;
    fill(255);
    rect(x, y, 60, 120);
    if(faces[i].indexOf("紅心") == -1 && faces[i].indexOf("方塊") == -1) fill(0);
    else fill(255,0,0);
    text(faces[i], x+10, y+60);
  }
}
void mousePressed()
{
  int a = int(random(52));
  int b = int(random(52));
  String temp = faces[a];
  faces[a] = faces[b];
  faces[b] = temp;
}

step1 - 6  
將第一個程式與上一個程式做合併(方法一)
void setup()
{
  size(700,700);
  PFont font = createFont("標楷體", 14);
  textFont(font);
  myShuffle();
}
String [] faces = {
  "黑桃A", "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K", 
  "紅心A", "紅心2", "紅心3", "紅心4", "紅心5", "紅心6", "紅心7", "紅心8", "紅心9", "紅心10", "紅心J", "紅心Q", "紅心K",
  "方塊A", "方塊2", "方塊3", "方塊4", "方塊5", "方塊6", "方塊7", "方塊8", "方塊9", "方塊10", "方塊J", "方塊Q", "方塊K",
  "梅花A", "梅花2", "梅花3", "梅花4", "梅花5", "梅花6", "梅花7", "梅花8", "梅花9", "梅花10", "梅花J", "梅花Q", "梅花K"
};
void myShuffle()
{
  for(int k=0 ; k<10000 ; k++)
  {
    int a = int(random(52));
    int b = int(random(52));
    String temp = faces[a];
    faces[a] = faces[b];
    faces[b] = temp;
  }
  face1 = faces[0];
  face2 = faces[1];
  face3 = faces[2];
  face4 = faces[3];
}
void mousePressed()
{
  myShuffle();
}
String face1, face2, face3, face4;
void draw()
{
  background(#FFFFF2);
  for(int i=0 ; i<52 ; i++)
  {
    int x = (i%10)*60;
    int y = int(i/10)*120;
    fill(255);
    rect(x, y, 60, 120);
    if(faces[i].indexOf("紅心") == -1 && faces[i].indexOf("方塊") == -1) fill(0);
    else fill(255,0,0);
    text(faces[i], x+10, y+60);
  }
}
    

step1 - 6  
(方法二)
void setup()
{
  size(700,700);
  PFont font = createFont("標楷體", 14);
  textFont(font);
  myShuffle();
}
String [] faces = {
  "黑桃A", "黑桃2", "黑桃3", "黑桃4", "黑桃5", "黑桃6", "黑桃7", "黑桃8", "黑桃9", "黑桃10", "黑桃J", "黑桃Q", "黑桃K", 
  "紅心A", "紅心2", "紅心3", "紅心4", "紅心5", "紅心6", "紅心7", "紅心8", "紅心9", "紅心10", "紅心J", "紅心Q", "紅心K",
  "方塊A", "方塊2", "方塊3", "方塊4", "方塊5", "方塊6", "方塊7", "方塊8", "方塊9", "方塊10", "方塊J", "方塊Q", "方塊K",
  "梅花A", "梅花2", "梅花3", "梅花4", "梅花5", "梅花6", "梅花7", "梅花8", "梅花9", "梅花10", "梅花J", "梅花Q", "梅花K"
};
void myShuffle()
{
  for(int k=0 ; k<10000 ; k++)
  {
    int a = int(random(52));
    int b = int(random(52));
    String temp = faces[a];
    faces[a] = faces[b];
    faces[b] = temp;
  }
  face1 = faces[0];
  face2 = faces[1];
  face3 = faces[2];
  face4 = faces[3];
}
void mousePressed()
{
  myShuffle();
}
String face1, face2, face3, face4;
void draw()
{
  drawPokerCard(100,100, face1);
  drawPokerCard(130,150, face2);
  drawPokerCard(160,200, face3);
  drawPokerCard(190,250, face4);
}
void drawPokerCard(int x, int y, String face)
{
  int w=20;
  fill(255);
  rect(x-w/2,y-w/2, 150+w, 250+w, 20);
  fill(#FF9090);
  rect(x,y, 150, 250, 20);
  ///fill(0);
  if(face.indexOf("黑桃") == -1 && face.indexOf("梅花") == -1) fill(#FF0000);
  else fill(0);
  textSize(40);
  text(face, x, y+40);
}

沒有留言:

張貼留言