今日課堂內容:
做一個小遊戲:
1-1 做一張卡片
size(500,500);
int w=10
rect(100-w/2,100-w/2, 150+w, 250+w, 20);
fill(#FF9090);
rect(100,100, 150, 250, 20);
1-2 利用函式做出更多的卡片
void setup()
{
size(500,500);
}
int w=20;
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(#FF9090);
rect(x,y, 150, 250, 20);
}
1-3 加上文字
void setup()
{
size(500,500);
}
int w=20;
void draw()
{
drawPokerCard(100,100, "S4");
drawPokerCard(130,150, "H3");
drawPokerCard(160,200, "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(#FF9090);
rect(x,y, 150, 250, 20);
fill(0);
textSize(40);
text(face, x, y+40);
}
1-4 變更文字的字體
void setup()
{
size(500,500);
PFont font = createFont("標楷體", 40);
textFont(font);
}
int w=20;
void draw()
{
drawPokerCard(100,100, "黑桃4");
drawPokerCard(130,150, "紅心3");
drawPokerCard(160,200, "方塊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(#FF9090);
rect(x,y, 150, 250, 20);
fill(0);
textSize(40);
text(face, x, y+40);
}
(變更字形 : 檔案 => 偏好設定 => 編輯字形)
1-5 讓文字有不同的顏色
void setup()
{
size(500,500);
PFont font = createFont("標楷體", 40);
textFont(font);
}
int w=20;
void draw()
{
drawPokerCard(100,100, "黑桃4");
drawPokerCard(130,150, "紅心3");
drawPokerCard(160,200, "方塊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(#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);
}
1-6 隨機變換卡片的數字
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,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);
}
沒有留言:
張貼留言