⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitmain.pas

📁 经典游戏扫雷,仿XP
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit UnitMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ExtCtrls, ImgList, Buttons, StdCtrls, IniFiles;

type                                              
    Lei =RECORD
         IsLei, IsShow ,IsMark: bool;
         Num : Integer;
         END;

//-------------------------------------------------------------------------------
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    Game: TMenuItem;
    Start: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    Middle: TMenuItem;
    SetLei: TMenuItem;
    N7: TMenuItem;
    Elementary: TMenuItem;
    N9: TMenuItem;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Exit: TMenuItem;
    BackGround: TImage;
    StartButton: TSpeedButton;
    Timer1: TTimer;
    Num1: TImage;
    Num2: TImage;
    Num3: TImage;
    Lei1: TImage;
    Lei2: TImage;
    Mark: TMenuItem;
    ImageList1: TImageList;
    procedure ExitClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ElementaryClick(Sender: TObject);
    procedure BackGroundMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure MiddleClick(Sender: TObject);
    procedure BackGroundMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure StartButtonClick(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Timer1Timer(Sender: TObject);
    procedure SetLeiClick(Sender: TObject);
    procedure BackGroundDblClick(Sender: TObject);
    procedure StartClick(Sender: TObject);
    procedure MarkClick(Sender: TObject);
    procedure N7Click(Sender: TObject);
  private
    { Private declarations }
  public
    Level,LeiNum : Integer;
    Leis : Array of Array of Lei;
    LeftRect : Integer;
    IsFirst, IsStart : bool;
    Time : Integer;
    IsReady, IsShow : bool;
    SLNum : Integer;
    procedure CreateLei();
    procedure ShowLei();
    procedure ClearSpace(var x,y :integer);
    procedure InitLeis();
    procedure InitGround();
    procedure GameOver();
    procedure SetNum( N1,N2,N3:Integer);
    procedure ShowLeiNum(LN : Integer);
  end;
const
    ONLYGAME ='ONLY ONE APPLICATION CAN BE RUN AT THE SAME TIME';
var
  Form1: TForm1;
  MutexHandle : Thandle;

implementation
  uses UnitShowMark, UnitAbout;


{$R *.dfm}

procedure TForm1.ExitClick(Sender: TObject);
begin
    Form1.Close();
end;

procedure TForm1.FormCreate(Sender: TObject);
var
    IniFile : TIniFile;
begin
//------------------------------------------------------------------------------
//set only----------------------------------------------------------------------
    MutexHandle :=OpenMutex( MUTEX_ALL_ACCESS, false, ONLYGAME );
    if MutexHandle=0 then
        CreateMutex( nil, false, ONLYGAME )
    else
    Begin
        Application.ShowMainForm :=false;
        Application.Terminate;
    End;
//------------------------------------------------------------------------------
    Self.DoubleBuffered :=true;
    Form1.Width :=32*10-20;
    Form1.Height :=32*11+Panel2.Height;
    IsFirst :=true;
    IsStart :=false;
    Time :=0;
    IsReady :=false;
//------------------------------------------------------------------------------
    IniFile :=TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));

    IniFile.WriteString('First','Name', IniFile.ReadString('First','Name','匿名'));
    IniFile.WriteString('First','Mark', IniFile.ReadString('First','Mark','999'));
    IniFile.WriteString('Second','Name', IniFile.ReadString('Second','Name','匿名'));
    IniFile.WriteString('Second','Mark', IniFile.ReadString('Second','Mark','999'));
    IniFile.Free;


end;

procedure TForm1.ElementaryClick(Sender: TObject);
begin

    BackGround.Free;
    BackGround :=TImage.Create(self);
    BackGround.Parent :=Panel3;
    BackGround.Align :=Panel3.Align;
    BackGround.OnMouseDown :=BackGroundMouseDown;
    BackGround.OnMouseUp :=BackGroundMouseUp;
    BackGround.OnDblClick :=BackGroundDblClick;
    Level :=9;
    LeiNum :=10;
    LeftRect :=Level*Level;
    Form1.Width :=32*(Level+1)-20;
    Form1.Height :=32*(Level+2)+Panel2.Height-6;
    InitGround();
    InitLeis;
    CreateLei;
    SetNum(0,0,0);
    Elementary.Checked :=true;
    Middle.Checked :=false;
    SetLei.Checked :=false;
    SLNum :=LeiNum;
    ShowLeiNum(SLNum);
    IsStart :=false;
    IsShow :=false;
    SetLei.Enabled :=true;
    IsReady :=true;
  //  INit Everything and Get Read
end;

procedure TForm1.BackGroundMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
    BitMap :TBitMap;
    MyRect,OtherRect :TRect;
begin
    StartButton.Glyph.LoadFromFile('Img/Suprize Face.bmp');
    if(not IsStart and IsReady) then
    Begin
        IsStart :=true;
        SetLei.Enabled :=false;
    End;
