using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace connect
{
public partial class Form1 : Form
{
LineConnectBoard lcb;
Point p1 = new Point(-1, -1), p2 = new Point(-1, -1);
Point pt;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics gs = Graphics.FromHwnd(panel1.Handle);//获取面板
lcb = new LineConnectBoard(gs,40,40,10, 50, imageList1);
lcb.DrawGrid();
lcb.InitArray(7);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics gs = Graphics.FromHwnd(panel1.Handle);//获取面板
if (lcb!=null)
{
lcb.DrawGrid();
for (int i = 1; i <= lcb.BlockNums; i++)
for (int j = 1; j <= lcb.BlockNums; j++)
lcb.FillBlock(j-1, i-1, lcb.array[i, j]);
}
}
private void panel1_Click(object sender, EventArgs e)
{
int ax = -1, ay = -1;
pt = panel1.PointToClient(Cursor.Position);
if(!(lcb.PointToArray(pt, ref ax, ref ay)))
return;
if (p1.X == -1 && p1.Y == -1)
{
p1.X = ax;
p1.Y = ay;
lcb.DrawRecByBlue(ax, ay);
}
else if ((p1.X == ax && p1.Y == ay) || (lcb.array[p1.Y, p1.X] != lcb.array[ay, ax])||(!lcb.CanConnected(p1.X, p1.Y, ax, ay)))
{
lcb.DrawRecByRed(p1.X, p1.Y);
p1.X = ax;
p1.Y = ay;
lcb.DrawRecByBlue(ax, ay);
}
else if(lcb.array[p1.Y, p1.X] == lcb.array[ay, ax])
{
if(lcb.CanConnected(p1.X,p1.Y,ax,ay))
{
lcb.DrawRecByRed(ax,ay);
lcb.DrawRecByRed(p1.X,p1.Y);
long i = 0;
while (i <= 10000000)
{
i++;
}
lcb.ClearBlock(ax, ay, lcb.array[ay, ax]);
lcb.ClearBlock(p1.X, p1.Y, lcb.array[p1.Y, p1.X]);
p1.X=-1;p1.Y=-1;
lcb.ClearLine();
if (lcb.Succeed())
MessageBox.Show("你胜利了,恭喜哦");
}
}
}
}
public class LineConnectBoard //面板类
{
private Graphics gs;//定义一个画板对象的引用
private int StartX;//起点X
private int StartY;//起点Y
public int BlockNums;//起点的个数
private int BlockSize;//结点的大小
public int[,] array;//定义一个二维数组的引用
private ImageList Imgs;//定义一个图片列表的引用
public LineConnectBoard(Graphics gs, int StartX, int StartY, int BlockNums, int BlockSize, ImageList Imgs)
{
this.gs = gs;
this.StartX = StartX;
this.StartY = StartY;
this.BlockNums = BlockNums;
this.BlockSize = BlockSize;
array = new int[BlockNums+2, BlockNums+2];
this.Imgs = Imgs;
}
public void DrawGrid() //画格子
{
Pen pn1 = new Pen(Color.Red, 1);//定义一个画笔
int x = this.StartX;
int y = this.StartY;
for (int i = 0; i <= this.BlockNums; i++)
{
gs.DrawLine(pn1, x, this.StartY, x, this.StartY + BlockNums * BlockSize);//画列线
x += BlockSize;
gs.DrawLine(pn1, this.StartX, y, this.StartX + BlockNums * BlockSize, y);//画行线
y += BlockSize;
}
}
public bool Succeed()
{
bool flag=true;
for(int i=0;i<BlockNums+2;i++)
for (int j = 0; j < BlockNums + 2; j++)
{
if (array[i, j] != -1)
{
flag = false;
break;
}
}
return flag;
}
public void ClearLine()
{
Brush br = Brushes.White;
Rectangle rect = new Rectangle( 10, 10, BlockSize * BlockNums + 40 * 2, 30);
Rectangle rect2 = new Rectangle( 10,BlockNums*BlockSize+BlockSize+10, BlockSize * BlockNums + 40 * 2, 20);
Rectangle rect3 = new Rectangle( 10, 10, 30, (BlockNums + 2) * BlockSize - 20);
Rectangle rect4 = new Rectangle( 10+(BlockNums+1)*BlockSize, 10, 30, (BlockNums + 2) * BlockSize - 20);
gs.FillRectangle(br, rect);
gs.FillRectangle(br, rect2);
gs.FillRectangle(br, rect3);
gs.FillRectangle(br, rect4);
for(int i=0;i<=BlockNums+1;i++)
for (int j = 0; j <= BlockNums+1; j++)
{
if (array[i, j] == -1)
ClearBlock(j, i, array[i, j]);
this.DrawGrid();
}
}
public void FillBlock(int x, int y, int index) //填充格子
{
Rectangle rect = new Rectangle(StartX + BlockSize * x + 1, StartY + BlockSize * y + 1, BlockSize - 1, BlockSize - 1);//确定方格的位置
if(index>-1)
gs.DrawImage(Imgs.Images[index], rect);
}
public void DrawLines(int x1, int y1, int x2, int y2)
{
x1--; y1--; x2--; y2--;
Pen pn=new Pen(Color.Black,5);
gs.DrawLine(pn, StartX + BlockSize * x1 + 1 + 25, StartY + BlockSize * y1 + 1 + 25, StartX + BlockSize * x2 + 1 + 25, StartY + BlockSize * y2 + 1 + 25);
}
public void ClearBlock(int x, int y, int index) //用白色背景刷新格子
{
x--;
y--;
Brush br = Brushes.White;
Rectangle rect = new Rectangle(StartX + BlockSize * x + 1, StartY + BlockSize * y + 1, BlockSize - 1, BlockSize - 1);//确定方格的位置
gs.FillRectangle(br, rect);
array[y+1, x+1] = -1;
}
public void DrawRecByBlue(int x, int y) //在指定位置画绿色边框
{
x--;
y--;
Pen p = new Pen(Color.Blue);
Rectangle rect = new Rectangle(StartX + BlockSize * x , StartY + BlockSize * y , BlockSize , BlockSize );//确定方格的位置
gs.DrawRectangle(p, rect);
}
public void DrawRecByRed(int x, int y) //在指定位置画红色边框
{
x--;
y--;
Pen p = new Pen(Color.Red);
Rectangle rect = new Rectangle(StartX + BlockSize * x , StartY + BlockSize * y , BlockSize , BlockSize );//确定方格的位置
gs.DrawRectangle(p, rect);
}
public bool PointToArray(Point pt, ref int AX, ref int AY)
{
if (pt.X < StartX || pt.Y < StartY || pt.X > StartX + BlockSize * BlockNums || pt.Y > StartY + BlockSize * BlockNums)
return false;
AX = (pt.X - StartX) / BlockSize+1;//得到X坐标
AY = (pt.Y - StartY) / BlockSize+1;//得到Y坐标
return true;
}
public void InitArray(int MAX)
{
for (int i = 0; i < BlockNums + 2; i++)
{
array[0, i] = -1;
array[BlockNums + 1, i] = -1;
}
for (int i = 1; i < BlockNums + 1; i++)
{
array[i, 0] = -1;
array[i, BlockNums + 1] = -1;
}
int N = Block
用C#实现的连连看游戏
需积分: 9 73 浏览量
2010-05-22
21:35:23
上传
评论
收藏 1.38MB RAR 举报
caofeng891102
- 粉丝: 168
- 资源: 1217
最新资源
- python-leetcode面试题解之第157题用Read4读取N个字符-题解.zip
- python-leetcode面试题解之第156题上下翻转二叉树-题解.zip
- python-leetcode面试题解之第155题最小栈-题解.zip
- python-leetcode面试题解之第153题寻找旋转排序数组中的最小值-题解.zip
- python-leetcode面试题解之第152题乘积最大子数组-题解.zip
- python-leetcode面试题解之第151题反转字符串中的单词-题解.zip
- python-leetcode面试题解之第150题逆波兰表达式求值-题解.zip
- python-leetcode面试题解之第149题直线上最多的点数-题解.zip
- python-leetcode面试题解之第148题排序链表-题解.zip
- python-leetcode面试题解之第147题对链表进行插入排序-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