亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#帶你玩掃雷(附源碼)

發布時間:2020-09-22 03:05:35 來源:腳本之家 閱讀:167 作者:Cosecant 欄目:編程語言

掃雷游戲,大家都應該玩過吧!其實規則也很簡單,可是我們想自己實現一個掃雷,我們應該怎么做呢?

C#帶你玩掃雷(附源碼)

Step1: 知曉游戲原理

掃雷就是要把所有非地雷的格子揭開即勝利;踩到地雷格子就算失敗。游戲主區域由很多個方格組成。使用鼠標左鍵隨機點擊一個方格,方格即被打開并顯示出方格中的數字;方格中數字則表示其周圍的8個方格隱藏了幾顆雷;如果點開的格子為空白格,即其周圍有0顆雷,則其周圍格子自動打開;如果其周圍還有空白格,則會引發連鎖反應;在你認為有雷的格子上,點擊右鍵即可標記雷;如果一個已打開格子周圍所有的雷已經正確標出,則可以在此格上同時點擊鼠標左右鍵以打開其周圍剩余的無雷格。

1代表1的上下左右及斜角合計有一顆雷,依次輪推,2則有2顆,3則有3顆..

在確實是炸彈的方格上點了旗子,就安全了,不是炸彈的被點了旗子,后面會被炸死的..問號就先不確定這里有沒有炸彈,不會存在點錯了被炸死的狀況..

Step2: 由step1可知,游戲由格子組成,翻譯成代碼語言就叫做數組,也就是游戲地圖就是一個二維數組。格子對象,格子的值即當前雷的數量,那么此時我們暫定雷的數字標識為-1。除此之外,格子對象還有是否被顯示,顯示當前雷數量等屬性,那么我們大概可以定義這樣一個類:

 public class CellBlockRole
  {
    /// <summary>
    /// 位于游戲地圖中的坐標點X
    /// </summary>
    public int X { get; set; }

    /// <summary>
    /// 位于游戲地圖中的坐標點Y
    /// </summary>
    public int Y { get; set; }

    /// <summary>
    /// 是否展示最后格子所代表的結果
    /// </summary>
    public bool IsShowResult { get; set; } = false;

    /// <summary>
    /// 是否計算數字結果
    /// </summary>
    public bool IsComputeResult { get; set; } = false;

    /// <summary>
    /// 是否已經展示過計算結果了
    /// </summary>
    public bool IsHasShowComputed { get; set; } = false;

    /// <summary>
    /// 當前的格子的角色數字, -1:地雷,其他當前雷的數量
    /// </summary>
    public int Number { set; get; } = 0;

    /// <summary>
    /// 是否被Flag標識
    /// </summary>
    public bool IsFlag { get; set; } = false;

    /// <summary>
    /// 是否是雷
    /// </summary>
    public bool IsBoom => Number == -1;

  }

繪制游戲UI畫面,見代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using SweeperLibrary.Properties;
using Timer = System.Threading.Timer;

namespace SweeperLibrary
{
  public delegate void OnGameOverDelegate();

  public delegate void OnShowANumberDelegate();

  public delegate void OnPublishGameTimeDelegate(string timeDescription);

  public partial class GameView : UserControl
  {

    /// <summary>
    /// 游戲結束事件
    /// </summary>
    public event OnGameOverDelegate OnGameOverEvent;

    /// <summary>
    /// 當一個格子被點擊時,顯示當前數字的事件
    /// </summary>
    public event OnShowANumberDelegate OnShowANumberEvent;

    /// <summary>
    /// 發布當前游戲的時間
    /// </summary>
    public event OnPublishGameTimeDelegate OnPublishGameTimeEvent;

    /// <summary>
    /// 游戲繪制地圖的每個格子的大小
    /// </summary>
    public static readonly int CellSize = 40;

    /// <summary>
    /// 游戲規模N*N
    /// </summary>
    public static readonly int GameCellCount = 10;

