一篇 不錯的Delphi游戲編寫教程,實現數字拼圖游戲,包括了完整的代碼和注釋,後附有源碼下載。本款Delphi拼圖游戲是從另外一個思路實現了數字拼圖游戲。從菜單中選擇“新游戲”後,19個數字就會隨機顯示到19個SpeedButton上。我們要做的就是將這19個數字排列好,就算游戲成功。程序運行結果如圖:
在實現思路方面,個人覺得很簡單:將隨機產生的數字有序化。特點如:1、數字隨機顯示。2、背景可以自行更換。3、數字顏色可以更換。因此具體的思路是:游戲開始,數字隨機生成後,先判斷空位的位置,再判斷空位附近的按鈕,並檢測這些按鈕的可移動方向,然後將這些數字按鈕排列成有序即可。
編寫代碼。
(1)首先,要定義本例用到的幾個全局變量。
var Form1: TForm1; x,y:integer; //數字按鈕移動的點坐標 vright,vleft,vdown,vup:boolean; //數字移動方向的判斷 vright2left,vleft2right,vdown2up,vup2down:boolean;//數字移動方向的判斷 a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12, a13,a14,a15,a16,a17,a18,a19:integer; //19個數字 RanNums:integer; //隨機數字
(2)單擊Newgame1(“新游戲”),添加下列代碼以響應OnClick事件,開啟Timr1,隨機生成19個數字,並將這些數字顯示到19個SpeedButton中。
procedure TForm1.Newgame1Click(Sender: TObject); begin RanNums:=random(20); //顯示speedbutton1上的數字 if RanNums=0 then RanNums:=RanNums+1; a1:=RanNums; speedbutton1.caption:=IntToStr(RanNums); repeat //顯示speedbutton2上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a2:=RanNums; speedbutton2.caption:=inttostr(RanNums); until(a2<>a1); repeat //顯示speedbutton3的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a3:=RanNums; speedbutton3.caption:=inttostr(RanNums); until(a3<>a1)and(a3<>a2); repeat //顯示speedbutton4上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a4:=RanNums; speedbutton4.caption:=inttostr(RanNums); until(a4<>a1)and(a4<>a2)and(a4<>a3); repeat //顯示speedbutton5上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a5:=RanNums; speedbutton5.caption:=inttostr(RanNums); until(a5<>a1)and(a5<>a2)and(a5<>a3)and(a5<>a4); repeat //顯示speedbutton6上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a6:=RanNums; speedbutton6.caption:=inttostr(RanNums); until(a6<>a1)and(a6<>a2)and(a6<>a3)and(a6<>a4)and(a6<>a5); repeat //顯示speedbutton7上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a7:=RanNums; speedbutton7.caption:=inttostr(RanNums); until(a7<>a1)and(a7<>a2)and(a7<>a3)and(a7<>a4)and(a7<>a5)and(a7<>a6); repeat //顯示speedbutton8上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a8:=RanNums; speedbutton8.caption:=inttostr(RanNums); until(a8<>a1)and(a8<>a2)and(a8<>a3)and(a8<>a4)and(a8<>a5)and(a8<>a6)and(a8<>a7); repeat //顯示speedbutton9上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a9:=RanNums; speedbutton9.caption:=Inttostr(RanNums); until(a9<>a1)and(a9<>a2)and(a9<>a3)and(a9<>a4)and(a9<>a5)and(a9<>a6)and(a9<>a7) and(a9<>a8); repeat //顯示speedbutton10上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a10:=RanNums; speedbutton10.caption:=inttostr(RanNums); until(a10<>a1)and(a10<>a2)and(a10<>a3)and(a10<>a4)and(a10<>a5)and(a10<>a6)and(a10<>a7) and(a10<>a8)and(a10<>a9); repeat //顯示speedbutton11上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a11:=RanNums; speedbutton11.caption:=inttostr(RanNums); until(a11<>a1)and(a11<>a2)and(a11<>a3)and(a11<>a4)and(a11<>a5)and(a11<>a6)and(a11<>a7) and(a11<>a8)and(a11<>a9)and(a11<>a10); repeat //顯示speedbutton12上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a12:=RanNums; speedbutton12.caption:=inttostr(RanNums); until(a12<>a1)and(a12<>a2)and(a12<>a3)and(a12<>a4)and(a12<>a5)and(a12<>a6)and(a12<>a7) and(a12<>a8)and(a12<>a9)and(a12<>a10)and(a12<>a11); repeat //顯示speedbutton13上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a13:=RanNums; speedbutton13.caption:=inttostr(RanNums); until(a13<>a1)and(a13<>a2)and(a13<>a3)and(a13<>a4)and(a13<>a5)and(a13<>a6)and(a13<>a7) and(a13<>a8)and(a13<>a9)and(a13<>a10)and(a13<>a11)and(a13<>a12); repeat //顯示speedbutton14上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a14:=RanNums; speedbutton14.caption:=inttostr(RanNums); until(a14<>a1)and(a14<>a2)and(a14<>a3)and(a14<>a4)and(a14<>a5)and(a14<>a6)and(a14<>a7) and(a14<>a8)and(a14<>a9)and(a14<>a10)and(a14<>a11)and(a14<>a12)and(a14<>a13); repeat //顯示speedbutton15上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a15:=RanNums; speedbutton15.caption:=inttostr(RanNums); until(a15<>a1)and(a15<>a2)and(a15<>a3)and(a15<>a4)and(a15<>a5)and(a15<>a6)and(a15<>a7) and(a15<>a8)and(a15<>a9)and(a15<>a10)and(a15<>a11)and(a15<>a12)and(a15<>a13) and(a15<>a14); repeat //顯示speedbutton16上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a16:=RanNums; speedbutton16.caption:=inttostr(RanNums); until(a16<>a1)and(a16<>a2)and(a16<>a3)and(a16<>a4)and(a16<>a5)and(a16<>a6)and(a16<>a7) and(a16<>a8)and(a16<>a9)and(a16<>a10)and(a16<>a11)and(a16<>a12)and(a16<>a13) and(a16<>a14)and(a16<>a15); repeat //顯示speedbutton17上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a17:=RanNums; speedbutton17.caption:=inttostr(RanNums); until(a17<>a1)and(a17<>a2)and(a17<>a3)and(a17<>a4)and(a17<>a5)and(a17<>a6)and(a17<>a7) and(a17<>a8)and(a17<>a9)and(a17<>a10)and(a17<>a11)and(a17<>a12)and(a17<>a13) and(a17<>a14)and(a17<>a15)and(a17<>a16); repeat //顯示speedbutton18上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a18:=RanNums; speedbutton18.caption:=inttostr(RanNums); until(a18<>a1)and(a18<>a2)and(a18<>a3)and(a18<>a4)and(a18<>a5)and(a18<>a6)and(a18<>a7) and(a18<>a8)and(a18<>a9)and(a18<>a10)and(a18<>a11)and(a18<>a12)and(a18<>a13) and(a18<>a14)and(a18<>a15)and(a18<>a16)and(a18<>a17); repeat //顯示speedbutton19上的數字 RanNums:=random(20); if RanNums=0 then RanNums:=RanNums+1; a19:=RanNums; speedbutton19.caption:=inttostr(RanNums); until(a19<>a1)and(a19<>a2)and(a19<>a3)and(a19<>a4)and(a19<>a5)and(a19<>a6)and(a19<>a7) and(a19<>a8)and(a19<>a9)and(a19<>a10)and(a19<>a11)and(a19<>a12)and(a19<>a13) and(a19<>a14)and(a19<>a15)and(a19<>a16)and(a19<>a17)and(a19<>a18); end;
(3)雙擊SpeedButton1(“1”),然後添加下列代碼以響應OnClick事件,根據不同的條件判斷其移動與否,向哪個方向移動。
procedure TForm1.SpeedButton1Click(Sender: TObject); {----------------------先構造一個檢測函數,用於判斷方向-----------------------------------} procedure detect; begin //8個方向的初始值都是False vright:=falsE; vleft:=false; vdown:=false; vup:=falsE; vright2left:=falsE; vleft2right:=false; vdown2up:=false; vup2down:=falsE; if(speedbutton1.left+48=Panel1.left)and(speedbutton1.top=Panel1.top) then vright:=true; //按鈕右移 if(speedbutton1.left-48=Panel1.left)and(speedbutton1.top=Panel1.top) then vleft:=true; //按鈕左移 if(speedbutton1.left=Panel1.left)and(speedbutton1.top-40=Panel1.top) then vup:=true; //按鈕上移 if(speedbutton1.left=Panel1.left)and(speedbutton1.top+40=Panel1.top) then vdown:=true; //按鈕下移 end; {-------------------下面是SpeedButton1的OnClick事件的內容---------------------------------} begin detect; if(vright=true)and(vleft2right=false) then begin x:=speedbutton1.left; y:=speedbutton1.top; speedbutton1.left:=Panel1.left; speedbutton1.top:=Panel1.top; Panel1.lefT:=x; Panel1.top:=y; vright2left:=true; end; if(vleft=true)and(vright2left=false) then begin x:=speedbutton1.left; y:=speedbutton1.top; speedbutton1.left:=Panel1.left; speedbutton1.top:=Panel1.top; Panel1.lefT:=x; Panel1.top:=y; end; if(vup=true)and(vup2down=falsE) then begin x:=speedbutton1.left; y:=speedbutton1.top; speedbutton1.left:=Panel1.left; speedbutton1.top:=Panel1.top; Panel1.lefT:=x; Panel1.top:=y; vup2down:=true; end; if(vdown=true)and(vup2down=falsE) then begin x:=speedbutton1.left; y:=speedbutton1.top; speedbutton1.left:=Panel1.left; speedbutton1.top:=Panel1.top; Panel1.lefT:=x; Panel1.top:=y; vup2down:=false; end; end;
要說明的是:其余18個SpeedButton的OnClick事件也類似這段代碼,這裡就不一一列舉了,可以參考光盤的內容。
(4)本例還實現了一些必要的游戲設置,如背景圖的更換和SpeedButton上的數字顏色。下面的這兩段代碼就實現了這兩個功能。
procedure TForm1.N2Click(Sender: TObject); begin If OpenPictureDialog1.Execute Then //設置背景圖 Begin Image1.Stretch:=True; Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName); //載入背景圖片 end; end; procedure TForm1.N3Click(Sender: TObject); begin If ColorDialog1.Execute Then //設置數字顏色 Begin SpeedButton1.Font.Color:=ColorDialog1.Color; SpeedButton2.Font.Color:=ColorDialog1.Color; SpeedButton3.Font.Color:=ColorDialog1.Color; SpeedButton4.Font.Color:=ColorDialog1.Color; SpeedButton5.Font.Color:=ColorDialog1.Color; SpeedButton6.Font.Color:=ColorDialog1.Color; SpeedButton7.Font.Color:=ColorDialog1.Color; SpeedButton8.Font.Color:=ColorDialog1.Color; SpeedButton9.Font.Color:=ColorDialog1.Color; SpeedButton10.Font.Color:=ColorDialog1.Color; SpeedButton11.Font.Color:=ColorDialog1.Color; SpeedButton12.Font.Color:=ColorDialog1.Color; SpeedButton13.Font.Color:=ColorDialog1.Color; SpeedButton14.Font.Color:=ColorDialog1.Color; SpeedButton15.Font.Color:=ColorDialog1.Color; SpeedButton16.Font.Color:=ColorDialog1.Color; SpeedButton17.Font.Color:=ColorDialog1.Color; SpeedButton18.Font.Color:=ColorDialog1.Color; SpeedButton19.Font.Color:=ColorDialog1.Color; end; end;
思路非常簡單,可是真正要實現起來需要很長的完整實現代碼。光是SpeedButton的OnClick事件就有19段。但是代碼的重復性比較大,只要稍稍修改就行了。從這裡可以看出,游戲開發的另一個特點就是適度的重復。