连连看游戏源代码C#.docx

上传人:b****1 文档编号:1712432 上传时间:2023-05-01 格式:DOCX 页数:25 大小:19.61KB
下载 相关 举报
连连看游戏源代码C#.docx_第1页
第1页 / 共25页
连连看游戏源代码C#.docx_第2页
第2页 / 共25页
连连看游戏源代码C#.docx_第3页
第3页 / 共25页
连连看游戏源代码C#.docx_第4页
第4页 / 共25页
连连看游戏源代码C#.docx_第5页
第5页 / 共25页
连连看游戏源代码C#.docx_第6页
第6页 / 共25页
连连看游戏源代码C#.docx_第7页
第7页 / 共25页
连连看游戏源代码C#.docx_第8页
第8页 / 共25页
连连看游戏源代码C#.docx_第9页
第9页 / 共25页
连连看游戏源代码C#.docx_第10页
第10页 / 共25页
连连看游戏源代码C#.docx_第11页
第11页 / 共25页
连连看游戏源代码C#.docx_第12页
第12页 / 共25页
连连看游戏源代码C#.docx_第13页
第13页 / 共25页
连连看游戏源代码C#.docx_第14页
第14页 / 共25页
连连看游戏源代码C#.docx_第15页
第15页 / 共25页
连连看游戏源代码C#.docx_第16页
第16页 / 共25页
连连看游戏源代码C#.docx_第17页
第17页 / 共25页
连连看游戏源代码C#.docx_第18页
第18页 / 共25页
连连看游戏源代码C#.docx_第19页
第19页 / 共25页
连连看游戏源代码C#.docx_第20页
第20页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

连连看游戏源代码C#.docx

《连连看游戏源代码C#.docx》由会员分享,可在线阅读,更多相关《连连看游戏源代码C#.docx(25页珍藏版)》请在冰点文库上搜索。

连连看游戏源代码C#.docx

连连看游戏源代码C#

连连看

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Collections;//ArrayList命名空间

namespace连连看

