1. 畫卡片
(1) szie(500,500); //設置畫面
(2) rect(100,100,150,250,20); //設置弧度
(3) 完成
2. 畫卡片 2.0 _ 變數
(1) 增加 rect
rect(100 , 100 , 150 , 250 , 20);
(2) 填滿顏色 fill
fill(#EECCCC);
(3) 增加一個變數 W
int W=25;
(4) 修改 rect
rect(x-W/2 , y-W/2 , 150+W , 250+W , 20);
3. 畫卡片 3.0 _ 函式重複做
(1) 增加 setup
void setup(){
size(500,500);
}
int W=25;
(2) 使用函式 draw
void draw(){
drawCard(100,100); //使用函式
drawCard(150,150); //使用函式
drawCard(200,200); //使用函式
}
(3) 使用函式 drawCard
void drawCard(int x, int y){
fill(255);
rect(x-W/2 , y-W/2 , 150+W , 250+W , 20);
fill(#EECCCC);
rect(x,y,150,250,20); //弧度
}
(4) 完成
4. 畫撲克牌
(1) 開新視窗
Ctrl + n
(2) 修改函式 draw
void draw(){
drawPokerCard(100,100 ,"S4"); //使用函式
drawPokerCard(130,150 ,"A8"); //使用函式
drawPokerCard(160,200 ,"R5"); //使用函式
drawPokerCard(190,250 ,"L3"); //使用函式
}
(3) 修改函式 drawPokerCard
void drawPokerCard(int x, int y, String face){
fill(255);
rect(x-W/2,y-W/2,150+W,250+W,20);
fill(#EECCCC);
rect(x,y,150,250,20); //弧度
fill(0);
textSize(40);
text(face , x , y+40);
}
(1) 中文無法顯示!
(2) 增加程式碼
void setup(){
size(600,600);
PFont font = createFont("標楷體" , 20);
textFont(font);
}
(3) 完成
6. 畫撲克牌 3.0 _ 改字體顏色
(1) indexOf(不是 0 喔)
void drawPokerCard(int x, int y, String face){
fill(255);
rect(x-W/2,y-W/2,150+W,250+W,20);
fill(#EECCCC);
rect(x,y,150,250,20); //弧度
if(face.indexOf("黑桃") == -1 && face.indexOf("梅花") == -1 ) fill(#FF0000);
else fill(0);
textSize(20);
text(face , x+10 , y+25);
}
(2) 完成
7. 畫撲克牌 4.0 _ 洗牌
(1) 加陣列 + 亂數
void setup(){
size(600,600);
PFont font = createFont("標楷體" , 20);
textFont(font);
String [] flower = {"黑桃","紅心","方塊","梅花"}; //陣列
0 1 2 3 //0 - 3 有 4 個
face1 = flower[int(random(4))] + int(random(13)+1); //取整數0...12 所以再加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);
} //洗牌的英文 Shuffle
(2) 改牌面
String face1, face2, face3, face4;
void draw(){
drawPokerCard(100,100 , face1 ); //使用函式
drawPokerCard(130,150 , face2 ); //使用函式
drawPokerCard(160,200 , face3 ); //使用函式
drawPokerCard(190,250 , face4 ); //使用函式
} //牌面
(3) 完成
8. 畫撲克牌 4.0 _ 滑鼠按一下洗牌
(1) 加函式 mousePressed
void mousePressed(){
String [] flower = {"黑桃","紅心","方塊","梅花"}; //陣列
face1 = flower[int(random(4))] + int(random(13)+1); //取整數0...12 所以再加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);
}
(2) 完成
github
-------------------------------------------------------------------------








沒有留言:
張貼留言