C课程设计俄罗斯方块.docx
- 文档编号:30146293
- 上传时间:2023-08-05
- 格式:DOCX
- 页数:40
- 大小:86.63KB
C课程设计俄罗斯方块.docx
《C课程设计俄罗斯方块.docx》由会员分享,可在线阅读,更多相关《C课程设计俄罗斯方块.docx(40页珍藏版)》请在冰豆网上搜索。
C课程设计俄罗斯方块
C课程设计--俄罗斯方块
C#课程实训报告
(2013--2014年度第1学期)
专业
软件工程
学生姓名
班级
学号
指导教师
完成日期
C#课程实训报告
1设计目的
本课程设计是在学完教学大纲规定的全部内容、完成所有实践环节的基础上,旨在深化学生学习的C#语言课程基本知识,进一步掌握C#语言程序设计方法,提高分析问题、解决问题的综合应用能力。
2设计内容
俄罗斯方块游戏的编制。
3实现方法
3.1程序概述
3.1.1游戏的功能
它的基本功能就是要求玩家先登录窗体输入用户名(ldq)和密码(afafa),如果正确则登录游戏,如果输错三次则退出游戏。
登录游戏后玩家通过移动或旋转自己窗口内落下的方块,方块在一行堆满后就可以消掉;如果方块堆积至窗口顶端,则游戏结束。
系统默认设置使用右边的窗口,用光标操作:
左箭头左移一格,右箭头右移一格,上箭头旋转方块,下箭头方块丢下。
用户还可以定义自己习惯的按键来操作游戏。
3.1.2游戏的预览
游戏运行的登录窗口如图3-1所示。
图3-1登录窗体
游戏运行的主操作界面如图3-2所示。
图3-2主操作界面
单击[设置]按钮,出现一个对话框,可以设置游戏规则。
在操作设置界面中可以进行键盘设置,还可以进行环境设置,设置游戏的难度。
操作界面如图3-3所示。
图3-3操作设置界面
[开始级别](默认是一级),级别是1-10,级别越高方块下落速度越快。
3.2游戏的概要设计
3.2.1游戏实现方案
屏幕上有一个X列Y层的区域,计算机自动产生多重不同形状的方块,并出现在区域的最顶端;然后每过一个固定时间向下落一层,玩家可以使用键盘控制方块向三个方向移动和顺(逆)时针旋转90°。
不过前提是方块移动和旋转后的位置是空的,并且碰触不到提前设置好的边界。
当方块落到区域底部或者另一个方块之上是将停止移动。
该方块降落后检查每行是否有满行的现象,如果有,这一行全部取消方块,将上一层的方块重新画到这一层。
随后新的方块产生,当新方块无处放置时,游戏结束。
3.3.2游戏逻辑设计
首先是定义与游戏相关的数据的存储方法,分为两个主要的部分:
一个是当前的方块,需要保存位置、当前的旋转状态;另一个是屏幕区域的状态,这可以用一个二维数组来表示。
其次,分析得到按键的含义,做出响应,响应模块:
“左”、“右”、“下”、“移动”和“旋转”几个模块,负责改变当前方块的状态的数据;画方块模块:
位置移动后,把方块画到新的位置上;检查模块:
在每次改变状态之前,对方块企图占用的区域进行检查。
如果无处可放,则此次移动失败。
如果方块到区域底端了,则应该固定在当前位置,这可以调用前面的画方块模块来实现,同时再再产生一个方块。
这样还需要写一个产生方块的模块。
每次当方块到底的时候,都需要检查一下是否有新的层排满了,如果有,则应该消层。
这就又需要写两个模块,检车是否排满和消层。
产生新方块时也需要检查其产生的位置,如果这个位置已被占用,则游戏结束,这需要调用前面的检查模块来实现。
最后检查系统时间,规定每过一个指定时间间隔就调用一次方块下落模块。
3.3游戏的详细设计及编码
3.3.1主界面设计编码
主界面的作用就是显示本游戏所有的功能菜单项,提供游戏的而显示界面。
在本界面中共设计了5种控件,通过这些控件,玩家可以方便的控制游戏,各个控件的名称、作用和类型如表3-1所示。
表3-1主界面的控件设计
控件类型
控件名称
作用
menustrip
退出ToolStripMenuItem
退出
打开命令ToolStripMenuItem
操作控制
打开音乐ToolStripMenuItem
打开音乐
帮助ToolStripMenuItem
帮助
panel
Panel1
显示游戏中当前所有方块
Panel2
显示要出现的下一个方块
Label
Label1
显示级别
Label3
显示块数
Label5
显示行数
Label7
显示手速
Botton
Botton1
开始
Botton2
打开游戏设置界面
Botton3
暂停
Windowsmediaplayer
axMediaPlayer1
音乐播放器
另外,在界面中还设置了1个TextBox控件。
在界面操作中,把焦点一直防止在这个控件上,方便用户用键盘操作游戏方块。
在游戏中需要保存用户自定义的一些设置,本游戏中把设置保存到setting.cob文件中,并在游戏加载的时候调用。
主界面Load事件的代码如例程3-1所示,游戏设置保存代码如例程3-2所示。
例程3-1主界面Load代码,用到Initiate函数进行操作设置初始化。
privatevoidForm1_Load(objectsender,EventArgse)
{
this.Initiate();
}
privatevoidInitiate()
{
try
{
XmlDocumentdoc=newXmlDocument();
doc.Load("c:
\\setting.ini");
XmlNodeListnodes=doc.DocumentElement.ChildNodes;
this.startLevel=Convert.ToInt32(nodes[0].InnerText);
keys=newKeys[5];
for(inti=0;i { KeysConverterkc=newKeysConverter(); this.keys[i]=(Keys)(kc.ConvertFromString(nodes[2].ChildNodes[i].InnerText)); } } catch { this.trans=false; keys=newKeys[5]; keys[0]=Keys.Left; keys[1]=Keys.Right; keys[2]=Keys.Down; keys[3]=Keys.Up; keys[4]=Keys.Space; this.level=1; this.startLevel=1; } this.timer1.Interval=500-50*(level-1);//时钟 this.label1.Text="级别: "+this.startLevel; if(trans) { this.TransparencyKey=Color.Black; } } [设置]按钮是进入操作设置,代码如下 privatevoidbutton2_Click_1(objectsender,EventArgse) { if(! paused) { this.atPause=DateTime.Now; this.paused=true; this.timer1.Stop(); } sform.SetOptions(this.keys,this.startLevel,this.trans); sform.DialogResult=DialogResult.Cancel; sform.ShowDialog(); if(sform.DialogResult==DialogResult.OK) { sform.GetOptions(refthis.keys,refthis.startLevel,refthis.trans); this.level=this.startLevel; this.label1.Text="级别"+this.level; this.timer1.Interval=500-50*(level-1); if(this.trans) { this.TransparencyKey=Color.Black; } else this.TransparencyKey=Color.Transparent; } this.paused=false; this.pauseTime+=DateTime.Now-this.atPause; this.timer1.Start(); this.textBox1.Focus(); } [开始]按钮的事件是设置label的显示文本、开始计时和时间间隔,并调用生成方块的函数。 主要代码如例程3-3所示。 例程[开始]按钮事件代码 privatevoidStart() { this.block=null; this.nextBlock=null; this.label4.Text=“手速”: 0"; this.label3.Text=“块数”: 0"; this.label2.Text=“行数”: 0"; this.label5.Text=“成绩”: 0"; this.score=0; this.label1.Text="级别”: "+this.startLevel; this.level=this.startLevel; this.timer1.Interval=500-50*(level-1);//方块生成的时间间隔 this.paused=false; this.failed=false; this.panel1.Invalidate(); this.panel2.Invalidate(); this.nextShapeNO=0; this.CreateBlock();//生成当前方块 this.CreateNextBlock(); this.timer1.Enabled=true; this.atStart=DateTime.Now; this.pauseTime=newTimeSpan(0); } [暂停]按钮的事件是对计时器进行暂停控制,如果已经处于暂停状态,则计时器开始计时 privatevoidbutton3_Click(objectsender,EventArgse) { if(! this.failed) { if(paused) { this.pauseTime+=DateTime.Now-this.atPause; paused=false; this.timer1.Start(); } else { this.atPause=DateTime.Now; paused=true; this.timer1.Stop(); } } this.textBox1.Focus(); } [其他]还有需要一些函数辅助这些控件才能完成功能: 在Start函数中,需要调用一个函数创建新方块(creatblock)和如果能够顺利执行将调用的创建下一个方块(creatnextblock) privateboolCreateBlock() { PointfirstPos; Colorcolor; if(this.nextShapeNo==0) { Randomrand=newRandom(); this.nextShapeNo=rand.Next(1,8); } switch(this.nextShapeNo) { case1: //田 firstPos=newPoint(4,0); color=Color.Turquoise; break; case2: //一 firstPos=newPoint(3,0); color=Color.Red; break; case3: //土 firstPos=newPoint(4,0); color=Color.Silver; break; case4: //z firstPos=newPoint(4,0); color=Color.LawnGreen; break; case5: //倒z firstPos=newPoint(4,1); color=Color.DodgerBlue; break; case6: //L firstPos=newPoint(4,0); color=Color.Yellow; break; default: //倒L firstPos=newPoint(4,0); color=Color.Salmon; break; } if(this.block==null) { block=newBlock(this.panel1,9,19,25,this.nextShapeNo,firstPos,color); } else { if(! block.GeneBlock(this.nextShapeNo,firstPos,color)) { returnfalse; } } block.EraseLast(); block.Move (2); returntrue; } privatevoidCreateNextBlock() { Randomrand=newRandom(); this.nextShapeNo=rand.Next(1,8); Pointfirstpos; Colorcolor; switch(this.nextShapeNo) { case1: //田¬? firstpos=newPoint(1,0); color=Color.Turquoise; break; case2: //一°? firstpos=newPoint(0,1); color=Color.Red; break; case3: //土ª¨¢ firstpos=newPoint(0,0); color=Color.Silver; break; case4: //z firstpos=newPoint(0,0); color=Color.LawnGreen; break; case5: //倒Ì1z firstpos=newPoint(0,1); color=Color.DodgerBlue; break; case6: //L firstpos=newPoint(0,0); color=Color.Yellow; break; default: //倒Ì1L firstpos=newPoint(0,0); color=Color.SaddleBrown; break; } if(nextblock==null) nextblock=newBlock(this.panel4,3,1,20,this.nextShapeNo,firstpos,color); else { nextblock.GeneBlock(this.nextShapeNo,firstpos,color); nextblock.EraseLast(); } } 3.3.2游戏控制设置设计编码 操作设置界面的作用就是显示本游戏操作键、开始级别设置和音效、是否运行界面为透明的设置。 下面是该界面所需的一些控件及控件实现所要求功能的源代码 表3-1主界面的控件设计 控件类型 控件名称 作用 groupBox groupBox1 盛放操作键设置的容器 groupBox2 盛放开始级别和有无音效是否透明的容器 button button1 确定保存设置 button2 取消本次设置 Label Label1、2、3、4、5、6 显示操作内容和开始级别的文字 textbox textbox1、2、3、4、5 用户自行设置 checkbox checkBox1 音效有无 checkBox2 是否透明 publicpartialclassForm2: Form { privatebooltrans; privateintlevel; privateKeys[]keys; publicForm2() { InitializeComponent(); this.keys=newKeys[5]; } privatevoidChangeKey(TextBoxtextbox,System.Windows.Forms.KeyEventArgse,inti) { if((e.KeyValue>=32&&e.KeyValue<=40)||(e.KeyValue>=45&&e.KeyValue<=46)||(e.KeyValue>=48&&e.KeyValue<=57)||(e.KeyValue>=65&&e.KeyValue<=90)||(e.KeyValue>=96&&e.KeyValue<=107)||(e.KeyValue>=109&&e.KeyValue<=111)||(e.KeyValue>=186&&e.KeyValue<=192)||(e.KeyValue>=219&&e.KeyValue<=222)) { textbox.Text=e.KeyCode.ToString(); this.keys[i]=e.KeyCode; } } 3.3.3登陆界面设计编码 publicpartialclassForm3: Form { publicintcount=0; publicForm3() { InitializeComponent(); } privatevoidbutton1_Click(objectsender,EventArgse) { count++; if(count<=3) { if(textBox1.Text=="ldq"&&textBox2.Text=="afafa") { Form1aform=newForm1(); aform.Show(); } else MessageBox.Show("用户名或密码错误","警告! "); } else Application.Exit(); } 3.3.4游戏方块设计编码 publicclassBlock { privateControlcon;//绘图控件 privateintleftBorder;//左边界 privateintbottomBorder;//下边界 privateintunitPix;//每块像素数 privateintshapeNo;//形状号 privateintblockNum=0;//块数 privateintrowDelNum=0;//消除行数 privatebool[,]huji; privateColor[,]iori; privateColorcolor;//当前块的颜色 privatePoint[]pos;//当前位置 privatePoint[]lastPos;//上一次位置 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]; } 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;//第一个序号为0,则为第一个方块的位置 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,first
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 课程设计 俄罗斯方块