{

publicpartialclassForm1:

Form

{

privateBitmapSource;//所有动物图案的图片

privateintW=50;//动物方块图案的宽度

privateintGameSize=10;//布局大小即行列数

privateboolSelect_first=false;//是否已经选中第一块

privateintx1,y1;//被选中第一块的地图坐标

privateintx2,y2;//被选中第二块的地图坐标

Pointz1,z2;//折点棋盘坐标

privateintm_nCol=10;

privateintm_nRow=10;

privateint[]m_map=newint[10*10];

privateintBLANK_STATE=-1;

publicenumLinkType{LineType,OneCornerType,TwoCornerType};

LinkTypeLType;//连通方式

publicForm1()

{

InitializeComponent();

}

privatevoidForm1_Load(objectsender,EventArgse)

{

Source=(Bitmap)Image.FromFile("..\\..\\res\\animal.bmp");

this.pictureBox1.Height=W*(m_nRow+2);

this.pictureBox1.Width=W*(m_nCol+2);

this.pictureBox1.Top=0;

this.pictureBox1.Left=0;

//当前窗体标题栏高度

intd=(this.Height-this.ClientRectangle.Height);

this.Height=this.pictureBox1.Height+this.pictureBox1.Top+d;

this.Width=this.pictureBox1.Width+this.pictureBox1.Left;

//for(inti=0;i<10*10;i++)

//{

//m_map[i]=i%6;

//}

StartNewGame();

Init_Graphic();

}

privatevoidStartNewGame()

{

//初始化地图,将地图中所有方块区域位置置为空方块状态

for(intiNum=0;iNum<(m_nCol*m_nRow);iNum++)

{

m_map[iNum]=BLANK_STATE;

}

Randomr=newRandom();

//生成随机地图

//将所有的动物物种放进一个临时的地图tmpMap中

ArrayListtmpMap=newArrayList();

for(inti=0;i<(m_nCol*m_nRow)/4;i++)

for(intj=0;j<4;j++)

tmpMap.Add(i);

//每次从上面的临时地图tmpMap中取走(获取后并在临时地图删除)

//一个动物放到地图的空方块上

for(inti=0;i

{

//随机挑选一个位置

intnIndex=r.Next()%tmpMap.Count;

//获取该选定物件放到地图的空方块

m_map[i]=(int)tmpMap[nIndex];

//在临时地图tmpMap除去该动物

tmpMap.RemoveAt(nIndex);

}

}

privatevoidInit_Graphic()

{

Graphicsg=get_Graphic();//生成Graphics对象

for(inti=0;i<10*10;i++)

{

g.DrawImage(create_image(m_map[i]),W*(i%GameSize)+W,

W*(i/GameSize)+W,W,W);

}

}

privateGraphicsget_Graphic()

{

if(pictureBox1.Image==null)

{

Bitmapbmp=newBitmap(pictureBox1.Width,pictureBox1.Height);

pictureBox1.Image=bmp;

}

Graphicsg=Graphics.FromImage(pictureBox1.Image);

returng;

}

publicGraphicsGetGraphicsObject(refPictureBoxpic)

{

System.Drawing.Graphicsg;

Bitmapbmp=newBitmap(pic.Width,pic.Height);

pic.Image=bmp;

g=Graphics.FromImage(bmp);

returng;

}

//create_image()方法实现按标号n从所有动物图案的图片中截图。

privateBitmapcreate_image(intn)//按标号n截图

{

Bitmapbit=newBitmap(W,W);

Graphicsg=Graphics.FromImage(bit);//生成Graphics对象

Rectanglea=newRectangle(0,0,W,W);

Rectangleb=newRectangle(0,n*39,39,39);

//截取原图中b矩形区域的图形

g.DrawImage(Source,a,b,GraphicsUnit.Pixel);

returnbit;

}

///

///检测是否已经赢得了游戏

///

boolIsWin()

{

//检测所有是否尚有非未被消除的方块

//(非BLANK_STATE状态)

for(inti=0;i

{

if(m_map[i]!

=BLANK_STATE)

{

returnfalse;

}

}

returntrue;

}

privateboolIsSame(intx1,inty1,intx2,inty2)

{

if(m_map[y1*m_nCol+x1]==m_map[y2*m_nCol+x2])

returntrue;

else

returnfalse;

}

//

//X直接连通即垂直方向连通

//

boolX_Link(intx,inty1,inty2)

{

//保证y1的值小于y2

if(y1>y2)

{

//数据交换

intn=y1;

y1=y2;

y2=n;

}

//直通

for(inti=y1+1;i<=y2;i++)

{

if(i==y2)

returntrue;

if(m_map[i*m_nCol+x]!

=BLANK_STATE)

break;

}

returnfalse;

}

//

//Y直接连通即水平方向连通

//

boolY_Link(intx1,intx2,inty)

{

if(x1>x2)

{

intx=x1;

x1=x2;

x2=x;

}

//直通

for(inti=x1+1;i<=x2;i++)

{

if(i==x2)

returntrue;

if(m_map[y*m_nCol+i]!

=BLANK_STATE)

break;

}

returnfalse;

}

//

//1直角接口连通

//

boolOneCornerLink(intx1,inty1,intx2,inty2)

{

if(x1>x2)//目标点(x1,y1),(x2,y2)两点交换

{

intn=x1;

x1=x2;

x2=n;

n=y1;

y1=y2;

y2=n;

}

if(y2

{

//判断矩形右下角折点(x2,y1)是否空

if(m_map[y1*m_nCol+x2]==BLANK_STATE)

{

if(Y_Link(x1,x2,y1)&&X_Link(x2,y1,y2))

//判断折点(x2,y1)与两个目标点是否直通

{

z1.X=x2;z1.Y=y1;//保存折点坐标到z1

returntrue;

}

}

//判断矩形左上角折点(x1,y2)是否空

if(m_map[y2*m_nCol+x1]==BLANK_STATE)

{

if(Y_Link(x2,x1,y2)&&X_Link(x1,y2,y1))

//判断折点(x1,y2)与两个目标点是否直通

{

z1.X=x1;z1.Y=y2;//保存折点坐标到z1

returntrue;

}

}

returnfalse;

}

else//(x1,y1)为矩形左上顶点,(x2,y2)点为矩形右下顶点

{

//判断矩形左下角折点(x1,y2)是否空

if(m_map[y2*m_nCol+x1]==BLANK_STATE)

{

if(Y_Link(x1,x2,y2)&&X_Link(x1,y1,y2))

//判断折点(x1,y2)与两个目标点是否直通

{

z1.X=x1;z1.Y=y2;//保存折点坐标到z1

returntrue;

}

}

//判断矩形右上角折点(x2,y1)是否空

if(m_map[y1*m_nCol+x2]==BLANK_STATE)

{

if(Y_Link(x1,x2,y1)&&X_Link(x2,y1,y2))

//判断折点(x2,y1)与两个目标点是否直通

{

z1.X=x2;z1.Y=y1;//保存折点坐标到z1

returntrue;

}

}

returnfalse;

}

}

///

///2折点连通

///

boolTwoCornerLink(intx1,inty1,intx2,inty2)

{

if(x1>x2)

{

intn=x1;

x1=x2;

x2=n;

n=y1;

y1=y2;

y2=n;

}

//右

intx,y;

for(x=x1+1;x<=m_nCol;x++)

{

if(x==m_nCol)

//两个折点在选中方块的右侧,且两个折点在图案区域之外

if(XThrough(x2+1,y2,true))

//Y_Link(x2+1,m_nCol-1,y2)&&m_map[y1*(m_nCol-1)+x]==BLANK_STATE

{

z2.X=m_nCol;z2.Y=y1;

z1.X=m_nCol;z1.Y=y2;

returntrue;

}

else

break;

if(m_map[y1*m_nCol+x]!

=BLANK_STATE)

break;

if(OneCornerLink(x,y1,x2,y2))

{

z2.X=x;z2.Y=y1;

returntrue;

}

}

//左

for(x=x1-1;x>=-1;x--)

{

if(x==-1)

//两个折点在选中方块的左侧,且两个折点在图案区域之外

if(XThrough(x2-1,y2,false))

{

z2.X=-1;z2.Y=y1;

z1.X=-1;z1.Y=y2;

returntrue;

}

else

break;

if(m_map[y1*m_nCol+x]!

=BLANK_STATE)

break;

if(OneCornerLink(x,y1,x2,y2))

{

z2.X=x;z2.Y=y1;

returntrue;

}

}

//上

for(y=y1-1;y>=-1;y--)

{

if(y==-1)

//两个折点在选中方块的上侧,且两个折点在图案区域之外

if(YThrough(x2,y2-1,false))

{

z2.X=x1;z2.Y=-1;

z1.X=x2;z1.Y=-1;

returntrue;

}

else

break;

if(m_map[y*m_nCol+x1]!

=BLANK_STATE)

break;

if(OneCornerLink(x1,y,x2,y2))

{

z2.X=x1;z2.Y=y;

returntrue;

}

}

//下

for(y=y1+1;y<=m_nRow;y++)

{

if(y==m_nRow)

//两个折点在选中方块的下侧,且两个折点在图案区域之外

if(YThrough(x2,y2+1,true))

{

z2.X=x1;z2.Y=m_nRow;

z1.X=x2;z1.Y=m_nRow;

returntrue;

}

else

break;

if(m_map[y*m_nCol+x1]!

=BLANK_STATE)

break;

if(OneCornerLink(x1,y,x2,y2))

{

z2.X=x1;z2.Y=y;

returntrue;

}

}

returnfalse;

}

boolXThrough(intx,inty,boolbAdd)//水平方向判断到边界的连通性

{

if(bAdd)//True,水平向右判断是否连通(是否为空)

{

for(inti=x;i

if(m_map[y*m_nCol+i]!

=BLANK_STATE)

returnfalse;

}

else//false,水平向左判断是否连通(是否为空)

{

for(inti=0;i<=x;i++)

if(m_map[y*m_nCol+i]!

=BLANK_STATE)

returnfalse;

}

returntrue;

}

boolYThrough(intx,inty,boolbAdd)//垂直方向判断到边界的连通性)

{

if(bAdd)//True,垂直方向向下判断是否连通(是否为空)

{

for(inti=y;i

if(m_map[i*m_nCol+x]!

=BLANK_STATE)

returnfalse;

}

else//false,垂直方向向上判断是否连通(是否为空)

{

for(inti=0;i<=y;i++)

if(m_map[i*m_nCol+x]!

=BLANK_STATE)

returnfalse;

}

returntrue;

}

//

//判断选中的两个方块是否可以消除

//

boolIsLink(intx1,inty1,intx2,inty2)

{

//X直连方式即垂直方向连通

if(x1==x2)

{

if(X_Link(x1,y1,y2))

{LType=LinkType.LineType;returntrue;}

}

//Y直连方式即水平方向连通

elseif(y1==y2)

{

if(Y_Link(x1,x2,y1))

{LType=LinkType.LineType;returntrue;}

}

//一个转弯(折点)的联通方式

if(OneCornerLink(x1,y1,x2,y2))

{

LType=LinkType.OneCornerType;

returntrue;

}

//两个转弯(折点)的联通方式

elseif(TwoCornerLink(x1,y1,x2,y2))

{

LType=LinkType.TwoCornerType;

returntrue;

}

returnfalse;

}

privateboolFind2Block()

{

boolbFound=false;

//第一个方块从地图的0位置开始

for(inti=0;i

{

//找到则跳出循环

if(bFound)

break;

//无动物的空格跳过

if(m_map[i]==BLANK_STATE)

continue;

//第二个方块从前一个方块的后面开始

for(intj=i+1;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)连通

Graphicsg=this.pictureBox1.CreateGraphics();//生成Graphics对象

//**********************************

//Graphicsg=get_Graphic();//生成Graphics对象

//************************************

PenmyPen=newPen(Color.Blue,3);

Rectangleb1=newRectangle(x1*W+1+W,y1*W+1+W,W-3,W-3);

g.DrawRectangle(myPen,b1);

Rectangleb2=newRectangle(x2*W+1+W,y2*W+1+W,W-3,W-3);

g.DrawRectangle(myPen,b2);

}

returnbFound;

}

privatevoidForm1_Paint(objectsender,PaintEventArgse)

{

//Init_Graphic();

}

privatevoidDelay(intsecond)//让程序延时数秒

{

DateTimenow=DateTime.Now;

while(now.AddSeconds(second)>DateTime.Now)

{

}

}

///

///画选中方块之间连接线

///

privatevoidDrawLinkLine(intx1,inty1,intx2,inty2,LinkTypeLType)

{

Graphicsg=this.pictureBox1.CreateGraphics();//生成Graphics对象

Penp=newPen(Color.Red,3);

Pointp1=newPoint(x1*W+W/2+W,y1*W+W/2+W);

Pointp2=newPoint(x2*W+W/2+W,y2*W+W/2+W);

if(LType==LinkType.LineType)

g.DrawLine(p,p1,p2);

if(LType==LinkType.OneCornerType)

{

Pointpixel_z1=newPoint(z1.X*W+W/2+W,z1.Y*W+W/2+W);

g.DrawLine(p,p1,pixel_z1);

g.DrawLine(p,pixel_z1,p2);

}

if(LType==LinkType.TwoCornerType)

{

Pointpixel_z1=newPoint(z1.X*W+W/2+W

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2