?? tank.pas
字號:
unit Tank;
interface
uses
Windows,mmsystem, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, Menus;
type
//定義記錄類型
TPatDT=record
Used:Byte; //角色所使用的標記
Sban:Byte; //復合圖案編號
Xpos:Integer; //X坐標
Ypos:Integer; //Y坐標
Smov:Byte; //移動方向(0=右;1=左;2=下:3=上)
//Scon:Byte; //移動計數器
//Count:Byte; //計數器
//FBullet:Byte; //子彈標志
// FExplode:Byte; //爆炸標志
end;
TMainForm = class(TForm)
Timer1: TTimer;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
Timer2: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure Timer2Timer(Sender: TObject);
private
{ Private declarations }
//procedure SetValue(Chpon:array of Byte);
//定義控制敵方坦克的函數
Procedure Etanks(var Etank:TpatDt);
//定義控制我方坦克的函數
Procedure Mytanks(var Mytank:TpatDt);
//定義畫敵人坦克的函數 其中一步我方坦克的繪制也調用此函數,
Procedure DrawEtank(Sban:Byte;X1,Y1:Integer);
//定義碰撞檢測函數
Procedure HitCk;
//定義爆炸函數
Procedure Explode(Sban:Byte;X1,Y1:Integer);
//定義我方子彈控制函數
Procedure MyBullets;
//定義敵方子彈控制函數
Procedure EBullets;
//定義畫敵方子彈函數
Procedure DrawEBullets;
public
{ Public declarations }
end;
const
Yoko = 18; // 橫向圖樣數
Tate = 14; // 直立圖樣數
//Yoko = 36; // 橫向圖樣數
//Tate = 28; // 直立圖樣數
DYoko=Yoko*32; // 顯示的橫向點數
DTate=Tate*32; // 顯示縱向點數
MaxETank=5; // 屏幕內敵方最多出現的坦克數量
var
MainForm: TMainForm;
//定義背景載入圖
BitmapBJ:TBitmap;
//定義游戲結束載入圖
BitmapGameover:TBitmap;
//定義游戲過關圖
BitmapSuccess:Tbitmap;
// 定義載入用、去除用摸板用、背景用與繪圖用的點陣圖
Load_Bmap: TBitmap;
//Xpat_Bmap:TBitmap;
//Back_Bmap:TBitmap;
Make_Bmap: TBitmap;
//定義角色用與復合圖案敵方坦克所用的數組TPatDt類型
//定義敵方坦克的數組
Etank:array[0..(MaxEtank-1)] of TPatDt;
//定義我方坦克的數組
Mytank:array[0..0]of TpatDt;
//定義我方坦克子彈數組
MyBullet:array[0..0] of TPatDt;
//定義敵方坦克子彈數組
EBullet:array[0..MaxETank-1] of TPatDt;
//定義中間變量數組,用來控制磚塊軍旗碰壯用
MapGrid: array[0..17, 0..13] of TPatDt;
//把背景數組傳遞過來
Grid:array[0..17, 0..13] of Byte;
//定義各種變量(Byte類型、Trect類型)
P, PX, PY, GX, GY,n: Byte;
//定義碰撞中檢測坐標差值全局變量
X2,Y2,X3,Y3,X5,Y5,X6,Y6:Integer;
//敵人坦克爆炸時把坦克坐標傳到全局變量X4,Y4中; UseMap為爆炸圖片數組中的某一張
X4,Y4,UseMap:Integer;
rect_1,rect_2,rect_3,rect_4,Rect_L,Rect_M,Rect_D,Rect_B: TRect;
// 圖樣陣列2
{ Ptmap3: array[0..17, 0..13] of Byte = (
(0,0,0,0,0,0,0,0,0,0,0,0,0,0),
(0,1,1,0,0,0,0,0,0,0,0,0,0,0),
(0,1,1,0,0,0,0,0,0,0,0,0,0,0),
(0,1,1,0,0,0,0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0,0,0,0,0,0,0),
(0,1,1,0,0,1,1,1,0,0,0,0,0,0),
(0,1,1,0,0,1,1,1,0,0,0,0,0,0),
(0,1,1,0,0,1,1,1,0,0,0,0,0,0),
(0,0,0,0,0,1,1,1,0,0,0,0,1,1),
(0,1,1,0,0,1,1,1,0,0,0,0,1,30),
(0,1,1,0,0,1,1,1,0,0,0,0,1,1),
(0,1,1,0,0,1,1,1,0,0,0,0,0,0),
(0,0,0,0,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0,0,0,0,1,1),
(0,0,0,0,0,0,0,0,0,0,0,0,1,30),
(0,0,0,0,0,0,0,0,0,0,0,0,1,1),
(0,0,0,0,0,0,0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0,0,0,0,0,0,0),
(0,0,0,0,0,0,0,0,0,0,0,0,0,0)
);
//歡迎畫面 //游戲進行中 //過關畫面 //GAME OVER //程式即將關閉
gsTitle,gsPlaying,gsSuccess,gsOver,gsTerminate: bool;
//定義爆炸圖畫數組
ExplodeMap:array[0..5]of Byte=(32,33,34,35,36,0);
//定義一個全局變量用于當發射子彈時選擇敵人坦克的某一輛坦克
ChooseENum:Byte;
implementation
{$R *.dfm}
procedure TMainForm.FormCreate(Sender: TObject);
var
X, Y,Cn: Byte;
begin
//FORM剛創建時把游戲開始標志設為FALSE
gsPlaying:=false;
//定義窗口的高與寬
MainForm.Height:= 500;
MainForm.Width:=584;
//導入最最原始的背景
BitmapBJ:=TBitmap.Create();
BitmapBJ.LoadFromFile(GetcurrentDir+'\img\背景.bmp');
//載入自己制作的組合圖樣資料
Load_Bmap := TBitmap.Create;
Load_Bmap.LoadFromFile(GetCurrentDir + '\img\Pat_Sample9.bmp');
//儲存背景用點陣圖
//儲存描繪用點陣圖
Make_Bmap:=TBitmap.Create;
Make_Bmap.Width:=DYoko;
Make_Bmap.Height:=DTate;
//cmSrcCopy方式將復制源點陣圖復制起來 (默認值)
Make_Bmap.Canvas.CopyMode := cmSrcCopy;
for Y := 0 to (Tate - 1) do
for X := 0 to (Yoko - 1) do
begin
P := Ptmap2[X,Y];
//樣圖中一行中的的第幾個
PX := (P mod 8) * 32;
//樣圖中的第PY行
PY := (P div 8) * 32;
//畫背景地圖
Rect_L := Rect(PX, PY, PX + 32, PY + 32);
Rect_M := Rect(X * 32, Y * 32, X * 32 + 32, Y * 32 + 32);
Make_Bmap.Canvas.CopyRect(Rect_M, Load_Bmap.Canvas, Rect_L);
end;
MainForm.Canvas.CopyMode := cmSrcCopy;
MainForm.Canvas.Draw(0, 0, Make_Bmap);
//初始化定義敵方坦克出現標志都為0
for Cn:=0 to (MaxETank-1) do
begin
Etank[Cn].Used:=0;
end;
end;
procedure TMainForm.FormPaint(Sender: TObject);
begin
//畫面的再描繪
Canvas.Draw(0,0,BitmapBJ);
//Canvas.Draw(176,128,BitmapGameover);
MainForm.Canvas.CopyMode:=cmSrcCoPy;
MainForm.Canvas.Draw(0,0,Make_Bmap);
//GameOver時所產生的畫面
MainForm.Canvas.Draw(176,128,BitmapGameover);
end;
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
//將載入用與描繪用的點陣圖釋放掉
Load_Bmap.Free;
//Back_Bmap.Free;
Make_Bmap.Free;
end;
//計時器事件是本程序的核心,坦克的開動,發射子彈,碰撞檢測,爆炸都要用到
procedure TMainForm.Timer1Timer(Sender: TObject);
var
X, Y,Cn:Byte;
begin
// 如果游戲還沒有過關
if(gsSuccess=false) then
begin
//碰撞檢測與子彈的行動管理
HitCk;
//將所有的角色繪制至Make_Bmap
for Cn:=0 to (MaxETank-1) do
begin
//如果游戲為開始狀態則進入敵方坦克的控制
if(gsPlaying=true)and(Etank[Cn].Used=1)then
begin
//進入敵方坦克控制模塊
Etanks(Etank[Cn]);
//可以進行角色的繪制 敵人坦克 物品等//進行敵人坦克的繪制
DrawEtank(Etank[Cn].Sban,Etank[Cn].Xpos,Etank[Cn].Ypos);
end;
//進行我方坦克的繪制
if(gsPlaying=true)and (Mytank[0].Used=1)then
begin
//用黑色背景先把我方坦克原來位置覆蓋掉,我方坦克然后再改變坐標
//必須在響應按鍵時首先將原來坦克用黑色背景覆蓋掉
Mytanks(Mytank[0]);
//利用畫敵方坦克函數畫我方坦克
DrawEtank(Mytank[0].Sban,Mytank[0].Xpos,Mytank[0].Ypos);
end;
//調用我方坦克子彈函數
MyBullets;
//調用敵人坦克子彈函數
Ebullets;
//畫出敵人子彈
DrawEBullets;
//如果敵方坦克為爆炸標志
if Etank[Cn].Used=255 then
begin
// 調用坦克爆炸聲音
sndPlaySound( 'bomb.wav', SND_ASYNC);
//敵人坦克消失標志
Etank[Cn].Used:=0;
//把敵人坦克與子彈碰撞時坦克的坐標賦值給全局變量
X4:=Etank[Cn].Xpos;
Y4:=Etank[Cn].Ypos;
//調用爆炸開始圖第一張爆炸圖
UseMap:=0;
//把定時器2開啟調用爆炸畫面
Timer2.Enabled:=true;
//執行爆炸程序
//for n:=0 to 4 do
//Explode(ExplodeMap[0],Etank[Cn].Xpos,Etank[Cn].Ypos);
//Explode(ExplodeMap[1],Etank[Cn].Xpos,Etank[Cn].Ypos);
//Explode(ExplodeMap[2],Etank[Cn].Xpos,Etank[Cn].Ypos);
//Explode(ExplodeMap[3],Etank[Cn].Xpos,Etank[Cn].Ypos);
//Explode(ExplodeMap[4],Etank[Cn].Xpos,Etank[Cn].Ypos);
// Explode(ExplodeMap[5],Etank[Cn].Xpos,Etank[Cn].Ypos);
// BNum:=BNum+1;
// end;
end;
//如果我方坦克為爆炸標志
if Mytank[0].Used=255 then
begin
// 調用爆炸聲音
sndPlaySound( 'bomb.wav', SND_ASYNC);
//執行爆炸程序
//for n:=0 to 4 do
//Explode(ExplodeMap[n],Etank[Cn].Xpos,Etank[Cn].Ypos);
Explode(ExplodeMap[4],Mytank[0].Xpos,Mytank[0].Ypos);
//我方坦克消失標志
Mytank[0].Used:=0;
end;
end;
//將繪制用點陣圖顯示在Form上
MainForm.Canvas.CopyMode := cmSrcCopy;
Rect_M:=Rect(0,0,DYoko,DTate);
Rect_D:=Rect(0,0,DYoko,DTate);
MainForm.Canvas.CopyRect(Rect_D,Make_Bmap.Canvas,Rect_M);
//如果游戲還沒過關;并且游戲在進行狀態并且敵人的坦克被全部消滅used都為0則游戲勝利
//其中Timer2.Enabled=false是為了讓敵人坦克的最后一輛爆炸結束后再調用過關畫面
if (gsSuccess=false)and(gsPlaying=true)and(Etank[0].Used=0)and (Etank[1].Used=0)
and(Etank[2].Used=0)and(Timer2.Enabled=false) then
begin
//過關成功標志設為true
gsSuccess:=true;
if gsSuccess=true then
begin
sleep(2000);
//調用游戲勝利聲音
sndPlaySound( 'StageWin.WAV', SND_ASYNC);
BitmapSuccess:=TBitmap.Create();
BitmapSuccess.LoadFromFile(GetcurrentDir+'\img\過關圖片.bmp');
Canvas.Draw(0,0,BitmapSuccess);
sleep(10000);
//重新開始畫圖畫 //進入第二關
for Y := 0 to (Tate - 1) do
for X := 0 to (Yoko - 1) do
begin
P := Ptmap3[X,Y];
Grid[X,Y]:=P;
PX := (P mod 8) * 32;
PY := (P div 8) * 32;
//畫背景地圖
Rect_L := Rect(PX, PY, PX + 32, PY + 32);
Rect_M := Rect(X * 32, Y * 32, X * 32 + 32, Y * 32 + 32);
Make_Bmap.Canvas.CopyRect(Rect_M, Load_Bmap.Canvas, Rect_L);
end;
MainForm.Canvas.CopyMode := cmSrcCopy;
MainForm.Canvas.Draw(0, 0, Make_Bmap);
//調用第二關開始聲音
sndPlaySound( 'Start.WAV', SND_ASYNC);
// 游戲開始狀態下能進行鍵盤控制坦克
//并且敵方坦克也開始移動
//游戲開始把游戲開始標志設為ture
gsPlaying:=true;
//三輛敵方坦克最初出現的位置
Etank[0].Xpos:=0;
Etank[0].Ypos:=0;
Etank[0].Used:=1;
Etank[1].Xpos:=288;
Etank[1].Ypos:=0;
Etank[1].Used:=1;
Etank[2].Xpos:=544;
Etank[2].Ypos:=0 ;
Etank[2].Used:=1;
//我方坦克初始化
Mytank[0].Xpos:=224;
Mytank[0].Ypos:=416;
Mytank[0].Used:=1;
Mytank[0].Sban:=20;
Mytank[0].Smov:=3;
//先把我方坦克子彈使用設為可以發射子彈
Mybullet[0].Used:=4;
//把敵方坦克子彈使用設為可以發射子彈
Ebullet[0].Used:=4;
Ebullet[0].Xpos:=-16;
Ebullet[0].Ypos:=-16;
Ebullet[1].Used:=4;
Ebullet[1].Xpos:=-16;
Ebullet[1].Ypos:=-16;
Ebullet[2].Used:=4;
Ebullet[2].Xpos:=-16;
Ebullet[2].Ypos:=-16;
// Count:=0;
//游戲剛開始把過關標志設為false
gsSuccess:=false;
//游戲剛開始把游戲失敗標志設為false
gsOver:=false;
//gsSuccess:=false;
end;
end;
//如果我方坦克被敵人子彈擊中則Mytank[0].used=0;則游戲結速
if (Mytank[0].used=0)and(gsPlaying=true)then
begin
gsSuccess:=True;
if gsSuccess=true then
begin
BitmapGameover:=TBitmap.Create();
BitmapGameover.LoadFromFile(GetcurrentDir+'\img\Gameover.bmp');
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -