GDI绘图写的简单扫雷
由于没话多少时间,这个扫雷我只实现了主要功能(扫雷功能,递归实现)
废话不多说,直接上代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace saolei
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} Graphics gs;
Random rand;
List<Point> leiPoint;//存放雷的坐标
List<Point> isSearch;//存放已扫描过得坐标
private void Form1_Load(object sender, EventArgs e)
{
leiPoint = new List<Point>();
rand = new Random();
} private void btnStart_Click(object sender, EventArgs e)
{
setLei();
isSearch = new List<Point>();//实例化扫描过得雷区
gs = pnlLei.CreateGraphics();//创建画图对象
gs.Clear(this.pnlLei.BackColor);//清除所有
for (int i = ; i < ; i+=)//循环绘制地图
for (int j = ; j < ; j+=)
gs.FillRectangle(new SolidBrush(Color.Gray), i - , j-, , ); foreach (Point p in leiPoint)//额外绘制地雷(可以注释掉)
gs.FillRectangle(new SolidBrush(Color.Purple), p.X-, p.Y-, , ); } //设置雷的方法
private void setLei()
{
for (int i = ; i < ; i++)//循环生成20的地雷
{
int x = rand.Next(, );
int y = rand.Next(, );
foreach (Point p in leiPoint)
{
if (p == new Point(x, y))
{
i--;
} continue;
}
leiPoint.Add(new Point(x * + , y * + ));
}
} //private Color getColor()
//{
// return Color.FromArgb(rand.Next(1, 255), rand.Next(1, 255), rand.Next(1, 255));
//} private void pnlLei_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//判断鼠标是否使用左键单击的panel控件
{
if (!isOk(e.Location.X, e.Location.Y))//判断是否是扫描过得雷区
{
isSearch.Add(new Point(e.Location.X, e.Location.Y));//加入扫描雷区
int x = e.Location.X / * + ;//将鼠标单击的坐标X调成最接近的哪一个雷区的坐标
int y = e.Location.Y / * + ;//将鼠标单击的坐标Y调成最接近的哪一个雷区的坐标
if (x > || x < -) return;//判断是否超出X轴界限(结束递归)
if (y > || y < -) return;//判断是否超出Y轴界限(结束递归)
int count = getLeiCount(x, y);//获取周围雷的个数
if (count > )//判断是否有雷(结束递归)
{
gs.DrawString(count.ToString(), new Font("楷体", ),//绘制雷的个数
new SolidBrush(Color.Red), x + , y + );
return;
}
gs.FillRectangle(new SolidBrush(Color.Blue), x - , y - , , );
pnlLei_MouseClick(sender, new MouseEventArgs(e.Button, , x + , y, ));//递归调用本次单击事件(向右扫描雷区)
pnlLei_MouseClick(sender, new MouseEventArgs(e.Button, , x - , y, ));//递归调用本次单击事件(向左扫描雷区)
pnlLei_MouseClick(sender, new MouseEventArgs(e.Button, , x, y + , ));//递归调用本次单击事件(向下扫描雷区)
pnlLei_MouseClick(sender, new MouseEventArgs(e.Button, , x, y - , ));//递归调用本次单击事件(向上扫描雷区) } }
} //后去周围8个方向雷的个数
private int getLeiCount(int x, int y)
{
int leiCount = ; foreach (Point p in leiPoint)
if (p.X >= x - && p.Y >= y - && p.X <= x + && p.Y <= y + )//循环判断周边是否有雷
leiCount++; return leiCount;
} //判断是否是已经扫描过得雷区
private bool isOk(int x, int y)
{
foreach (Point p in isSearch)
if (p == new Point(x, y))
return true;
return false;
}
}
}
很多不足之处,望各位博友多指教
GDI绘图写的简单扫雷的更多相关文章
- MFC GDI绘图基础
一.关于GDI的基本概念 什么是GDI? Windows绘图的实质就是利用Windows提供的图形设备接口GDI(Graphics Device Interface)将图形绘制在显示器上. 在Wind ...
- Windows GDI绘图基础知识
一.Windows可以画直线.椭圆线(椭圆圆周上的曲线)和贝塞尔曲线.////////////7 个画线函式是:(1)画直线LineTo BOOL LineTo(HDC hdc,int nXEn ...
- GDI+ 绘图闪烁解决方法
闲着没事,准备做一个类似于TeeChart的自定义控件,结果第一步的绘图就把我给难倒了,虽然早就知道GDI绘图的闪烁问题很坑,但是却没有想到如此之坑,折腾了两天,才找到解决方法. 首先在窗体加载的时候 ...
- 使用C语言实现二维,三维绘图算法(3)-简单的二维分形
使用C语言实现二维,三维绘图算法(3)-简单的二维分形 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其 ...
- GDI绘图中的映射模式CDC::SetMapMode()
原文链接:http://blog.csdn.net/charlessimonyi/article/details/8264572 在GDI绘图前,一般要设置映射模式.映射模式是什么呢?它是逻辑长度单位 ...
- iOSQuart2D绘图之UIImage简单使用
代码地址如下:http://www.demodashi.com/demo/11609.html 人生得意须尽欢,莫使金樽空对月. 天生我材必有用,千金散尽还复来. 前记 说到UIImage大家都不会感 ...
- canvas写个简单的小游戏
之前在HTML5 Canvas属性和方法汇总一文中,介绍过Canvas的各种属性以及方法的说明,并列举了自己写的一些Canvas demo,接下来开始写一个简单的小游戏吧,有多简单,这么说吧,代码不到 ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- 如何写一个简单的http服务器
最近几天用C++写了一个简单的HTTP服务器,作为学习网络编程和Linux环境编程的练手项目,这篇文章记录我在写一个HTTP服务器过程中遇到的问题和学习到的知识. 服务器的源代码放在Github. H ...
随机推荐
- Python读取CSV文件,报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 727: illegal multibyte sequence
Python读取CSV文件,报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 727: illegal mul ...
- nginx+django+uwsgi
最近来了兴致,想搞一下django开发,so, 搭建一下环境 1.安装django,可能通过pip install 或者源码安装(因为环境是python2.6.6的环境,所以这里采用django 1 ...
- FacadePattern(23种设计模式之一)
设计模式六大原则(1):单一职责原则 设计模式六大原则(2):里氏替换原则 设计模式六大原则(3):依赖倒置原则 设计模式六大原则(4):接口隔离原则 设计模式六大原则(5):迪米特法则 设计模式六大 ...
- Java学习——JSTL标签与EL表达式之间的微妙关系
原文总结的太好了,忍不住记录.转发. 原文地址:http://blog.csdn.net/u010168160/article/details/49182867 目录(?)[-] 一EL表达式 EL相 ...
- On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API
Ints are easy. Strings are mostly easy. Dates? A nightmare. They always will be. There's different c ...
- TensorFlow中文手册
注意:本文只为读书笔记. 第一章 起步 - 起步 - [介绍](SOURCE/get_started/introduction.md) - [下载及安装](SOURCE/get_started/os_ ...
- 很棒的bootstrap学习网站
http://www.w3cschool.cc/bootstrap/bootstrap-tutorial.html
- ORACLE_ERP帐务分录
ORACLE MRPII各模块会计分录 第一章 采购模块 一.资产采购(科目来源:库存组织) 1.物料接收 借 材料采购 接收数量*采购单价 贷 应计暂估 接收数量*采购单价 ...
- How do I create a .pyc file?
Python automatically compiles your script to compiled code, so called byte code, before running it. ...
- delphi 创建DLL文件 及其调用和注意事项
首先创建一个DLL文件,项目自带的代码为: library ProjectPnr; { Important note about DLL memory management: ShareMem mus ...