當(dāng)前位置:首頁(yè)>軟件教程>maya教程>教程內(nèi)容

mel語(yǔ)初解之一-基礎(chǔ)和界面篇(4)

來(lái)源: 作者:admin 學(xué)習(xí):20369人次

關(guān)于表單布局,再舉兩個(gè)例子:
-----------------------------------------
// 這是一種常用的按鈕擺放方法
global proc myTest()
{
if(`window -ex myTestWin`) deleteUI myTestWin;

window -t "表單布局1(formLayout)測(cè)試窗" myTestWin;

string $form = `formLayout -numberOfDivisions 3`; // 分3份
// "-horizontalScrollBarThickness 0"無(wú)橫向滾動(dòng)條
string $scroll = `scrollLayout -horizontalScrollBarThickness 0`;
columnLayout;
button; button; button;button; button;
button; button; button;button; button;
setParent ..;
setParent ..;

string $sep = `separator`;
string $b1 = `button -label "按鈕1"`;
string $b2 = `button -label "按鈕2"`;
string $b3 = `button -label "按鈕3"`;

formLayout -edit
// 滾動(dòng)布局
-attachForm $scroll "top" 4 // 頂部距離表單4個(gè)像素
-attachForm $scroll "left" 4 // 左邊距離表單4個(gè)像素
-attachControl $scroll "bottom" 4 $sep // 底部距離"分隔線"4個(gè)像素
-attachForm $scroll "right" 4 // 右邊距離表單4個(gè)像素
// 分隔線
-attachNone $sep "top" // 頂部為默認(rèn)值
-attachForm $sep "left" 2 // 左邊距離表單2個(gè)像素
-attachControl $sep "bottom" 4 $b1 // 底部距離"按鈕1"4個(gè)像素
-attachForm $sep "right" 2 // 右邊距離表單2個(gè)像素
// 按鈕1
-attachNone $b1 "top" // 頂部為默認(rèn)值
-attachForm $b1 "left" 4 // 左邊距離表單4個(gè)像素
-attachForm $b1 "bottom" 4 // 底部距離表單4個(gè)像素
-attachPosition $b1 "right" 2 1 // 右邊距離表單的1/3處2個(gè)像素
// 按鈕2
-attachNone $b2 "top" // 頂部為默認(rèn)值
-attachControl $b2 "left" 4 $b1 // 左邊距離"按鈕1"表單4個(gè)像素
-attachForm $b2 "bottom" 4 // 底部距離表單4個(gè)像素
-attachPosition $b2 "right" 2 2 // 右邊距離表單的2/3處2個(gè)像素
// 按鈕3
-attachNone $b3 "top" // 頂部為默認(rèn)值
-attachControl $b3 "left" 4 $b2 // 左邊距離"按鈕2"4個(gè)像素
-attachForm $b3 "bottom" 4 // 底部距離表單4個(gè)像素
-attachForm $b3 "right" 4 // 右邊距離表單4個(gè)像素
$form;

showWindow myTestWin;
}
myTest;

-----------------------------------------
// 隨意擺放控件的位置
global proc myTest()
{
if(`window -ex myTestWin`) deleteUI myTestWin;

window -t "表單布局2(formLayout)測(cè)試窗" myTestWin;

string $form = `formLayout`;
string $b1 = `button -l "按鈕1"`;
string $b2 = `button -l "按鈕2"`;
string $b3 = `button -l "按鈕3"`;

formLayout -e
-af $b1 "top" 1
-af $b1 "left" 5
-af $b2 "top" 19
-af $b2 "left" 132
-af $b3 "top" 46
-af $b3 "left" 40
$form;

showWindow myTestWin;
}
myTest;

"rowLayout"命令 (橫布局)
橫布局與橫豎布局有些相似,只不過(guò)控件只能橫著擺而已。
-----------------------------------------
window;
rowLayout -numberOfColumns 3
-columnWidth3 80 75 150
-adjustableColumn 2
-columnAlign 1 "right"
-columnAttach 1 "both" 0
-columnAttach 2 "both" 0
-columnAttach 3 "both" 0;
text;
intField;
intSlider;
showWindow;
-----------------------------------------