    /// <summary>
    /// 移動方向坐標點改變的數組
    /// </summary>
    public static readonly int[][] MoveDirectionPoints = {
      new[]{-1, -1},
      new[] {0, -1},
      new[] {1, -1},
      new[] {1, 0},
      new[] {1, 1},
      new[] {0, 1},
      new[] {-1, 1},
      new[] {-1, 0}
    };

    /// <summary>
    /// 隨機數雷生成對象
    /// </summary>
    private static readonly Random random = new Random(Guid.NewGuid().GetHashCode());
    /// <summary>
    /// 游戲地圖標識數組
    /// </summary>
    private CellBlockRole[][] gameMap = new CellBlockRole[GameCellCount][];

    /// <summary>
    /// 雷的數量,默認為10
    /// </summary>
    public int BoomCount { get; set; } = 10;

    /// <summary>
    /// 游戲開始時間
    /// </summary>
    private DateTime gameStartTime;

    /// <summary>
    /// 計時定時器
    /// </summary>
    private System.Windows.Forms.Timer gameTimer = new System.Windows.Forms.Timer();

    public GameView()
    {
      InitializeComponent();
      SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
      SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      InitGame(); //默認游戲已經開始
      SetGameTimer(); //設置游戲定時器
    }

    private void GameView_Paint(object sender, PaintEventArgs e)
    {
      Width = GameCellCount + 1 + GameCellCount * CellSize;
      Height = GameCellCount + 1 + GameCellCount * CellSize;
      //繪制游戲界面
      Graphics graphics = e.Graphics;
      graphics.Clear(Color.WhiteSmoke);
      if (gameMap != null && gameMap.Length > 0 && gameMap[0] != null && gameMap[0].Length > 0)
      {
        for (int y = 0; y < GameCellCount; y++)
        {
          for (int x = 0; x < GameCellCount; x++)
          {
            int dx = x + 1 + x * CellSize,
              dy = y + 1 + y * CellSize;
            CellBlockRole cellBlockRole = gameMap[y][x];
            graphics.FillRectangle(new SolidBrush(cellBlockRole.IsShowResult ? Color.LightSlateGray : Color.WhiteSmoke),
              dx, dy, CellSize, CellSize);
            graphics.DrawRectangle(new Pen(Color.LightGray), dx, dy, CellSize, CellSize);
            if (cellBlockRole.IsShowResult && cellBlockRole.Number != 0)
            {
              switch (cellBlockRole.Number)
              {
                case -1: //雷
                  graphics.DrawImage(Image.FromHbitmap(Resources.boom.GetHbitmap()), new RectangleF(dx, dy, CellSize, CellSize));
                  break;
                default: //數字
                  string drawText = cellBlockRole.Number.ToString();
                  Font textFont = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold);
                  SizeF textSize = graphics.MeasureString(drawText, textFont);
                  graphics.DrawString(drawText, textFont, new SolidBrush(Color.White),
                    dx + (CellSize - textSize.Width) / 2, dy + (CellSize - textSize.Height) / 2);
                  break;
              }
            }
          }
        }
      }
    }

    private void GameView_MouseDown(object sender, MouseEventArgs e)
    {
      int px = (e.X - 1) / (CellSize + 1),
        py = (e.Y - 1) / (CellSize + 1);
      switch (e.Button)
      {
        case MouseButtons.Left: //鼠標左鍵
          if (!gameMap[py][px].IsShowResult)
          {
            if (gameMap[py][px].IsBoom)
            {
              new Thread(() =>
              {
                ShowAllCellBlockRoleNumber();
                if (this.InvokeRequired)
                {
                  MethodInvoker del = Invalidate;
                  this.Invoke(del);
                } else
                {
                  Invalidate();
                }
              }).Start();
              gameTimer.Stop();
              OnGameOverEvent?.Invoke();
            } else
            {
              new Thread(() =>
              {
                ShowNeiborhoodCellRolesByPosi(px, py);
                if (this.InvokeRequired)
                {
                  MethodInvoker del = Invalidate;
                  this.Invoke(del);
                } else
                {
                  Invalidate();
                }
              }).Start();
              OnShowANumberEvent?.Invoke();
            }
          }
          break;
        case MouseButtons.Right: //鼠標右鍵
          break;
      }
    }

