实例介绍
【实例简介】连连看 .net版本
【实例截图】

【核心代码】
public partial class Form1 : Form
{
private Bitmap Source; //所有动物图案的图片
private int W; //动物方块图案的宽度
private int GameSize=10; //布局大小即行列数
private bool Select_first = false; //是否已经选中第一块
private int x1, y1; //被选中第一块的地图坐标
private int x2, y2; //被选中第二块的地图坐标
private int m_nCol = 10;
private int m_nRow = 10;
private int[] m_map = new int[10*10];
private int BLANK_STATE = -1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Source = (Bitmap)Image.FromFile("..\\..\\res\\animal.bmp");
W = this.Width / GameSize; //显示动物方块图案的宽度
this.Height = this.Width 30;
for (int i = 0; i< 10 * 10; i )
{
m_map[i] = i%6;
}
}
//窗体第一次显示时发生
private void Form1_Shown(object sender, EventArgs e)
{
Init_Graphic();
}
private void Init_Graphic()
{
//Graphics g = this.CreateGraphics(); //生成Graphics对象
//**********************************
Graphics g = get_Graphic(); //生成Graphics对象
//************************************
for (int i = 0; i< 10 * 10; i )
{
g.DrawImage(create_image(m_map[i]), W * (i % GameSize),
W * (i / GameSize), W, W);
}
}
private Graphics get_Graphic()
{
Bitmap bmp = new Bitmap(this.Width, this.Height);
this.BackgroundImage = bmp;
Graphics g = Graphics.FromImage(bmp);
return g;
}
//public Graphics GetGraphicsObject(ref Form f)
//{
// System.Drawing.Graphics g;
// Bitmap bmp = new Bitmap(f.Width, f.Height);
// f.BackgroundImage= bmp;
// g = Graphics.FromImage(bmp);
// return g;
//}
//create_image()方法实现按标号n从所有动物图案的图片中截图。
private Bitmap create_image(int n) //按标号n截图
{
Bitmap bit = new Bitmap(W, W);
Graphics g = Graphics.FromImage(bit); //生成Graphics对象
Rectangle a = new Rectangle(0, 0, W, W);
Rectangle b = new Rectangle(0, n *39, 39, 39);
//截取原图中b矩形区域的图形
g.DrawImage(Source, a, b, GraphicsUnit.Pixel);
return bit;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.Hand;
Graphics g = this.CreateGraphics(); //生成Graphics对象
//**********************************
//Graphics g = get_Graphic(); //生成Graphics对象
//************************************
Pen myPen = new Pen(Color.Red , 3);
int x, y;
if (e.Button == MouseButtons.Left)
{
//计算点击的方块的位置坐标
x = e.X / W;
y = e.Y / W;
//如果该区域无方块
if (m_map[y * m_nCol x] == BLANK_STATE) return;
if (Select_first == false)
{
x1 = x; y1 = y;
Rectangle b1 = new Rectangle(x1 * W 1, y1 * W 1, W - 3, W - 3);
g.DrawRectangle(myPen, b1);
Select_first = true;
}
else
{
x2 = x; y2 = y;
//判断第二次点击的方块是否已被第一次点击选取,如果是则返回。
if ((x1 == x2) && (y1 == y2)) return;
//
if (IsSame(x1, y1, x2, y2)&&IsLink(x1, y1, x2, y2))
{
SolidBrush myBrush = new SolidBrush(this.BackColor); //定义背景色画刷
Rectangle b1 = new Rectangle(x1 * W, y1 * W, W, W);
Rectangle b2 = new Rectangle(x2 * W, y2 * W, W , W);
g.FillRectangle(myBrush, b2);
g.FillRectangle(myBrush, b1);
////清空记录方块的值
m_map[y1 * m_nCol x1] = BLANK_STATE;
m_map[y2 * m_nCol x2] = BLANK_STATE;
Select_first = false;
}
else
{
//重画(x1,y1)处动物图案来达到取消选定的框线
int i = y1 * m_nCol x1;
g.DrawImage(create_image(m_map[i]), W * (i % GameSize),
W * (i / GameSize), W, W);
x1 = e.X / W; y1 = e.Y / W;
myPen = new Pen(Color.Red, 3);
Rectangle b2 = new Rectangle(x2 * W 1, y2 * W 1, W - 3, W - 3);
g.DrawRectangle(myPen, b2);
Select_first = true;
}
}
}
//察看是否已经胜利
if (IsWin())
{
MessageBox.Show("恭喜您胜利闯关,即将开始新局");
//StartNewGame();
}
}
///
/// 检测是否已经赢得了游戏
///
bool IsWin()
{
//检测所有是否尚有非未被消除的方块
// (非BLANK_STATE状态)
for(int i=0;i<m_nRow*m_nCol;i )
{
if(m_map[i] != BLANK_STATE)
{
return false;
}
}
return true;
}
private bool IsSame(int x1, int y1,int x2, int y2)
{
if (m_map[y1 * m_nCol x1] == m_map[y2 * m_nCol x2])
return true;
else
return false;
}
//
//X直接连通
//
bool X1_Link_X2(int x, int y1,int y2)
{
//保证y1的值小于y2
if(y1>y2)
{
//数据交换
int n=y1;
y1=y2;
y2=n;
}
//直通
for(int i=y1 1;i<=y2;i )
{
if(i==y2)
return true;
if(m_map[i*m_nCol x]!=BLANK_STATE)
break;
}
////左通
//if(XThrough(x-1,y1,false)&&XThrough(x-1,y2,false))
// return true;
////右通
//if(XThrough(x 1,y1,true)&&XThrough(x 1,y2,true))
// return true;
return false;
}
//
//Y直接连通
//
bool Y1_Link_Y2(int x1,int x2,int y)
{
if(x1>x2)
{
int x=x1;
x1=x2;
x2=x;
}
//直通
for(int i=x1 1;i<=x2;i )
{
if(i==x2)
return true;
if(m_map[y*m_nCol i]!=BLANK_STATE)
break;
}
////上通
//if(YThrough(x1,y-1,false)&&YThrough(x2,y-1,false))
// return true;
////下通
//if(YThrough(x1,y 1,true)&&YThrough(x2,y 1,true))
// return true;
return false;
}
//
// 是否同一直线通
//
bool LineX(int x,int y1,int y2)
{
if(y1>y2)
{
int y=y1;
y1=y2;
y2=y;
}
for(int y=y1;y<=y2;y )
{
if(m_map[y*m_nCol x]!=BLANK_STATE)
return false;
if(y==y2)
return true;
}
return false;
}
//
// 是否同一直线通
//
bool LineY(int x1,int x2,int y)
{
if(x1>x2)
{
int x=x1;
x1=x2;
x2=x;
}
for(int x=x1;x<=x2;x )
{
if(m_map[y*m_nCol x]!=BLANK_STATE)
return false;
if(x==x2)
return true;
}
return false;
}
//
// 1直角接口连通
//
bool OneCornerLink(int x1, int y1,int x2, int y2)
{
if(x1>x2)
{
int n=x1;
x1=x2;
x2=n;
n=y1;
y1=y2;
y2=n;
}
if(y2<y1)
{
if(LineY(x1 1,x2,y1)&&LineX(x2,y1,y2 1))
return true;
if(LineY(x2-1,x1,y2)&&LineX(x1,y2,y1-1))
return true;
}
else
{
if(LineY(x1 1,x2,y1)&&LineX(x2,y1,y2-1))
return true;
if(LineY(x2-1,x1,y2)&&LineX(x1,y2,y1 1))
return true;
}
return false;
}
//
// 2直角接口连通
//
bool TwoCornerLink(int x1, int y1, int x2, int y2)
{
if(x1>x2)
{
int n=x1;
x1=x2;
x2=n;
n=y1;
y1=y2;
y2=n;
}
//右通
if(XThrough(x1 1,y1,true)&&XThrough(x2 1,y2,true))
return true;
//左通
if(XThrough(x1-1,y1,false)&&XThrough(x2-1,y2,false))
return true;
//上通
if(YThrough(x1,y1-1,false)&&YThrough(x2,y2-1,false))
return true;
//下通
if(YThrough(x1,y1 1,true)&&YThrough(x2,y2 1,true))
return true;
//右
int x,y;
for(x=x1 1;x<m_nCol;x )
{
if (m_map[y1 * m_nCol x] != BLANK_STATE)
break;
if(OneCornerLink(x,y1,x2,y2))
return true;
}
//左
for(x=x1-1;x>-1;x--)
{
if(m_map[y1*m_nCol x]!=BLANK_STATE)
break;
if(OneCornerLink(x,y1,x2,y2))
return true;
}
//上
for(y=y1-1;y>-1;y--)
{
if(m_map[y*m_nCol x1]!=BLANK_STATE)
break;
if(OneCornerLink(x1,y,x2,y2))
return true;
}
//下
for(y=y1 1;y<m_nRow;y )
{
if(m_map[y*m_nCol x1]!=BLANK_STATE)
break;
if(OneCornerLink(x1,y,x2,y2))
return true;
}
return false;
}
bool XThrough(int x, int y, bool bAdd)
{
if(bAdd)
{
for(int i=x;i<m_nCol;i )
if(m_map[y*m_nCol i]!=BLANK_STATE)
return false;
}
else
{
for(int i=0;i<=x;i )
if(m_map[y*m_nCol i]!=BLANK_STATE)
return false;
}
return true;
}
bool YThrough(int x, int y,bool bAdd)
{
if(bAdd)
{
for(int i=y;i<m_nRow;i )
if(m_map[i*m_nCol x]!=BLANK_STATE)
return false;
}
else
{
for(int i=0;i<=y;i )
if(m_map[i*m_nCol x]!=BLANK_STATE)
return false;
}
return true;
}
//
// 判断选中的两个方块是否可以消除
//
bool IsLink(int x1, int y1, int x2, int y2)
{
//X直连方式
if(x1==x2)
{
if(X1_Link_X2(x1,y1,y2))
return true;
}
//Y直连方式
else if(y1==y2)
{
if(Y1_Link_Y2(x1,x2,y1))
return true;
}
//一个转弯直角的联通方式
if(OneCornerLink(x1,y1,x2,y2))
{
return true;
}
//两个转弯直角的联通方式
else if(TwoCornerLink(x1,y1,x2,y2))
{
return true;
}
return false;
}
private void Form1_DoubleClick(object sender, EventArgs e)
{
bool bFound =false ;
//第一个方块从地图的0位置开始
for (int i = 0; i < m_nRow * m_nCol; i )
{
//找到则跳出循环
if (bFound)
break;
//无动物的空格跳过
if (m_map[i] == BLANK_STATE)
continue;
//第二个方块从前一个方块的后面开始
for (int j = i 1; j < m_nRow * m_nCol; j )
{
//第二个方块不为空 且与第一个方块的动物相同
if (m_map[j] != BLANK_STATE && m_map[i] == m_map[j])
{
//算出对应的虚拟行列位置
x1 = i % m_nCol;
y1 = i / m_nCol;
x2 = j % m_nCol;
y2 = j / m_nCol;
//判断是否可以连通
if (IsLink(x1, y1, x2, y2))
{
bFound = true ;
break;
}
}
}
}
if (bFound)
{
//(x1,y1)与(x2,y2)连通
Graphics g = this.CreateGraphics(); //生成Graphics对象
//**********************************
//Graphics g = get_Graphic(); //生成Graphics对象
//************************************
Pen myPen = new Pen(Color.Red, 3);
Rectangle b1 = new Rectangle(x1 * W 1, y1 * W 1, W - 3, W - 3);
g.DrawRectangle(myPen, b1);
Rectangle b2 = new Rectangle(x2 * W 1, y2 * W 1, W - 3, W - 3);
g.DrawRectangle(myPen, b2);
}
}
}
标签: 游戏
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论