"gridLayout"命令 (格子布局)
格子布局與橫豎布局有些相似,受格子的影響,每個(gè)的控件大小相同。
-----------------------------------------
window;
gridLayout -numberOfColumns 2 -cellWidthHeight 50 50;
button; button; button; button;
button; button; button;
showWindow;
-----------------------------------------

"menuBarLayout"命令 (菜單條布局)
用于一個(gè)窗口放多個(gè)菜單條的情況。
-----------------------------------------
string $window = `window`;
columnLayout -adjustableColumn true;
// Create first menu bar layout.
string $menuBarLayout = `menuBarLayout`;
menu -label "File";
menuItem -label "New";
menuItem -label "Open";
menuItem -label "Close";
menu -label "Help" -helpMenu true;
menuItem -label "About...";
columnLayout;
button -label "Add Menu"
-command ("menu -parent " + $menuBarLayout + "; menuItem");
setParent ..;
setParent ..;

separator -height 10 -style "none";

// Create a second menu bar layout.
//
menuBarLayout;
menu -label "Edit";
menuItem -label "Cut";
menuItem -label "Copy";
menuItem -label "Paste";
menu -label "View";
menuItem -label "Fonts...";
menuItem -label "Colors...";
columnLayout;
text -label "Add some controls here.";
setParent ..;
setParent ..;

showWindow $window;
-----------------------------------------

"shelfLayout"命令 (工具架布局)
放置shelf按鈕的布局,支持鼠標(biāo)中鍵拖放代碼。
-----------------------------------------
window;
tabLayout;
shelfLayout Anim;
setParent ..;
shelfLayout Render;
setParent ..;
shelfLayout Misc;
setParent ..;
showWindow;
-----------------------------------------

"shelfTabLayout"命令 (工具架標(biāo)簽布局)
-----------------------------------------
可以使工具架上多一個(gè)垃圾桶圖標(biāo),以便刪除不用的shelf按鈕。
window;
shelfTabLayout
-image "smallTrash.xpm"
-imageVisible true mainShelfTab;
shelfLayout Dynamics;
setParent ..;
shelfLayout Rendering;
setParent ..;
shelfLayout Animation;
setParent ..;
showWindow;

共有的標(biāo)志(一):
如果你讀過(guò)mel命令的幫助文檔,你會(huì)發(fā)現(xiàn)所有的界面命令都具有一些共同的標(biāo)志。比如,"-q"、"-e"就是所有界面命令都具有的標(biāo)志。我把這些標(biāo)志介紹一下,這樣無(wú)論任何界面(布局或控件)命令,只要用到這些標(biāo)志時(shí),你們就都知道怎么去用了。

"-q/-query"(查詢)
表示查詢的意思,你可以通過(guò)它獲得某一標(biāo)志的數(shù)值。(前面講過(guò)了)

"-e/-edit"(編輯)
表示編輯的意思,你可以通過(guò)它修改某一標(biāo)志的數(shù)值。(前面講過(guò)了)

"-ex/exists"(是否存在)
通過(guò)"-ex"可以得知一個(gè)界面元素是否存在。
例如前面講過(guò)的"if(`window -ex myTestWin`)"。

"-dt/defineTemplate" (定義模板)
"-ut/useTemplate" (使用模板)
一般我們沒(méi)必要定義模板,不過(guò)有幾個(gè)Maya已經(jīng)定義好的模板有時(shí)會(huì)用到,我以后再講。

"-p/parent" (指定父級(jí)界面元素)
指定父級(jí)界面元素(布局或菜單),平常我們用"setParent"命令來(lái)解決這個(gè)問(wèn)題,所以"-p"很少用到。

"-en/enable" (是否激活)
我們經(jīng)?吹揭恍┎藛位虬粹o上的字是灰色的,表示它們現(xiàn)在不能用。
使用"-en"就可以讓你的界面元素上的字變灰,使它們不能用。
例如"button -e -en off myButton1"就是讓按鈕myButton1變得不能用。
而"button -e -en on myButton1"就是讓按鈕myButton1恢復(fù)活力。

"-w/width" (寬度)
界面元素的寬度為多少像素。
你會(huì)發(fā)現(xiàn)使用這個(gè)標(biāo)志時(shí)經(jīng)常不起作用。
比如如果在布局命令中指定了里面控件的寬度,再給布局中這些控件指定寬度就不起作用了。
窗口的寬度只在第一次創(chuàng)建時(shí)有用,以后創(chuàng)建會(huì)使用預(yù)置中的尺寸。預(yù)置中的尺寸就是窗口關(guān)閉時(shí)的大小,這個(gè)尺寸記錄在預(yù)置文件(windowPrefs.mel)中,可以通過(guò)"windowPref"命令去除窗口的預(yù)置尺寸,方法是"windowPref -remove 窗口名;"

"-h/height" (高度)
界面元素的高度為多少像素。
用法同"-w/width",例如"button -w 32 -h 32;"。

"-vis/visible" (是否可見(jiàn))
指定界面元素是否可見(jiàn)。
前面講過(guò)"window -visible off"的問(wèn)題。其它界面元素也可以通過(guò)"-visible off"暫時(shí)隱藏,想要顯示出來(lái),就用"-visible on"。

"-io/isObscured" (是否看不到)
查詢界面元素是否不可見(jiàn)。你肯定會(huì)問(wèn)"-io"和"-vis"的區(qū)別。"-io"只能用于查詢(-q)模式,般查詢結(jié)果正好與"-vis"的查詢結(jié)果相反。不過(guò)"-io"的不可見(jiàn)包括窗口最小化、父級(jí)界面元素隱藏等造成的不可見(jiàn)因素。

"-m/manage" (可控制)
指定界面元素是否可見(jiàn)。跟"-vis"沒(méi)什么區(qū)別。

"-ann/annotation" (注釋)
給界面元素注釋。注釋可以在幫助欄顯示,也可以讓鼠標(biāo)在界面元素上停一會(huì),在彈出式的淡黃底色注釋條看到。

"-bgc/backgroundColor" (底色)
要想編寫(xiě)彩色窗口就要用到這個(gè)標(biāo)志。

還有一些共有標(biāo)志留到以后講。

布局到此已經(jīng)全講完了,接下來(lái)講控件。
前面提到過(guò)一些控件:
"text"命令 (靜態(tài)文本)
"textField"命令 (文本框)
"button"命令 (按鈕)
"separator"命令 (分隔線)

這些控件雖然已經(jīng)講過(guò)了,但由于十分重要,現(xiàn)在在強(qiáng)調(diào)一下,一定要熟練掌握。

另外,先隨便講幾個(gè):
"picture"命令 (圖片)
靜態(tài)圖片。
---------------------------------------------------
picture -w 80 -h 60 -image "sphere.xpm" -tile on;
---------------------------------------------------
"-i/image"指定圖片。
"-tl/tile"指定圖片是否重復(fù)排疊顯示。

"iconTextButton"命令 (圖標(biāo)文本按鈕)
既有字又有圖標(biāo)的按鈕,你可以只顯示字或圖標(biāo)。
---------------------------------------------------
string $window = `window`;
columnLayout -adj 1;
iconTextButton -style "textOnly"
-image1 "sphere.xpm" -label "sphere";
iconTextButton -style "iconOnly"
-image1 "spotlight.xpm" -label "spotlight";
iconTextButton -style "iconAndTextHorizontal"
-image1 "cone.xpm" -label "cone";
iconTextButton -style "iconAndTextVertical"
-image1 "cube.xpm" -label "cube";
showWindow $window;

"symbolButton"命令 (符號(hào)按鈕)
有圖片的按鈕。
---------------------------------------------------
symbolButton -image "cone.xpm" -c "cone";
---------------------------------------------------

"intSlider"命令 (整數(shù)型滑動(dòng)條)
---------------------------------------------------
intSlider -min -100 -max 100 -value 0;
---------------------------------------------------
"-min"最小值
"-max"最大值
"-value"當(dāng)前值

一直這么講下去難免有些枯燥乏味,還是來(lái)點(diǎn)實(shí)戰(zhàn)輕松一下吧。
下面介紹一下賭牌游戲myFourUp的編寫(xiě)方法。
------------------------------
實(shí)例7:賭牌游戲myFourUp
myFourUp程序很像一個(gè)撲克機(jī),它的原版是Stephen D. Gilbert的Visual C++ 6編程藍(lán)皮書(shū)中的一個(gè)實(shí)例(FourUp),我認(rèn)為這個(gè)程序用于Mel也同樣很能說(shuō)明問(wèn)題,就擅自把它移植了過(guò)來(lái),不過(guò)我在界面和算法部分都作了較大改動(dòng)。