    /// <summary>
    /// 初始化游戲
    /// </summary>
    private void InitGame()
    {
      new Thread(() =>
      {
        InitGameMap();
        GenerateBooms();
        if (this.InvokeRequired)
        {
          MethodInvoker del = Invalidate;
          this.Invoke(del);
        } else
        {
          Invalidate();
        }
      }).Start();
    }

    /// <summary>
    /// 設置游戲定時器
    /// </summary>
    private void SetGameTimer()
    {
      gameTimer.Interval = 1000;
      gameTimer.Enabled = true;
      gameTimer.Tick += (sender, args) =>
      {
        long dMillisecond = DateTime.Now.Millisecond - gameStartTime.Millisecond;
        long hour = dMillisecond / 60 / 60 / 1000;
        long minute = (dMillisecond - hour * (60 * 60 * 1000)) / (60 * 1000);
        long second = ((dMillisecond - hour * (60 * 60 * 1000)) % (60 * 1000)) / 1000;
        OnPublishGameTimeEvent?.Invoke((hour > 0 ? (hour > 9 ? hour.ToString() : "0" + hour) + ":" : "")
                        + (minute > 9 ? minute.ToString() : "0" + minute) + ":" + (second > 9 ? second.ToString() : "0" + second));
      };
    }

    /// <summary>
    /// 初始化游戲地圖
    /// </summary>
    private void InitGameMap()
    {
      for (int i = 0; i < GameCellCount; i++)
      {
        gameMap[i] = new CellBlockRole[GameCellCount];
        for (int j = 0; j < GameCellCount; j++)
        {
          gameMap[i][j] = new CellBlockRole
          {
            X = j,
            Y = i
          };
        }
      }
      gameStartTime = DateTime.Now;
      gameTimer.Start();
    }

    /// <summary>
    /// 重置游戲地圖
    /// </summary>
    public void ResetGameMap()
    {
      new Thread(() =>
      {
        for (int i = 0; i < GameCellCount; i++)
        {
          for (int j = 0; j < GameCellCount; j++)
          {
            gameMap[i][j].X = j;
            gameMap[i][j].Y = i;
            gameMap[i][j].Number = 0;
            gameMap[i][j].IsShowResult = false;
            gameMap[i][j].IsComputeResult = false;
            gameMap[i][j].IsHasShowComputed = false;

          }
        }
        GenerateBooms(); //生成一些雷
        if (this.InvokeRequired)
        {
          MethodInvoker del = Invalidate;
          this.Invoke(del);
        } else
        {
          Invalidate();
        }
      }).Start();
      gameStartTime = DateTime.Now;
      gameTimer.Start();
    }

    /// <summary>
    /// 隨機生成一些地雷
    /// </summary>
    public void GenerateBooms()
    {
      for (int i = 0; i < BoomCount; i++)
      {
        int boomNumberIndex = random.Next(0, GameCellCount * GameCellCount - 1); //生成隨機數的范圍
        int boomX = boomNumberIndex % GameCellCount,
          boomY = boomNumberIndex / GameCellCount;
        if (gameMap[boomY][boomX].Number == 0)
          gameMap[boomY][boomX].Number = -1; //-1表示雷
        else // 已經存在雷了,所以要重新處理
          i--;
      }
      MakeAllNumberComputeInCellRole(0, 0); //默認從坐標(0,0)開始
    }

    /// <summary>
    /// 顯示所有的格子的信息
    /// </summary>
    private void ShowAllCellBlockRoleNumber()
    {
      for (int i = 0; i < GameCellCount; i++)
      {
        for (int j = 0; j < GameCellCount; j++)
        {
          gameMap[i][j].IsShowResult = true;
        }
      }
    }

