2022年9月12日 星期一

Da note

 1-1 先練習畫一張卡片

code:

size(500,500);

int W=25;

rect(100-W/2,100-W/2,150+W,250+W,20);//弧度

fill(#6AFFA4);//顏色

rect(100,100,150,250,20);



1-2 畫三張卡片,自己寫一個drawCard函式來使用

code:

void setup(){

  size(500,500);

}

int W=25;

void draw(){

  drawCard(100,100);

  drawCard(130,130);

  drawCard(160,160);

}

void drawCard(int x,int y){

fill(255);

rect(x-W/2,y-W/2, 150+W,250+W ,20);

fill(#FF00F2);

rect(x,y,150,250,20);

}

2-1 修改剛剛的函式,改成drawPokerCard,並給卡片編號
code:
void setup(){
  size(500,500);
}

int W=25;

void draw(){
  drawPokerCard(100,100,"S4");
  drawPokerCard(130,130,"H3");
  drawPokerCard(160,160,"D5");
  drawPokerCard(190,250,"CJ");
}

void drawPokerCard(int x,int y,String face){
fill(255);
rect(x-W/2,y-W/2, 150+W,250+W ,20);
fill(#FF00F2);
rect(x,y,150,250,20);
fill(0);
textSize(40);
text(face,x,y+40);
}
2-2 增加Font用來宣告標楷體 並把編號改成花色
code:
void setup(){
  size(500,500);
  PFont font = createFont("標楷體",40);
  textFont(font);
}
int W=25;
void draw(){
  drawPokerCard(100,100,"黑桃4");
  drawPokerCard(130,130,"紅心3");
  drawPokerCard(160,160,"方塊5");
  drawPokerCard(190,250,"梅花J");
}

void drawPokerCard(int x,int y,String face){
fill(255);
rect(x-W/2,y-W/2, 150+W,250+W ,20);
fill(#FF00F2);
rect(x,y,150,250,20);
fill(0);
textSize(40);
text(face,x,y+40);
}
2-3 修改不同花色的顏色
code:
void setup(){
  size(500,500);
  PFont font = createFont("標楷體",40);
  textFont(font);
}
int W=25;
void draw(){
  drawPokerCard(100,100,"黑桃4");
  drawPokerCard(130,130,"紅心3");
  drawPokerCard(160,160,"方塊5");
  drawPokerCard(190,250,"梅花J");
}

void drawPokerCard(int x,int y,String face){
fill(255);
rect(x-W/2,y-W/2, 150+W,250+W ,20);
fill(#FF00F2);
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);
}
3-1 寫一個亂數(random)使卡片隨機產生
code:
void setup(){
  size(500,500);
  PFont font = createFont("標楷體",40);
  textFont(font);
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);
}
String face1,face2,face3,face4;
void draw(){
  drawPokerCard(100,100,face1);
  drawPokerCard(130,130,face2);
  drawPokerCard(160,160,face3);
  drawPokerCard(190,250,face4);
}

void drawPokerCard(int x,int y,String face){
int W=25;
fill(255);
rect(x-W/2,y-W/2, 150+W,250+W ,20);
fill(#FF00F2);
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);
}







沒有留言:

張貼留言