說(shuō)到打賭,myFourUp程序與拉斯韋加斯和亞特蘭大城中賭場(chǎng)工作人員特別喜愛(ài)的撲克游戲相似。但myFourUp更簡(jiǎn)單,因此你可以集中精力學(xué)習(xí)程序設(shè)計(jì)和界面編寫(xiě)方法。
myFourUp并非技巧游戲。當(dāng)游戲開(kāi)始時(shí),給玩者1000美元。每一輪(需花一定賭注,比如2美元)玩者接到4張牌。牌值無(wú)關(guān)緊要-我們關(guān)心的只是花色。如果玩者得到兩對(duì)(例如,兩張紅心和兩張方片),將得4美元。如果玩者得到三個(gè)單張,將得到6美元。如果發(fā)牌者擺出四張同一花色,將得8美元。

當(dāng)然,游戲并非使用真錢(qián)。如果你想編寫(xiě)程序以便能撥入玩者的銀行帳戶來(lái)取出贏利(或彌補(bǔ)虧空),那是你自己的事。

在我們編寫(xiě)代碼之前,讓我們先花一點(diǎn)時(shí)間來(lái)計(jì)劃一下程序的外觀以及它將如何工作。最好是用紙和筆畫(huà)出該布局,畢竟修訂一個(gè)計(jì)劃比改變一個(gè)完成的程序要容易。

現(xiàn)在可以根據(jù)草圖來(lái)編寫(xiě)代碼,不過(guò)編寫(xiě)代碼之前最好先講一下什么是全局變量。
全局變量(Global Variable)
我們以前用到的變量都是局部變量。局部變量只能在函數(shù)里面用,當(dāng)函數(shù)運(yùn)行時(shí)局部變量被創(chuàng)建,函數(shù)運(yùn)行結(jié)束后,局部變量就被刪除了。

如果用戶需要在一個(gè)函數(shù)中創(chuàng)建的變量,其它的函數(shù)也可用,則用戶可以通過(guò)全局變量來(lái)實(shí)現(xiàn)。如果你創(chuàng)建了一個(gè)全局變量,你可以通過(guò)函數(shù),也可以通過(guò)命令行不斷修改它的值。全局變量將一直保留在內(nèi)存中,直到你退出Maya時(shí)才會(huì)被刪除。

在變量聲明前加上"global"表示為全局變量。(在命令行聲明全局變量可以不寫(xiě)"global")
例如:
global float $counter;

跟椐Maya幫助文檔的說(shuō)法應(yīng)當(dāng)盡量少用全局變量,否則很容易會(huì)因?yàn)榇中亩鲥e(cuò)。比如你聲明了一個(gè)全局變量$counter,而Maya中卻碰巧已經(jīng)有了一個(gè)全局變量$counter,此時(shí)你對(duì)$counter變量值所作的任何修改都將影響以前就存在的全局變量$counter,這將會(huì)產(chǎn)生錯(cuò)誤。
為避免碰巧同名變量的存在,請(qǐng)用較為獨(dú)特的名稱(chēng)來(lái)命名全局變量。Maya中的全局變量都是以小寫(xiě)字母"g"開(kāi)頭(例如$gMainWindow),你可以用除"g"以外的你喜歡的字母開(kāi)頭,本教程中用到的全局變量一般都以"mg"開(kāi)頭。

下面開(kāi)始我們的程序。程序雖然簡(jiǎn)單,但這個(gè)是一個(gè)完善的游戲程序,對(duì)于某些初學(xué)者來(lái)說(shuō)可能有些難度,如果你實(shí)在看不懂,就索性照抄一遍,借此體驗(yàn)一個(gè)編程的感覺(jué)。

在myFourUp程序中,用一個(gè)全局變量$mgMoneyRemaining來(lái)記錄你擁有的總錢(qián)數(shù),當(dāng)你贏錢(qián)時(shí)總錢(qián)數(shù)會(huì)增加,反之則會(huì)減少。
----------------------------------
//global proc myFourUp(){以下的代碼寫(xiě)在括號(hào)中}

// 初始化總錢(qián)數(shù)為1000美元
global int $mgMoneyRemaining;
$mgMoneyRemaining = 1000;

界面代碼:
首先創(chuàng)建窗口:
----------------------------------
if(`window -ex myFourUpWin`) deleteUI myFourUpWin;
window -t "Four Up"
-widthHeight 293 440
-sizeable false
myFourUpWin;
----------------------------------
"-wh/widthHeight"指定窗口的長(zhǎng)度和寬度分別為多少像素。也可用"-w"和"-h"分別來(lái)指定。
"-s/sizeable"指定窗口是否可以拉大縮小(變化尺寸)。

為了能夠隨意擺放控件,用一個(gè)表單布局作為窗口的主布局。
----------------------------------
string $form = `formLayout -backgroundColor 0.353 0.353 0.259`;
----------------------------------
創(chuàng)建布局$form中的元素:
----------------------------------
// 標(biāo)題圖片
string $pTitle_f = `picture -image "title_f.bmp" fourUpTitle`;
// 邊框圖片
string $pFrame_l = `picture -image "frame_l.bmp"`;
string $pFrame_r = `picture -image "frame_r.bmp"`;
string $pFrame_b = `picture -image "frame_b.bmp"`;

// 位圖按鈕(圖標(biāo)文本按鈕)
string $bBtn_1 = `iconTextButton -style "iconOnly"
-backgroundColor 0.353 0.353 0.259
-w 106 -h 41 -image1 "btn_deal.bmp"
-c "dealCards"`;
string $bBtn_2 = `iconTextButton -style "iconOnly"
-backgroundColor 0.353 0.353 0.259
-w 106 -h 41 -image1 "btn_Exit.bmp"
-c "deleteUI myFourUpWin;"`;

// 橫布局
// 包括一個(gè)靜態(tài)圖片,一個(gè)文本框和一個(gè)符號(hào)按鈕
string $row = `rowLayout -numberOfColumns 3
-backgroundColor 0.353 0.353 0.259
-columnWidth3 82 60 80`;
picture -image "deposit.bmp";
textField -ed false -tx "$2" placeBetField;
symbolButton -image "max.bmp" -c "placeBetMax";
setParent ..;

// 整數(shù)型滑動(dòng)條
string $slider = `intSlider
-backgroundColor 0.568 0.588 0.463
-min 2 -max 100 -value 2 -step 20
-dragCommand "placeBet" placeBetSlider`;

// 兩個(gè)邊框布局
string $frame_1 = `frameLayout -backgroundColor 0.863 0.729 0.435
-label "你還剩:$1000 "
-labelAlign "bottom" -borderStyle "etchedIn"
-h 65 MoneyRemainingFrame`;
// @@@A 創(chuàng)建布局$frame_1中元素的代碼寫(xiě)在這里:

setParent ..;

string $frame_2 = `frameLayout -backgroundColor 0.863 0.729 0.435
-label "贏錢(qián)規(guī)則:"
-labelAlign "bottom" -borderStyle "etchedIn"
-h 110`;
// @@@B 創(chuàng)建布局$frame_2中元素的代碼寫(xiě)在這里:

setParent ..;
----------------------------------

指定布局$form中每個(gè)界面元素的位置,
注意每張圖片所放置的位置。
----------------------------------
formLayout -e
// frame_l.bmp直接放在布局$form的左上角
-af $pFrame_l "top" 0
-af $pFrame_l "left" 0

// title_f.bmp
-af $pTitle_f "top" 0 // 上邊緊靠布局的上邊
-ac $pTitle_f "left" 0 $pFrame_l // 左邊緊靠frame_l.bmp

// frame_r.bmp
-af $pFrame_r "top" 0 // 上邊緊靠布局的上邊
-ac $pFrame_r "left" 0 $pTitle_f // 左邊緊靠title_f.bmp

// frame_b.bmp
-af $pFrame_b "bottom" 0 // 底邊緊靠布局的底邊
-ac $pFrame_b "left" 0 $pFrame_l // 左邊緊靠frame_l.bmp

-ac $frame_1 "top" 5 $pTitle_f
-ac $frame_1 "left" 5 $pFrame_l
-ac $frame_1 "right" 2 $pFrame_r

-ac $frame_2 "top" 12 $frame_1
-ac $frame_2 "left" 5 $pFrame_l
-ac $frame_2 "right" 2 $pFrame_r

-ac $row "top" 12 $frame_2
-ac $row "left" 5 $pFrame_l

-ac $slider "top" 8 $row
-ac $slider "left" 5 $pFrame_l
-ac $slider "right" 2 $pFrame_r

// btn_deal.bmp
-ac $bBtn_1 "bottom" 0 $pFrame_b // 底邊緊靠frame_b.bmp
-ac $bBtn_1 "left" 0 $pFrame_l // 左邊緊靠frame_l.bmp

// btn_exit.bmp
-ac $bBtn_2 "bottom" 0 $pFrame_b // 底邊緊靠frame_b.bmp
-ac $bBtn_2 "right" 0 $pFrame_r // 右邊緊靠frame_r.bmp
$form;
----------------------------------
顯示窗口:
----------------------------------
showWindow myFourUpWin;
----------------------------------

界面和主要部分編好了,下面讓我們來(lái)填補(bǔ)兩個(gè)邊框布局。

在前面代碼的"@@@A..."這句注釋之后填寫(xiě)如下代碼:
----------------------------------
// 布局$form_1中的布局分兩層
string $form_1 = `formLayout`;
// 先寫(xiě)的為第一層,第一層放置底紋圖片"back_1.bmp"
// "-tl"圖片重疊擺放
string $pBack_1 = `picture -tl 1 -image "back_1.bmp"`;

// 放置四種花色圖片
// spade - 黑桃,club - 梅花,diamond - 方片,heart - 紅心。
string $card_s = `picture -image "card_spade.bmp" cardImg_0`;
string $card_c = `picture -image "card_club.bmp" cardImg_1`;
string $card_d = `picture -image "card_diamond.bmp" cardImg_2`;
string $card_h = `picture -image "card_heart.bmp" cardImg_3`;

// 指定位置
formLayout -e
-af $pBack_1 "top" 0
-af $pBack_1 "bottom" 0
-af $pBack_1 "left" 0
-af $pBack_1 "right" 0

-af $card_s "top" 6
-ap $card_s "left" 8 0

-af $card_c "top" 6
-ap $card_c "left" 8 25

-af $card_d "top" 6
-ap $card_d "left" 8 50

-af $card_h "top" 6
-ap $card_h "left" 8 75
$form_1;

setParent ..;

布局$form_2編寫(xiě)的方法類(lèi)似布局$form_1,我就不做解釋了。
在"@@@B..."這句注釋之后填寫(xiě)如下代碼:
----------------------------------
string $form_2 = `formLayout`;
string $pBack_2 = `picture -tl 1 -image "back_1.bmp"`;
string $pT1 = `text -l " 兩對(duì)同花 $4"
-backgroundColor 0.568 0.588 0.463 placeBetText1`;
string $pT2 = `text -l " 三個(gè)同花 $6"
-backgroundColor 0.568 0.588 0.463 placeBetText2`;
string $pT3 = `text -l " 全部同花 $8"
-backgroundColor 0.568 0.588 0.463 placeBetText3`;

formLayout -e
-af $pBack_2 "top" 0
-af $pBack_2 "bottom" 0
-af $pBack_2 "left" 0
-af $pBack_2 "right" 0

-ap $pT1 "top" 10 0
-af $pT1 "left" 15
-af $pT1 "right" 15

-ap $pT2 "top" 10 30
-af $pT2 "left" 15
-af $pT2 "right" 15

-ap $pT3 "top" 10 60
-af $pT3 "left" 15
-af $pT3 "right" 15
$form_2;

// 因?yàn)?form_2之后不再創(chuàng)建界面元素了,所以可以不寫(xiě)"setParent ..;"

界面部分編完了,現(xiàn)在來(lái)編寫(xiě)代碼讓這個(gè)游戲運(yùn)轉(zhuǎn)起來(lái)。
在前面的代碼中,有三處需要調(diào)用自定義的命令,分別是:

1)位圖按鈕(btn_deal.bmp)。
string $bBtn_1 = `iconTextButton ... -c "dealCards"`;

2)"-dragCommand"拉動(dòng)滑動(dòng)條時(shí)執(zhí)行的命令。
string $slider = `intSlider ... -dragCommand "placeBet" placeBetSlider`;

3)橫布局中的符號(hào)按鈕(max.bmp)。
symbolButton ... -c "placeBetMax";

// 按下"Max"(max.bmp)按鈕執(zhí)行的命令
global proc placeBetMax()
{
// 把滑動(dòng)條撥到最右邊
intSlider -e -v 100 placeBetSlider;
// 文本框中填入相應(yīng)的值$100
textField -e -tx "$100" placeBetField;

// 修改贏錢(qián)規(guī)則
text -e -l (" 兩對(duì)同花 $200") placeBetText1;
text -e -l (" 三個(gè)同花 $300") placeBetText2;
text -e -l (" 全部同花 $400") placeBetText3;
}

// 拉動(dòng)滑動(dòng)條時(shí)執(zhí)行的命令
global proc placeBet()
{
// 獲得滑動(dòng)條上的數(shù)值
int $deposit = `intSlider -q -v placeBetSlider`;
// 文本框中填入滑動(dòng)條的數(shù)值
string $text = "$" + $deposit;
textField -e -tx $text placeBetField;

// 修改贏錢(qián)規(guī)則
int $m1 = $deposit * 2;
int $m2 = $deposit * 3;
int $m3 = $deposit * 4;

text -e -l (" 兩對(duì)同花 $" + $m1) placeBetText1;
text -e -l (" 三個(gè)同花 $" + $m2) placeBetText2;
text -e -l (" 全部同花 $" + $m3) placeBetText3;
}

現(xiàn)在來(lái)對(duì)付關(guān)鍵部分,相對(duì)比較復(fù)雜。
我們可以先來(lái)考慮一下,當(dāng)玩家點(diǎn)擊了"deal"按鈕時(shí),我們希望發(fā)生什么?
1)隨機(jī)選擇4張牌作為一付牌。
2)更新每張牌的圖像以指示該付牌的花色。
3)統(tǒng)計(jì)每種花色牌的張數(shù)。
4)統(tǒng)計(jì)同種花色的牌一共有幾對(duì)。
5)從總錢(qián)數(shù)扣去賭注。
6)計(jì)算輸贏,如果贏錢(qián)加入總錢(qián)數(shù)中。
7)贏錢(qián)后加亮贏錢(qián)規(guī)則字樣的底色。
8)修改標(biāo)題圖片,如果是贏錢(qián)顯示"Congratulations",否則顯示"Misfortune"。
8)顯示剩余金額。
----------------------------------
// 位圖按鈕(btn_deal.bmp)命令
global proc dealCards()
{
global int $mgMoneyRemaining;

// 分別用四個(gè)變量記錄每種花色牌的張數(shù)
int $num_spade = 0;
int $num_club = 0;
int $num_diamond = 0;
int $num_heart = 0;
// 用$pairs記錄同種花色的牌一共有幾對(duì)
int $pairs = 0;

for($i=0; $i<4; $i++)
{
// 隨機(jī)選擇4張牌中的一張
int $num = `rand 4`;
if($num == 0) {
// 更新花色牌的圖片
picture -e -image "card_spade.bmp" ("cardImg_"+$i);
// 統(tǒng)計(jì)每種花色牌的張數(shù)
$num_spade++; // $num_spade = $num_spade + 1
}
if($num == 1) {
picture -e -image "card_club.bmp" ("cardImg_"+$i);
$num_club++;
}
if($num == 2) {
picture -e -image "card_diamond.bmp" ("cardImg_"+$i);
$num_diamond++;
}
if($num == 3) {
picture -e -image "card_heart.bmp" ("cardImg_"+$i);
$num_heart++;
}
}

// 統(tǒng)計(jì)同種花色的牌一共有幾對(duì)
if($num_spade == 2)
$pairs++;
if($num_club == 2)
$pairs++;
if($num_diamond == 2)
$pairs++;
if($num_heart == 2)
$pairs++;

//----------------------------------------------
// 從滑動(dòng)條上得到賭注數(shù)額
$deposit = `intSlider -q -v placeBetSlider`;
int $m1 = $deposit * 2;
int $m2 = $deposit * 3;
int $m3 = $deposit * 4;

// 先用總錢(qián)數(shù)減去賭注
$mgMoneyRemaining -= $deposit;

// 調(diào)整文字,為的是保證文本框底色的刷新
text -e -l "" -backgroundColor 0.568 0.588 0.463 placeBetText1;
text -e -l "" -backgroundColor 0.568 0.588 0.463 placeBetText2;
text -e -l "" -backgroundColor 0.568 0.588 0.463 placeBetText3;
// 修改標(biāo)題圖片為"Misfortune"
picture -e -image "title_m.bmp" fourUpTitle;

// 兩對(duì)同花
if($pairs == 2) {
// 贏的錢(qián)加入總錢(qián)數(shù)中
$mgMoneyRemaining += $m1;
// 加亮文本底色,變?yōu)榱咙S色
text -e -backgroundColor 1 1 0.463 placeBetText1;
// 修改標(biāo)題圖片為"Congratulations"
picture -e -image "title_c.bmp" fourUpTitle;
}

// 三個(gè)同花
if( $num_spade == 3 || $num_club == 3 ||
$num_diamond == 3 || $num_heart == 3 ) {
$mgMoneyRemaining += $m2;
text -e -backgroundColor 1 1 0.463 placeBetText2;
picture -e -image "title_c.bmp" fourUpTitle;
}

// 全部同花
if( $num_spade == 4 || $num_club == 4 ||
$num_diamond == 4 || $num_heart == 4 ) {
$mgMoneyRemaining += $m3;
text -e -backgroundColor 1 1 0.463 placeBetText3;
picture -e -image "title_c.bmp" fourUpTitle;
}

// 刷新
text -e -l (" 兩對(duì)同花 $" + $m1) placeBetText1;
text -e -l (" 三個(gè)同花 $" + $m2) placeBetText2;
text -e -l (" 全部同花 $" + $m3) placeBetText3;

// 顯示剩余金額
frameLayout -e -label ("你還剩:$" + $mgMoneyRemaining) MoneyRemainingFrame;
}

