在delphi中创建一个暂停菜单

时间:2018-01-22 11:14:11

标签: delphi

背景:我创造了一个可玩的迷宫游戏,但是当迷宫变得更大时,出现错误的机会更多,并且需要很长时间才能再次到达迷宫的开始,我已被告知&#39真的很烦人。

问题:我想创建一个暂停游戏功能,当用户按下按钮键时,会弹出一个菜单,允许用户返回主菜单或重启迷宫。

我很少知道在代码中从哪里开始这个,因为我不想搞砸已经添加的任何东西。

我已经添加了绘制迷宫//创建迷宫, 初始化访问和走动//移除迷宫的墙壁, 表格键下移//移动播放器 和定时器//碰撞检测程序如下。

希望有人可以提供帮助吗?

非常感谢。

`procedure TfrmMazeDesign.draw_maze(square_size: integer);
      var
        row, col : integer;


      begin
         // setup default walls as "WWWW" wall on all four sides.
        SetLength(Shapes,height+3, width+3, 2 ) ;

        for row := 1 to  height do
            begin
                for col := 1 to width+2 do
                  begin

                      Shapes[row,col,0]:= TShape.Create(Self);
                      Shapes[row,col,0].Parent := Self;

                      with Shapes[row,col,0] do
                      begin
                        Width := 5;
                        Height := square_size;
                        Left := 100+ ((col-1) * square_size);
                        Top := 50+ ((row-1) * square_size);
                        Brush.Color := RGB(255, 255,25);
                        Shape := stRectangle;
                      end;


                  end;
            end;


        for row := 1 to height+1  do
            begin
                for col := 1 to width+1 do
                  begin

                      Shapes[row,col,1]:= TShape.Create(Self);
                      Shapes[row,col,1].Parent := Self;

                      with Shapes[row,col,1] do
                      begin
                        Width := square_size;
                        Height := 5;
                        Left := 100+ ((col-1) * square_size);
                        Top :=  50+ ((row-1) * square_size);
                        Brush.Color := RGB(255, 255,25);
                        Shape := stRectangle;
                      end;

                 end;
          end;


      end;




procedure  TfrmMazeDesign.initialise_visited(var visited: Tvisited);

var
  row, col: integer;


begin

  for  row := 0 to height+2 do
    for col := 0 to width+2  do
      if (col = 0) or (row = 0) or (row = height+1) or (col= width+2) then
        visited[row,col] := True
      else
        visited[row,col] := False;



end;




procedure TFrmMazeDesign.walk(visited: Tvisited; x: integer; y:integer);
  var
    xx,yy, counter, ran_direction: integer;
    direction: Tstringlist;  //  1= Up, 2= right, 3 = down, 4 = left
    text: string;


begin
   visited[x,y] := True;
   direction  := TStringlist.Create;
   direction.Add('1');
   direction.Add('2');
   direction.Add('3');
   direction.Add('4');
   for counter := direction.Count - 1 downto 0 do
      direction.Exchange(counter, Random(counter+1));



   for counter := 0 to direction.Count-1 do
      begin
       ran_direction := StrtoInt(direction[counter]);
       if ran_direction= 1 then
          begin
            xx := x-1;
            yy := y
          end;
       if ran_direction = 2 then
          begin
            xx := x+1;
            yy := y
          end;
       if ran_direction = 3 then
          begin
            xx := x;
            yy := y-1
          end;
       if ran_direction= 4 then
          begin
            xx := x;
            yy := y+1
          end;


       if  visited[xx,yy] = False then
          begin
            if xx = x then
                shapes[x,Max(yy,y),0].visible := False;
            if yy = y then
                shapes[Max(xx,x),y,1].visible := False;
             walk (visited, xx,yy)
           end;
      end;

end;


procedure TfrmMazeDesign.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
  begin
case Key of
    VK_UP:
    direction := 1;
    VK_DOWN:
    direction := 2;
    VK_LEFT:
    direction := 3;
    VK_RIGHT:
    direction := 4;
  end;

end;

procedure TfrmMazeDesign.FormOpen(Sender: TObject);
var

  block_height: integer;
  xx,yy: integer;
  visited: Tvisited;
  monster, monster2: TMonster;

  Shape_height : integer;
  Shape_Width : integer;
  imgfinish_height : integer;
  maze_width : integer;


begin

  width := frmdifficulty.ScrollBar1.Position;
  height := frmdifficulty.ScrollBar1.Position;
  maze_width := 650;

  block_height := maze_width div width;
  Shape_height :=  round( block_height * 0.5);
  imgfinish_height := round(block_height * 0.5);

  draw_maze(block_height);
  SetLength(visited, height+10, width+10) ;
  initialise_visited(visited);
  walk(visited,3,3);
  ShpUser.height := Shape_height;
  ShpUser.Width  := Shape_height;
  imgfinish.width := Shape_height;
  imgfinish.height := shape_height;


  ShpUser.Shape := UShapeEditor.frmShape.shpShape.Shape;
  ShpUser.Brush.color := UShapeEditor.frmShape.shpShape.Brush.color;

  FDateTimeTo := StrToDateTime(FormatDateTime('dd' + FormatSettings.DateSeparator + 'mm' +
  FormatSettings.DateSeparator + 'yyyy', Now)); Incsecond(time,120);

  Timer1.Enabled := True;


  monster := TMonster.Create(Self);
  monster.Parent := Self;
  monster.SetSizes(width, height, shape_height, 55+ maze_width + 10,665);
  monster.start;

  monster2 := TMonster.Create(Self);
  monster2.Parent := Self;
  monster2.SetSizes(width, height, shape_height, 50+ maze_width + 10,565);
  monster2.start;

  end;

procedure TfrmMazeDesign.Timer1Timer(Sender: TObject);

var IntersectionRect: TRect;
    collision, test_collision : boolean;
    up : boolean;
    right : boolean;
    max_width, max_height : integer;
    xx, yy : integer ;
    aRect1: TRect;
    buttonSelected : Integer;
    collisionend: boolean;
    frmMazeDesign: TfrmMazeDesign;
    row, col, hoz_vert : integer;
begin

  hoz_vert := 0;

  xx := 0;
  yy := 0;

  case direction of
    1:begin        //Up
          yy := -3;
      end;
    2: begin       //down
          yy := + 3;
       end;

    3:  begin      //left
           xx := -3;
       end;

    4:  begin        //right
           xx := + 3;
        end;

  end;


  repeat
    if hoz_Vert = 0 then begin
      max_width := width +3;
      max_height := Height+1;
    end else begin
      max_width := width+2;
      max_height := Height+2;
    end;

    row := 1;
    repeat
      col := 1;


      repeat
        aRect1 := Rect(ShpUser.Left+xx, ShpUser.Top+yy, ShpUser.Left+ShpUser.width+xx, ShpUser.top+ ShpUser.Height+yy);
        if Shapes[row,col,hoz_vert].visible = True then
          collision :=  IntersectRect(IntersectionRect, aRect1, Shapes[row,col,hoz_vert].BoundsRect)  ;

        col := col + 1;

      until (collision) or (col = max_width);
      row := row + 1 ;
    until (collision) or (row = max_height);
    hoz_vert := hoz_vert +1;
  until (collision) or (hoz_vert = 2);

  if (collision = False) and (direction <> 0) then begin
    ShpUser.Top := ShpUser.Top + yy;
    ShpUser.Left := ShpUser.Left + xx;
  end;


      if IntersectRect(IntersectionRect, imgfinish.BoundsRect, Shpuser.BoundsRect) then
      begin
      Collisionend := true;
      if collisionend = true  then
       frmfinish.Show;
        direction:= 0;
      end;`

1 个答案:

答案 0 :(得分:1)

添加到程序中这是相对简单的,已经是事件驱动的。总之,您需要执行以下操作:

  1. 在游戏开始时添加Boolean标记,可能名为Paused,设置为False
  2. 当用户按下暂停按钮时,将Paused标记设为True
  3. 当用户按下继续按钮时,将Paused标记设置为False
  4. 在时间事件处理程序中,在更新位置之前检查Paused标志。如果Paused标志为True,则在执行任何其他操作之前,只需退出计时器事件处理程序。