C#程序设计实训报告-俄罗斯方块.doc_第1页
C#程序设计实训报告-俄罗斯方块.doc_第2页
C#程序设计实训报告-俄罗斯方块.doc_第3页
C#程序设计实训报告-俄罗斯方块.doc_第4页
C#程序设计实训报告-俄罗斯方块.doc_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

c#程序设计实训报告题目:俄罗斯方块 专 业_计算机科学与技术 _年级班别_ 计算机09-2班_ 学 号 学生姓名_ _指导教师_ 成 绩 2012 年 1 月 目 录一 系统设计要求31.1 课题分析错误!未定义书签。1.2 设计环境31.3 设计思路3二 课题总体框架设计32.1程序流程图42.2类的结构图5三 课题实现63.1程序主界面63.2 开始游戏界面63.3 游戏结束界面73.4 暂停游戏界面73.5使用说明界面.83.6 关键程序代码8四 总结214.1设计总结214.2 设计体会2222一、系统设计要求1.1 课题分析本游戏系统是利用c#实现的, 是制作为我们所熟悉的非常简单的俄罗斯方块游戏,该系统能实现的具体功能如下:1) 能简便的开始游戏,游戏中的方块的功能与日常我们所熟悉的游戏的功能一致,各种块的设置也一致,包括方块的旋转,加速下降,左右移动,满行消去,满行消去自动加分,以及到顶游戏结束等功能;2) 能够通过对话框窗体说明各个功能的使用说明,以及一些其他功能。3) 界面简洁美观,简单易用。跟其他一般的游戏相差不大。1.2 设计环境本程序选择visual studio 2010作为实验环境。1.3 设计思路用面向对象的方法分析系统对于俄罗斯方块的程序制作,我们可以定义一个或者几个类,专门来描述俄罗斯方块,在这个类中,包含与之相关的方法、属性和字段,通过封装,实现其业务逻辑。其中,每一个俄罗斯方块都有相同的特征,由4个小正方形构成,有旋转,左右移动,下落的动作,整行被填满除去并计算分数而构成行的小正方体块。基中块的形状类型有7种:田、一、l、倒l、z、倒z、上。在窗口中通过调用主窗体form1当中的菜单栏来设置游戏的开始、暂停、结束、重新开始以及推出程序。还可以通过其菜单中游戏说明选项来查看游戏各个键的使用说明,还可调用帮助菜单来查看版权说明。二、课题总体框架设计 2.1、 程序流程图开始窗口初始化读取游戏开始游戏开启游戏时钟随机形成方块判断是否可移旋转左移右移加速下降暂停结束绘制方块是否越顶是否满行消行结束加分2.2、 类的结构图三、课题实现3.1程序主界面3.2 开始游戏界面3.3游戏结束3.4暂停游戏3.5使用说明界面和版权界面3.6关键程序代码1、form1类1) 构造函数,设定当前运行的方块,下一个即将出现的方块,方块产生的位置,玩家积分,游戏开关等。public partial class form1 : form private block currentblock; /当前在运行的方块private block nextblock; /下一个即将出现的方块private point startlocation = new point(bianjie.singlesquaresize * 8, 0); /方块产生的位置private int score = 0; /玩家积分private bool stillruning = false; /游戏运行开关2) 键盘操作:用来选择方块的移动方向,是向右移动,向左移动,向下加速,旋转,还是暂停。/*键盘操作*/private void form1_keydown(object sender, keyeventargs e) switch (e.keycode) case keys.right: currentblock.right() ; break;/向右移动 case keys.left: currentblock.left() ; break; /向左移动 case keys.up: currentblock.rotate(); break; /旋转 case keys.down: while (currentblock.down() ; break; /向下加速 case keys.space: /空格:暂停 timer1.enabled = !timer1.enabled; if (!timer1.enabled) showmsg(暂 停); else msg.sendtoback(); break; picback.focus(); 3) 时钟触发处理函数,使方块自动的向下移动,每1秒使方块向下移动一次 /*游戏时钟*/ private void timer1_tick(object sender, eventargs e) if (!stillruning) return; /检测是否还可以下移 if (!currentblock.down() if (currentblock.top() = 0) /如果到顶则游戏结束 showmsg(game over!); stillruning = false; timer1.stop(); return; /否则计算分数并继续 int eraselines = bianjie.checklines(); if (eraselines 0) score += bianjie.width * eraselines; t_score.text = score.tostring(); picback.invalidate(); application.doevents(); bianjie.redraw(); /产生下一个block currentblock = new block(startlocation, nextblock.blocktype); currentblock.draw(bianjie.winhandle); pic_preview.refresh();nextblock = new block(new point(50, 50), block.blocktypes.undefined); nextblock.draw(pic_preview.handle); currentblock.down(); 4) 对窗口进行重绘 /*窗口重绘*/ private void form1_activated(object sender, eventargs e) picback.invalidate(); application.doevents(); bianjie.redraw(); 2、singleblock类1) 构造单个方块的尺寸,颜色,前景色,背景色public singleblock(size initsize,color initforecolor,color initbackcolor) size = initsize; forecolor = initforecolor; backcolor = initbackcolor; 2) 画方块,用gdi+绘画,画出填充正方形 /画方块 public void draw(system.intptr winhandle) graphics g = graphics.fromhwnd(winhandle); graphicspath gp = new graphicspath(); rectangle rect = new rectangle(location, size); gp.addrectangle(rect); color surroundcolor = new color backcolor ; pathgradientbrush pgb = new pathgradientbrush(gp); pgb.centercolor = forecolor; pgb.surroundcolors = surroundcolor; g.fillpath(pgb, gp); /擦除方块 public void erase(system.intptr winhandle) graphics g = graphics.fromhwnd(winhandle); rectangle rect = new rectangle(location,size); g.fillrectangle(new solidbrush(bianjie.backcolor),rect); 3、block类1)随机产生方块形状,并设置四个方块的颜色public block(point thislocation,blocktypes btype) /当blocktype为undefined时,随机产生方块形状 random rand=new random(); if (btype = blocktypes.undefined) blocktype = (blocktypes)(rand.next(7) + 1); else blocktype = btype; /设置四小方块的颜色 int i=(int)blocktype-1; forecolor = bianjie.blockforecolori; backcolor = bianjie.blockbackcolori; size singlesquares=new size(singlesquaresize,singlesquaresize); singlesquare1 = new singleblock(singlesquares, forecolor, backcolor); singlesquare2 = new singleblock(singlesquares, forecolor, backcolor); singlesquare3 = new singleblock(singlesquares, forecolor, backcolor); singlesquare4 = new singleblock(singlesquares, forecolor, backcolor);2)设置小方块的位置,组合成指定形状的方块 /设置小方块的位置,组合成指定形状的一个大方块 switch (blocktype) case blocktypes.singlesquare: /组合成正方形 singlesquare1.location = new point(thislocation.x, thislocation.y); singlesquare2.location = new point(thislocation.x + singlesquaresize, thislocation.y); singlesquare3.location = new point(thislocation.x,thislocation.y+singlesquaresize);singlesquare4.location = new point(thislocation.x+singlesquaresize,thislocation.y+singlesquaresize); break;case blocktypes.line: /组合成线形 singlesquare1.location = new point(thislocation.x, thislocation.y); singlesquare2.location = new point(thislocation.x + singlesquaresize, thislocation.y); singlesquare3.location = new point(thislocation.x + 2 * singlesquaresize, thislocation.y); singlesquare4.location = new point(thislocation.x + 3 * singlesquaresize, thislocation.y); break; case blocktypes.j: /组合成j形 singlesquare1.location = new point(thislocation.x + singlesquaresize, thislocation.y); singlesquare2.location = new point(thislocation.x + singlesquaresize, thislocation.y + singlesquaresize); singlesquare3.location = new point(thislocation.x + singlesquaresize, thislocation.y + 2 * singlesquaresize); singlesquare4.location = new point(thislocation.x, thislocation.y + 2 * singlesquaresize); break; case blocktypes.l: /组合成l形 singlesquare1.location = new point(thislocation.x, thislocation.y); singlesquare2.location = new point(thislocation.x, thislocation.y + singlesquaresize); singlesquare3.location = new point(thislocation.x, thislocation.y + 2 * singlesquaresize); singlesquare4.location = new point(thislocation.x + singlesquaresize, thislocation.y + 2 * singlesquaresize); break; case blocktypes.t: /组合成t形 singlesquare1.location = new point(thislocation.x, thislocation.y); singlesquare2.location = new point(thislocation.x + singlesquaresize, thislocation.y); singlesquare3.location = new point(thislocation.x + 2*singlesquaresize, thislocation.y); singlesquare4.location = new point(thislocation.x + singlesquaresize, thislocation.y +singlesquaresize); break; case blocktypes.z: /组合成z形 singlesquare1.location = new point(thislocation.x, thislocation.y); singlesquare2.location = new point(thislocation.x + singlesquaresize, thislocation.y); singlesquare3.location = new point(thislocation.x + singlesquaresize, thislocation.y + singlesquaresize); singlesquare4.location = new point(thislocation.x + 2*singlesquaresize, thislocation.y + singlesquaresize); break;case blocktypes.s: /组合成s形 singlesquare1.location = new point(thislocation.x, thislocation.y + singlesquaresize); singlesquare2.location = new point(thislocation.x + singlesquaresize, thislocation.y + singlesquaresize); singlesquare3.location = new point(thislocation.x + singlesquaresize, thislocation.y); singlesquare4.location = new point(thislocation.x + 2 * singlesquaresize, thislocation.y); break; /*画方块*/ public void draw(system.intptr winhandle) singlesquare1.draw(winhandle); singlesquare2.draw(winhandle); singlesquare3.draw(winhandle); singlesquare4.draw(winhandle); /*擦方块*/ public void erase(system.intptr winhandle) singlesquare1.erase(winhandle); singlesquare2.erase(winhandle); singlesquare3.erase(winhandle); singlesquare4.erase(winhandle); 3) 移动方块 /*移动*/ public bool down() /检测是否可以下移 if (bianjie.isempty(singlesquare1.location.x / singlesquaresize, singlesquare1.location.y / singlesquaresize + 1) & bianjie.isempty(singlesquare2.location.x / singlesquaresize, singlesquare2.location.y / singlesquaresize + 1) &bianjie.isempty(singlesquare3.location.x / singlesquaresize, singlesquare3.location.y / singlesquaresize + 1) &bianjie.isempty(singlesquare4.location.x / singlesquaresize, singlesquare4.location.y / singlesquaresize + 1) erase(bianjie.winhandle); singlesquare1.location = new point(singlesquare1.location.x, singlesquare1.location.y + singlesquaresize); singlesquare2.location = new point(singlesquare2.location.x, singlesquare2.location.y + singlesquaresize);singlesquare3.location = new point(singlesquare3.location.x, singlesquare3.location.y + singlesquaresize); singlesquare4.location = new point(singlesquare4.location.x, singlesquare4.location.y + singlesquaresize); draw(bianjie.winhandle); return true; else /如果不能下移了 bianjie.stopsinglesquare(singlesquare1, singlesquare1.location.x / singlesquaresize, singlesquare1.location.y / singlesquaresize);bianjie.stopsinglesquare(singlesquare2, singlesquare2.location.x / singlesquaresize, singlesquare2.location.y / singlesquaresize); bianjie.stopsinglesquare(singlesquare3, singlesquare3.location.x / singlesquaresize, singlesquare3.location.y / singlesquaresize); bianjie.stopsinglesquare(singlesquare4, singlesquare4.location.x / singlesquaresize, singlesquare4.location.y / singlesquaresize); return false; /表示可以弹出下一个block了 public bool left() /检测是否可以左移 if (bianjie.isempty(singlesquare1.location.x / singlesquaresize-1, singlesquare1.location.y / singlesquaresize) &bianjie.isempty(singlesquare2.location.x / singlesquaresize-1, singlesquare2.location.y / singlesquaresize) &bianjie.isempty(singlesquare3.location.x / singlesquaresize-1, singlesquare3.location.y / singlesquaresize) & bianjie.isempty(singlesquare4.location.x / singlesquaresize-1, singlesquare4.location.y / singlesquaresize) erase(bianjie.winhandle); singlesquare1.location = new point(singlesquare1.location.x - singlesquaresize, singlesquare1.location.y); singlesquare2.location = new point(singlesquare2.location.x - singlesquaresize, singlesquare2.location.y); singlesquare3.location = new point(singlesquare3.location.x - singlesquaresize, singlesquare3.location.y); singlesquare4.location = new point(singlesquare4.location.x - singlesquaresize, singlesquare4.location.y); draw(bianjie.winhandle); return true; else /如果不能左移了 return false; public bool right() /检测是否可以右移 if (bianjie.isempty(singlesquare1.location.x / singlesquaresize +1, singlesquare1.location.y / singlesquaresize) &bianjie.isempty(singlesquare2.location.x / singlesquaresize +1, singlesquare2.location.y / singlesquaresize) &bianjie.isempty(singlesquare3.location.x / singlesquaresize +1, singlesquare3.location.y / singlesquaresize) &bianjie.isempty(singlesquare4.location.x / singlesquaresize +1, singlesquare4.location.y / singlesquaresize) erase(bianjie.winhandle); singlesquare1.location = new point(singlesquare1.location.x + singlesquaresize, singlesquare1.location.y ); singlesquare2.location = new point(singlesquare2.location.x + singlesquaresize, singlesquare2.location.y); singlesquare3.location = new point(singlesquare3.location.x + singlesquaresize, singlesquare3.location.y); singlesquare4.location = new point(singlesquare4.location.x + singlesquaresize, singlesquare4.location.y); draw(bianjie.winhandle); return true; else /如果不能右移了 return false; 4) 旋转方块/*旋转block*/public void rotate() /保存每个小块的位置 point oldposition1 = singlesquare1.location; point oldposition2 = singlesquare2.location; point oldposition3 = singlesquare3.location; point oldposition4 = singlesquare4.location; /保存当前的方向 rotatedirections oldrotation = myrotation; /开始试着旋转方块,旋转方向:顺时针方向 旋转中心点为形状拐点 erase(bianjie.winhandle); switch(blocktype) case blocktypes.singlesquare: break; case blocktypes.line: /直线的旋转只有两种方向 switch (myrotation) case rotatedirections.north: myrotation = rotatedirections.east; singlesquare1.location = new point(singlesquare2.location.x-singlesquaresize,singlesquare2.location.y); singlesquare3.location = new point(singlesquare2.location.x+singlesquaresize,singlesquare2.location.y); singlesquare4.location = new point(singlesquare2.location.x + 2 * singlesquaresize, singlesquare2.location.y); break; case rotatedirections.east: myrotation = rotatedirections.north; singlesquare1.location = new point(singlesquare2.location.x,singlesquare2.location.y-singlesquaresize); singlesquare3.location = new point(singlesquare2.location.x, singlesquare2.location.y +singlesquaresize); singlesquare4.location = new point(singlesquare2.location.x, singlesquare2.location.y + 2 * singlesquaresize); break; break;case blocktypes.j: /j形方块有四种旋转方向 switch (myrotation) case rotatedirections.north: myrotation = rotatedirections.east; singlesquare1.location = new point(singlesquare3.location.x+2*singlesquaresize,singlesquare3.location.y); singlesquare2.location = new point(singlesquare3.location.x+singlesquaresize,singlesquare3.location.y); singlesquare4.location = new point(singlesquare3.location.x,singlesquare3.location.y-singlesquaresize); break; case rotatedirections.east: myrotation = rotatedirections.south; singlesquare1.location = new point(singlesquare3.location.x,singlesquare3.location.y+2*singlesquaresize); singlesquare2.location = new point(singlesquare3.location.x,singlesquare3.location.y+singlesquaresize); singlesquare4.location = new point(singlesquare3.location.x+singlesquaresize,singlesquare3.location.y); break; case rotatedirections.south: myrotation = rotatedirections.west; singlesquare1.location = new point(singlesquare3.location.x-2*singlesquaresize,singlesquare3.location.y); singlesquare2.location = new point(singlesquare3.location.x-singlesquaresize,singlesquare3.location.y); singlesquare4.location = new point(singlesquare3.location.x,singlesquare3.location.y+singlesquaresize); break; case rotatedirections.west: myrotation = rotatedirections.north; singlesquare1.location = new point(singlesquare3.location.x,singlesquare3.location.y-2*singlesquaresize); singlesquare2.location = new point(singlesquare3.location.x,singlesquare3.location.y-singlesquaresize); singlesquare4.location = new point(singlesquare3.location.x-singlesquaresize,singlesquare3.location.y); break; break;case blocktypes.l: /l形方块有四种旋转方向 switch (myrotation) case rotatedirections.north: myrotation = rotatedirections.east; singlesquare1.location = new point(singlesquare3.location.x+2*singlesquaresize,singlesquare3.location.y); singlesquare2.location = new point(singlesquare3.location.x+singlesquaresize, singlesquare3.location.y); singlesquare4.location = new point(singlesquare3.location.x, singlesquare3.location.y+singlesquaresize); break; case rotatedirections.east: myrotation = rotatedirections.south; singlesquare1.location = new point(singlesquare3.location.x, singlesquare3.location.y + 2 * singlesquaresize); singlesquare2.location = new point(singlesquare3.location.x, singlesquare3.location.y + singlesquaresize); singlesquare4.location = new point

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论