全部代碼編寫(xiě)完畢,現(xiàn)在你可以體驗(yàn)賭牌的樂(lè)趣了。

我把全部范例代碼和圖片放到附件中,供你們參考。

1515133-myFourUp.rar (56.52k)

"if"語(yǔ)句 (如果)
關(guān)于"if"語(yǔ)句以前講的不夠詳細(xì),現(xiàn)在做一點(diǎn)簡(jiǎn)要補(bǔ)充。小括號(hào)里的雙豎線"||"表示"或"的意思,而"&&"表示"并且"的意思。

"if"后面的小括號(hào)里的代碼表示的只能是一個(gè)布爾值,1或0。
因此要表示相等要寫(xiě)雙等號(hào)。

"雙等號(hào)的用法"
----------------------------------
int $test = 4;
int $bool = $test == 5; // 如果$test等于5,$bool = 1(true)
----------------------------------
這里因?yàn)?test = 4,不等于5,所以$bool = 0(false)。

"rand"命令 (隨機(jī))
前面用到"rand"命令,"rand"是隨機(jī)的意思,就是從指定的范圍內(nèi)隨便取出一個(gè)數(shù)值。mel里的"rand"同C語(yǔ)言的"rand"有一個(gè)很明顯的差別,mel是浮點(diǎn)數(shù)的隨機(jī),C語(yǔ)言是整數(shù)的隨機(jī)。

我在myFourUp程序中寫(xiě)的"int $num = `rand 4`;" 實(shí)際上是一種浮點(diǎn)數(shù)轉(zhuǎn)整數(shù)的方法,還有一種寫(xiě)法更容易看明白一些:
float $floatNum = `rand 4`;
int $num = (int)$floatNum;

學(xué)習(xí) · 提示

  • 一定要打開(kāi)PS,跟著教程做一遍,做完的圖到這交作業(yè):提交作業(yè)
  • 建議練習(xí)時(shí),大家自己找素材,盡量不要用教程提供的素材。
  • 教程有看不懂的地方,可以到論壇發(fā)帖提問(wèn):新手求助
  • 加官方微信,隨時(shí)隨地,想學(xué)就能學(xué):ps_bbs,或掃右側(cè)二維碼!
  • 關(guān)注我們學(xué)更多,每天都有新教程:新浪微博 抖音視頻 微信小程序
- 發(fā)評(píng)論 | 交作業(yè) -
最新評(píng)論
42017-01-13 04:08
42017-01-13 03:52
挺牛逼的
2016-03-14 07:30
超贊的文章
陳軍波2015-12-14 12:59
竟然沒(méi)人評(píng)論

關(guān)注大神微博加入>>

網(wǎng)友求助,請(qǐng)回答!