    /// <summary>
    /// 顯示某點周邊所有格子的數字
    /// </summary>
    /// <param name="posiX">X軸坐標</param>
    /// <param name="posiY">Y軸坐標</param>
    private void ShowNeiborhoodCellRolesByPosi(int posiX, int posiY)
    {
      gameMap[posiY][posiX].IsShowResult = true;
      gameMap[posiY][posiX].IsHasShowComputed = true;
      int boomCount = GetBoomCountInNeiborhood(posiX, posiY);
      if (boomCount == 0) //如果周圍沒有雷,則翻開所有8個方向的相關數字
      {
        for (int i = 0; i < MoveDirectionPoints.Length; i++)
        {
          int[] itemPosi = MoveDirectionPoints[i];
          int rx = posiX + itemPosi[0],
            ry = posiY + itemPosi[1];
          bool isNotOutIndexRange = rx >= 0 && rx < GameCellCount && ry >= 0 && ry < GameCellCount;
          if (isNotOutIndexRange) //防止坐標溢出
          {
            gameMap[ry][rx].IsShowResult = true;
            if (!gameMap[ry][rx].IsHasShowComputed && gameMap[ry][rx].Number == 0)
              ShowNeiborhoodCellRolesByPosi(rx, ry);
          }
        }
      }
    }

    /// <summary>
    /// 獲取某點附近的雷數量
    /// </summary>
    /// <param name="posiX">X軸坐標點</param>
    /// <param name="posiY">Y軸坐標點</param>
    /// <returns></returns>
    private int GetBoomCountInNeiborhood(int posiX, int posiY)
    {
      int boomCount = 0;
      for (int i = 0; i < MoveDirectionPoints.Length; i++)
      {
        int[] itemPosi = MoveDirectionPoints[i];
        int rx = posiX + itemPosi[0],
          ry = posiY + itemPosi[1];
        bool isNotOutIndexRange = rx >= 0 && rx < GameCellCount && ry >= 0 && ry < GameCellCount;
        if (isNotOutIndexRange && gameMap[ry][rx].IsBoom) //防止坐標溢出
        {
          boomCount++;
        }
      }
      return boomCount;
    }

    /// <summary>
    /// 計算每個格子的數字標識
    /// </summary>
    /// <param name="posiX">X軸坐標</param>
    /// <param name="posiY">Y軸坐標</param>
    private void MakeAllNumberComputeInCellRole(int posiX, int posiY)
    {
      int boomCount = GetBoomCountInNeiborhood(posiX, posiY);
      if (boomCount != 0) //如果周圍沒有雷,則計算周圍的8個方向的格子
      {
        gameMap[posiY][posiX].Number = boomCount;
      } else
      {
        if (!gameMap[posiY][posiX].IsBoom)
          gameMap[posiY][posiX].Number = 0;
      }
      gameMap[posiY][posiX].IsComputeResult = true;
      for (int i = 0; i < MoveDirectionPoints.Length; i++)
      {
        int[] itemPosi = MoveDirectionPoints[i];
        int rx = posiX + itemPosi[0],
          ry = posiY + itemPosi[1];
        bool isNotOutIndexRange = rx >= 0 && rx < GameCellCount && ry >= 0 && ry < GameCellCount;
        if (isNotOutIndexRange && !gameMap[ry][rx].IsComputeResult && !gameMap[ry][rx].IsBoom) //防止坐標溢出
        {
          MakeAllNumberComputeInCellRole(rx, ry);
        }
      }
    }

  }

}

主要代碼已經實現,現已知現有代碼的定時器由問題,暫時不支持Flag(旗子標識)。當然代碼中還有其他不足的地方,游戲持續優化中。。。

源代碼地址:MineSweeper-CShape_jb51.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

老河口市| 南充市| 东海县| 临漳县| 洪湖市| 临夏县| 溧阳市| 达日县| 临泽县| 三明市| 伊春市| 乌什县| 霍山县| 理塘县| 清徐县| 崇左市| 邓州市| 芦山县| 荆州市| 南郑县| 塔河县| 灵璧县| 浮梁县| 濉溪县| 原平市| 鄂尔多斯市| 石楼县| 会东县| 平顶山市| 赣州市| 吉安市| 浮山县| 馆陶县| 汉中市| 龙州县| 邹平县| 手游| 南丰县| 赤城县| 孟津县| 轮台县|