C#俄罗斯方块课设报告.docx
《C#俄罗斯方块课设报告.docx》由会员分享,可在线阅读,更多相关《C#俄罗斯方块课设报告.docx(37页珍藏版)》请在冰豆网上搜索。
C#俄罗斯方块课设报告
成绩评定表
学生姓名
班级学号
专业
计算机科学与技术
课程设计题目
俄罗斯方块游戏
评
语
组长签字:
成绩
日期
2014年1月14日
课程设计任务书
学院
信息科学与工程学院
专业
计算机科学与技术
学生姓名
班级学号
课程设计题目
俄罗斯方块游戏
实践教学要求与任务:
本系统为一个用C#实现的为我们所熟悉的简单的俄罗斯方块游戏,该系统的具体功能如下:
(1)能简便的开始游戏,游戏中的方块的功能与日常我们所熟悉的游戏的功能一致,各种块的设置也一致,包括块的旋转,加速下降,平移,满行消去,到顶游戏结束功能;
(2)能够自定义游戏中功能键的具体按键,设置游戏的级别,即方块下降的速等,并在设置后立即生效,在游戏中立刻体现,在下次程序运行时读取上次设置的游戏环境;
(3)显示下一方块提示信息,以及游戏数据的统计,如速度,按键速度,分数行者等等;
(4)游戏有级别设置,级别越高,方块下降的速度越快,当到达一定分数后进入下一级别;
工作计划与进度安排:
第19周:
软件设计,编码调试,书写课设报告,答辩。
指导教师:
2013年12月26日
专业负责人:
2013年12月27日
学院教学副院长:
2013年12月28日
一、要求和规范
本系统为一个用C#实现的为我们所熟悉的简单的俄罗斯方块游戏,该系统的具体功能如下:
(1)能简便的开始游戏,游戏中的方块的功能与日常我们所熟悉的游戏的功能一致,各种块的设置也一致,包括块的旋转,加速下降,平移,满行消去,到顶游戏结束功能;
(2)能够自定义游戏中功能键的具体按键,设置游戏的级别,即方块下降的速等,并在设置后立即生效,在游戏中立刻体现,在下次程序运行时读取上次设置的游戏环境;
(3)显示下一方块提示信息,以及游戏数据的统计,如速度,按键速度,分数行者等等;
(4)游戏有级别设置,级别越高,方块下降的速度越快,当到达一定分数后进入下一级别;
二、制作过程
2.1新建C#2010项目
打开VisualC#.NET2005,从主菜单中选择“文件”/“新建”/“项目”,在弹出的“新建项目”对话框中,在左侧“已安装的模板”中选择VisaulC#/Windows,在中间位置选择“windows应用程序”,在“名称”中输入项目名称“RussiaBlock”,最后按确定按钮,建立新项目。
如图1所示。
图1.建立新项目“RussiaBlock”
在编辑器右侧文件树上,鼠标右键单击Form1.cs,选择“重命名(M)”,修改名称为:
MainForm.cs
2.2在MainForm设计窗体中添加控件,构造界面
2.3单击Mainform窗体空白处,在右下角属性表格中,找到KeyPreview属性,将值设置为true
2.4为工程添加新类,名称:
Block.cs
双击新类Block.cs,进入编辑代码,添加引用
usingSystem.Drawing;
usingSystem.Windows.Forms;
2.5编辑类SnakeNode中代码,如下内容:
namespaceRussiaBlock
{
publicclassBlock
{
publicBlock(Controlcon,intleftBorder,intbottomBorder,intunitPix,intshapeNO,PointfirstPos,Colorcolor)
{
this.con=con;
this.leftBorder=leftBorder;
this.bottomBorder=bottomBorder;
this.unitPix=unitPix;
this.SetPos(shapeNO,firstPos);
this.color=color;
this.huji=newbool[leftBorder+1,bottomBorder+1];
this.iori=newColor[leftBorder+1,bottomBorder+1];
this.lastPos=newPoint[4];
}
privateintshapeNO;//形状号
privateControlcon;//绘图控件
privatePoint[]pos;//当前位置
privatePoint[]lastPos;//上一次位置
privateintleftBorder;//左边界
privateintbottomBorder;//下边界
privateintunitPix;//每块象素数
privateintblockNum=0;
privateintrowDelNum=0;
privatebool[,]huji;
privateColor[,]iori;
privateColorcolor;//当前块颜色
publicvoidEraseLast()
{
foreach(Pointpinthis.lastPos)
{
this.con.Invalidate(newRectangle(p.X*unitPix,p.Y*unitPix,unitPix+1,unitPix+1));
}
}
privatevoidSetLastPos()
{
for(inti=0;i{
this.lastPos[i]=this.pos[i];
}
}
privatevoidSetPos(intshapeNO,PointfirstPos)
{
this.shapeNO=shapeNO;
this.pos=newPoint[4];
pos[0]=firstPos;
switch(shapeNO)
{
case1:
pos[1]=newPoint(firstPos.X+1,firstPos.Y);
pos[2]=newPoint(firstPos.X,firstPos.Y+1);
pos[3]=newPoint(firstPos.X+1,firstPos.Y+1);
break;
case2:
pos[1]=newPoint(firstPos.X+1,firstPos.Y);
pos[2]=newPoint(firstPos.X+2,firstPos.Y);
pos[3]=newPoint(firstPos.X+3,firstPos.Y);
break;
case3:
pos[1]=newPoint(firstPos.X+1,firstPos.Y);
pos[2]=newPoint(firstPos.X+1,firstPos.Y+1);
pos[3]=newPoint(firstPos.X+2,firstPos.Y);
break;
case4:
pos[1]=newPoint(firstPos.X+1,firstPos.Y);
pos[2]=newPoint(firstPos.X+1,firstPos.Y+1);
pos[3]=newPoint(firstPos.X+2,firstPos.Y+1);
break;
case5:
pos[1]=newPoint(firstPos.X+1,firstPos.Y);
pos[2]=newPoint(firstPos.X+1,firstPos.Y-1);
pos[3]=newPoint(firstPos.X+2,firstPos.Y-1);
break;
case6:
pos[1]=newPoint(firstPos.X,firstPos.Y+1);
pos[2]=newPoint(firstPos.X+1,firstPos.Y);
pos[3]=newPoint(firstPos.X+2,firstPos.Y);
break;
default:
pos[1]=newPoint(firstPos.X+1,firstPos.Y);
pos[2]=newPoint(firstPos.X+2,firstPos.Y);
pos[3]=newPoint(firstPos.X+2,firstPos.Y+1);
break;
}
}
privateboolCanMove(intdirection)
{
boolcanMove=true;
if(direction==0)
{
foreach(Pointpinthis.pos)
{
if(p.X-1<0||this.huji[p.X-1,p.Y])
{
canMove=false;
break;
}
}
}
elseif(direction==1)
{
foreach(Pointpinthis.pos)
{
if(p.X+1>this.leftBorder||this.huji[p.X+1,p.Y])
{
canMove=false;
break;
}
}
}
else
{
foreach(Pointpinthis.pos)
{
if(p.Y+1>this.bottomBorder||this.huji[p.X,p.Y+1])
{
canMove=false;
break;
}
}
}
returncanMove;
}
privateboolCanRotate(Point[]pos)
{
boolcanRotate=true;
foreach(Pointpinpos)
{
if(p.X<0||p.X>this.leftBorder||p.Y<0||p.Y>this.bottomBorder||this.huji[p.X,p.Y])
{
canRotate=false;
break;
}
}
if(canRotate==true)
this.SetLastPos();
returncanRotate;
}
privatevoidDelRows()
{
intcount=0;
inthighRow=20;
intlowRow=-1;
int[]delRow={-1,-1,-1,-1};
foreach(Pointpinthis.pos)
{
if(p.Y==highRow||p.Y==lowRow)
continue;
inti;
for(i=0;iif(huji[i,p.Y]==false)
break;
if(i==this.huji.GetLength(0))
{
delRow[count]=p.Y;
if(p.YhighRow=p.Y;
if(p.Y>lowRow)
lowRow=p.Y;
count++;
}
}
if(count>0)
{
//-----------------------------------------------------------------
Graphicsgra=con.CreateGraphics();
foreach(Pointpinthis.lastPos)
{
gra.FillRectangle(newSolidBrush(con.BackColor),p.X*this.unitPix,p.Y*unitPix,25,25);
}
foreach(Pointpinthis.pos)
{
this.DrawOne(p.X,p.Y,this.color,gra);
}
foreach(intiindelRow)
{
if(i>0)
{
for(intj=0;j{
gra.FillRectangle(newSolidBrush(Color.FromArgb(60,Color.Black)),j*this.unitPix,i*unitPix,25,25);
}
}
}
System.Threading.Thread.CurrentThread.Join(180);
//-----------------------------------------------------------------
if(count==2&&lowRow-highRow>1)
{
for(inti=lowRow;i>highRow+1;i--)
{
for(intj=0;j{
this.huji[j,i]=this.huji[j,i-1];
this.iori[j,i]=this.iori[j,i-1];
}
}
for(inti=highRow;i>=count;i--)
{
for(intj=0;j{
this.huji[j,i+1]=this.huji[j,i-1];
this.iori[j,i+1]=this.iori[j,i-1];
}
}
}
elseif(count==3&&lowRow-highRow>2)
{
intmidRow=-1;
foreach(introwindelRow)
{
if(row!
=highRow&&row!
=lowRow)
{
midRow=row;
break;
}
}
for(intj=0;j{
this.huji[j,lowRow]=this.huji[j,lowRow+highRow-midRow];
}
for(inti=highRow;i>=count;i--)
{
for(intj=0;j{
this.huji[j,i+2]=this.huji[j,i-1];
this.iori[j,i+2]=this.iori[j,i-1];
}
}
}
else
{
for(inti=lowRow;i>=count;i--)
{
for(intj=0;j{
this.huji[j,i]=this.huji[j,i-count];
this.iori[j,i]=this.iori[j,i-count];
}
}
}
for(inti=0;i{
for(intj=0;j{
this.huji[j,i]=false;
}
}
con.Invalidate(newRectangle(0,0,con.Width,(lowRow+1)*this.unitPix));
this.rowDelNum+=count;
}
}
publicvoidFixBlock()
{
this.blockNum++;
foreach(Pointpinthis.pos)
{
this.huji[p.X,p.Y]=true;
this.iori[p.X,p.Y]=this.color;
}
this.DelRows();
}
publicboolGeneBlock(intshapeNO,PointfirstPos,Colorcolor)
{
this.SetLastPos();
this.EraseLast();
this.SetPos(shapeNO,firstPos);
if(!
this.CanRotate(this.pos))
{
this.pos=null;
returnfalse;
}
else
{
this.color=color;
returntrue;
}
}
publicboolRotate()
{
boolrotated=true;
Point[]temp={pos[0],pos[1],pos[2],pos[3]};
switch(this.shapeNO)
{
case1:
rotated=false;
break;
case2:
temp[0].Offset(2,2);
temp[1].Offset(1,1);
temp[3].Offset(-1,-1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(2,2);
this.pos[1].Offset(1,1);
this.pos[3].Offset(-1,-1);
this.shapeNO=8;
}
else
rotated=false;
break;
case3:
temp[0].Offset(1,-1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(1,-1);
this.shapeNO=9;
}
else
rotated=false;
break;
case4:
temp[0].Offset(2,0);
temp[1].Offset(0,2);
if(this.CanRotate(temp))
{
this.pos[0].Offset(2,0);
this.pos[1].Offset(0,2);
this.shapeNO=12;
}
else
{
rotated=false;
}
break;
case5:
temp[2].Offset(-1,0);
temp[3].Offset(-1,2);
if(this.CanRotate(temp))
{
this.pos[2].Offset(-1,0);
this.pos[3].Offset(-1,2);
this.shapeNO=13;
}
else
{
rotated=false;
}
break;
case6:
temp[0].Offset(1,1);
temp[1].Offset(2,0);
temp[3].Offset(-1,-1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(1,1);
this.pos[1].Offset(2,0);
this.pos[3].Offset(-1,-1);
this.shapeNO=14;
}
else
{
rotated=false;
}
break;
case7:
temp[0].Offset(1,1);
temp[2].Offset(-1,-1);
temp[3].Offset(0,-2);
if(this.CanRotate(temp))
{
this.pos[0].Offset(1,1);
this.pos[2].Offset(-1,-1);
this.pos[3].Offset(0,-2);
this.shapeNO=17;
}
else
{
rotated=false;
}
break;
case8:
temp[0].Offset(-2,-2);
temp[1].Offset(-1,-1);
temp[3].Offset(1,1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(-2,-2);
this.pos[1].Offset(-1,-1);
this.pos[3].Offset(1,1);
this.shapeNO=2;
}
else
rotated=false;
break;
case9:
temp[2].Offset(-1,-1);
if(this.CanRotate(temp))
{
this.pos[2].Offset(-1,-1);
this.shapeNO=10;
}
else
rotated=false;
break;
case10:
temp[3].Offset(-1,1);
if(this.CanRotate(temp))
{
this.pos[3].Offset(-1,1);
this.shapeNO=11;
}
else
rotated=false;
break;
case11:
temp[0].Offset(-1,1);
temp[2].Offset(1,1);
temp[3].Offset(1,-1);
if(this.CanRotate(temp))
{
this.pos[0].Offset(-1,1);
this.pos[2].Offset(1,1);
this.pos[3].Offset(1,-1);
this.shapeNO=3;
}
else
rotated=false;
break;
case12:
temp[0].Offset(-2,0);
temp[0].Offset(0,-2);
if(this.CanRotate(temp))
{
this.pos[0].Offset(-2,0);
this.pos[1].Offset(0,-2);
this.shapeNO=4;
}
else
{
rotated=false;
}
break;
case1