using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
namespace winmine
{
public class Map
{
public static int Time = 0;
public static bool GameOver = false;
public static bool IsSuccess = false;
public static int UserMineCount = 0;
public static int Width = 17 * 10;
public static int Height = 17 * 10;
public static int BlockWidth = 16;
public static int MineNumber = 10;
public static int RowCount = 9;
public static int ColumnCount = 9;
public static int MainMarked = 0;
public static int BlockOpen = 0;
public static List<BlockData> BlockDataList = new List<BlockData>();
static Map()
{
Init();
}
public static void Init()
{
GameOver = false;
Time = 0;
Height = RowCount * BlockWidth;
Width = ColumnCount * BlockWidth;
}
static void OpenUnit(BlockData bd)
{
if (!bd.IsMine)
{
if (bd.State == BlockState.Open || bd.State == BlockState.UserMine)
{
return;
}
bd.State = BlockState.Open;
BlockGraphics.DrawBlock(bd);
BlockOpen++;
if (bd.MineCount == 0)
{
foreach (BlockData bd1 in bd.BlockList)
{
OpenUnit(bd1);
}
}
}
}
public static void Showall()
{
foreach (BlockData bd in Map.BlockDataList)
{
if (bd.State == BlockState.Closed)
{
bd.State = BlockState.Open;
}
}
}
public static void DrawBlocks()
{
if (!Map.GameOver)
foreach (BlockData bd in Map.BlockDataList)
{
BlockGraphics.DrawBlock(bd);
}
else
{
foreach (BlockData bd in Map.BlockDataList)
{
if (!bd.IsMine && bd.State == BlockState.UserMine)
{
BlockGraphics.DrawBlock(bd.x, bd.y, BlockGraphics.MineBlastMap);
}
else if (bd.IsMine)
{
BlockGraphics.DrawBlock(bd.x, bd.y, BlockGraphics.MineMap);
}
else
{
BlockGraphics.DrawBlock(bd);
}
}
}
}
public static bool IsSuccessful()
{
if (Map.UserMineCount == 0)
{
int i = 0;
for (i = 0; i < Map.BlockDataList.Count; i++)
{
if (!Map.BlockDataList[i].IsMine && Map.BlockDataList[i].State == BlockState.UserMine)
{
break;
}
}
if (i < Map.BlockDataList.Count)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
public static void SingleClick(int x, int y)
{
BlockData bd = GetBlock(x, y);
if (GameOver)
{
return;
}
if (bd != null && bd.IsMine)
{
GameOver = true;
Showall();
DrawBlocks();
}
else
{
OpenUnit(bd);
}
if (IsSuccessful())
{
Map.IsSuccess = true;
}
}
public static BlockData GetBlock(int x, int y)
{
if (x >= Map.ColumnCount * Map.BlockWidth || y > Map.RowCount * Map.BlockWidth)
{
return null;
}
int index = (y / Map.BlockWidth) * Map.ColumnCount + (x / Map.BlockWidth) % Map.ColumnCount;
if (index < BlockDataList.Count)
{
return BlockDataList[index];
}
else
{
return null;
}
}
public static int GetBlockIndex(int x, int y)
{
if (x >= Map.ColumnCount * Map.BlockWidth || y > Map.RowCount * Map.BlockWidth)
{
return -1;
}
int index = (y / Map.BlockWidth) * Map.ColumnCount + (x / Map.BlockWidth) % Map.ColumnCount;
if (index < BlockDataList.Count)
{
return index;
}
else
{
return -1;
}
}
}
public enum BlockState
{
Closed, Open, UserMine
}
public class BlockGraphics
{
public static Graphics G;
public static Graphics GG;
public static Bitmap MemImage;
public static Font DrawFont = new Font("Arial", 12, FontStyle.Bold);
public static Bitmap ClosedMap;//Map for the closed state
public static Bitmap OpenMap;//Map for the open state
public static Bitmap BlankOpenMap;//Map for the blank state
public static Bitmap UserMineMap;//Map for the mine user marked
public static Bitmap MineBlastMap;//Map for the BlastMine
public static Bitmap MineMap;//Map for the Mine
static BlockGraphics()
{
ClosedMap = new Bitmap((Image)new Bitmap("image/closed.gif"), new Size(Map.BlockWidth, Map.BlockWidth));
OpenMap = new Bitmap((Image)new Bitmap("image/open.gif"), new Size(Map.BlockWidth, Map.BlockWidth));
BlankOpenMap = new Bitmap((Image)new Bitmap("image/open.gif"), new Size(Map.BlockWidth, Map.BlockWidth));
UserMineMap = new Bitmap((Image)new Bitmap("image/usermine.gif"), new Size(Map.BlockWidth, Map.BlockWidth));
MineBlastMap = new Bitmap((Image)new Bitmap("image/MineBlast.gif"), new Size(Map.BlockWidth, Map.BlockWidth));
MineMap = new Bitmap((Image)new Bitmap("image/Mine.gif"), new Size(Map.BlockWidth, Map.BlockWidth));
}
public static void DrawBlock(int x, int y, Bitmap b)
{
if (G != null)
{
G.DrawImage(b, x, y);
GG.DrawImage(b, x, y);
}
}
public static void DrawBlock(BlockData bd)
{
Bitmap b = BlockGraphics.BlankOpenMap;
switch (bd.State)
{
case BlockState.Closed: { b = BlockGraphics.ClosedMap; break; }
case BlockState.Open: { b = BlockGraphics.OpenMap; break; }
case BlockState.UserMine: { b = BlockGraphics.UserMineMap; break; }
}
DrawBlock(bd.x, bd.y, b);
if (bd.State == BlockState.Open && bd.MineCount != 0)
{
StringFormat drawFormat = new StringFormat();
SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Blue);
switch (bd.MineCount)
{
case 1: { drawBrush = new SolidBrush(System.Drawing.Color.Blue); break; }
case 2: { drawBrush = new SolidBrush(System.Drawing.Color.Green); break; }
case 3: { drawBrush = new SolidBrush(System.Drawing.Color.Red); break; }
case 4: { drawBrush = new SolidBrush(System.Drawing.Color.Red); break; }
case 5: { drawBrush = new SolidBrush(System.Drawing.Color.Blue); break; }
case 6: { drawBrush = new SolidBrush(System.Drawing.Color.Blue); break; }
case 7: { drawBrush = new SolidBrush(System.Drawing.Color.Blue); break; }
case 8: { drawBrush = new SolidBrush(System.Drawing.Color.Blue); break; }
}
G.DrawString(bd.MineCount.ToString(), DrawFont, drawBrush, bd.x, bd.y, drawFormat);
GG.DrawString(bd.MineCount.ToString(), DrawFont, drawBrush, bd.x, bd.y, drawFormat);
}
}
public BlockGraphics()
{
}
}
public class BlockData
{
public int ID = 0;
public int x;
public int y;
public int MineCount = 0;//The mine count around it
public List<BlockData> MineList = new List<BlockData>();
public List<BlockData> BlockList = new List<BlockData>();
public int UserMineCount = 0;//The mine count around it which user marked
public BlockState State = BlockState.Closed;
public bool IsMine = false;
public BlockData(int x, int y, int MineCount)
{
this.x = x;
this.y = y;
this.MineCount = MineCount;
}
public BlockData(int x, int y)
{
this.x = x;
this.y = y;
}
}
}
分享到:
相关推荐
c#扫雷小程序,适用于交作业时,简单易懂。便于学习,适用于初学者。
自己编的一个小程序——扫雷,背景是优美的风景,游戏设置三个关卡。
c#写的和windows里自带的一样的扫雷游戏
用c#语言做的简单的扫雷小游戏,内容简单,代码简易,逻辑清晰,可供c#初学者制作参考,相信对初学者有所帮助。
该压缩包是c#wifrom窗体扫雷的代码,可供参考学习。。
winform 扫雷游戏,想要学习绘图或做小游戏的朋友可以下载来参考看看,有代码注释,如有疑问,欢迎询问
卓越的扫雷程序,经典程序 扫雷程序 C#程序 window应用窗体程序 免费下载 无限量下载
C#经典扫雷程序
C#简单制作扫雷窗体控件。
C#制作的高仿windows自带的扫雷,实现了双击功能,自定义功能,带英雄榜,适合C#初学者
一个windoms窗体程序 基本上实现了微软自带的扫雷
扫雷是一个宽益智游戏,对于大多数电脑玩家和普通朋友来说,扫雷可能是电脑入门游戏之一。 编这个游戏主要是练习可视化开工具的一些基本操作,下面说一下这个游戏的思想和难点。如果大家在玩的时候发现错误,或者有...
仿照windows里的扫雷。运用到了动态控件
自己写的C#开发的扫雷小游戏_仿照WINDOWS系统扫雷,很简单适合初学者
《扫雷》是一款大众类的益智小游戏。游戏目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。
无其他附带功能,结构、界面比较单一,附源码供参考 建议人群:.net初学者
C#扫雷源码 面向对象 基于C#开发的窗体应用程序
基于c#的仿xp扫雷游戏,源代码都有,基本功能均实现!大家共同学习
希望能有帮助,这是一个带数据库的扫雷程序
1)FrmMain 是主控窗体,负责项目的启动和关闭;并协调内部各个组建模块的协调工作。 2)CrlMineField是雷区的封装,是游戏的核心组建;它负责方格的布局以及地雷的分布;并控制玩家的基本操作以及正确的响应。 3)...