if ( (x>=0) and ( x<=Level*32 ) and (y>=0) and (y<=Level*32) ) then
Begin

    x :=Trunc(x/32);
    y :=Trunc(y/32);
    MyRect :=Rect(x*32, y*32, (x+1)*32, (y+1)*32 );
    OtherRect :=Rect(0,0,32,32);
//------------------------------------------------------------------------------
    if not Leis[x][y].IsShow then
    Begin
        if Button =mbRight then
        Begin
            if not Leis[x][y].IsMark  then

            Begin
                if (SLNum>0) then
                Begin
                BitMap :=TBitMap.Create;
                BitMap.LoadFromFile('Img/ButtonFlag.bmp');
                BackGround.Canvas.CopyRect(MyRect,BitMap.Canvas,OtherRect);
                BitMap.Free;
                Leis[x][y].IsMark :=true;
                SLNum :=SLNum-1;
                ShowLeiNum(SLNum);
                End;
            End
            else
            Begin
                BitMap :=TBitMap.Create;
                BitMap.LoadFromFile('Img/Button1.bmp');
                BackGround.Canvas.CopyRect(MyRect,BitMap.Canvas,OtherRect);
                BitMap.Free;
                Leis[x][y].IsMark :=False;
                SLNum :=SLNum+1;
                ShowLeiNum(SLNum);
            End;
        End
        else  if( (not Leis[x][y].IsMark) and (Button =mbLeft) ) then
        Begin
            if (Leis[x][y].IsLei) then
            Begin
                BitMap :=TBitMap.Create;
                BitMap.LoadFromFile('Img/ButtonLei3.bmp');
                BackGround.Canvas.CopyRect(MyRect,BitMap.Canvas,OtherRect);
                Leis[x][y].IsShow :=true;
                BitMap.Free;
                GameOver;
            End
//-------------------------Show Num of Lei or clear empty area------------------
            else if ( Leis[x][y].Num >0 ) then
            Begin
                BitMap :=TBitMap.Create;
                BitMap.LoadFromFile('Img/Num'+IntToStr(Leis[x][y].Num)+'.bmp');
                BackGround.Canvas.CopyRect(MyRect,BitMap.Canvas,OtherRect);
                Leis[x][y].IsShow :=true;
                LeftRect :=LeftRect-1;
                BitMap.Free;
            End
            else
            Begin
                ClearSpace(x,y);
            End;
//---------------------End Show Num of Lei or clear empty area------------------
        End;
    End

End;
    if LeftRect =LeiNum then
        ShowLei;

//------------------------------------------------------------------------------
end;

procedure TForm1.CreateLei;
var
    i : Integer;
    x, y : Integer;
    N : Integer;
    Get : Bool;
begin
//    FillChar(Leis,sizeof(Leis),ord(false)*3 );
    Randomize;
    for i:=1 to LeiNum do
    Begin
        Get :=false;
        While(not Get)  do
        Begin
            Get :=true;
            x :=Trunc( Random(Level) );
            y :=Trunc( Random(Level) );
            if ( not Leis[x][y].IsLei ) then
            Begin
                Leis[x][y].IsLei :=true;
            End
            else
                Get :=false;
        End;

    End;
//--------------------------------------INitNum--------------------------------
    for x :=0 to Level-1 do
    Begin
        for y :=0 to Level-1 do
        Begin
            if( not Leis[x][y].IsLei ) then
            Begin
                for  i :=x-1 to x+1 do
                Begin
                    for N :=y-1 to y+1 do
                    Begin
                       if( (i>=0) and (i<=Level-1)  and  (n>=0) and (n<=Level-1) ) then
                            if (Leis[i][N].IsLei) then
                                Leis[x][y].Num :=Leis[x][y].Num+1;

                    End;
                End;
            End;
        End;
    End;
//-----------------------------------------------------------------------------
end;

procedure TForm1.ShowLei;
Var
    LeiRect,MapRect : TRect;
    BitMap : TBitMap;
    Row, Column : integer;
    IsDie : bool;
    WinnerRange : String;
    IniFile : TIniFile;
begin
    BitMap :=TBitMap.Create;
    IsDie :=false;
    MapRect :=Rect(0,0,32,32);
    for Row :=0 to Level-1 do
    Begin
        for Column :=0 to Level-1 do
        Begin
            if(Leis[Row][Column].IsLei) then
            Begin
                if(Leis[Row][Column].IsShow) then
                Begin
                    BitMap.LoadFromFile('Img/TounchLei.bmp');
                    IsDie :=true;
                End

                else
                BitMap.LoadFromFile('Img/ButtonLei3.bmp');
                LeiRect :=Rect(Row*32, Column*32, (Row+1)*32, (Column+1)*32 );
                BackGround.Canvas.CopyRect(LeiRect,BitMap.Canvas,MapRect);

            End;

        End;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -