MyBlock实验报告文档_第1页
MyBlock实验报告文档_第2页
MyBlock实验报告文档_第3页
MyBlock实验报告文档_第4页
MyBlock实验报告文档_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

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

文档简介

1、一 需求分析1. 设计目的用C#语言编写一个俄罗斯方块游戏,要能够实现俄罗斯方块的全部功能(即控制游戏开始、暂停、结束,控制(或通关后)方块下降的速度,记分功能,游戏帮助,计算游戏时间等。2. 系统设计要求用visual studio .net 2003/2005编写,能在windows 2000及以上操作系统平台上运行。游戏界面友好,编码简洁,稳定,高效。二 总体设计建立正方形类(class square),用四个小正方形组合成俄罗斯方块的七种形状。用一窗口(class form1:form)作为游戏界面,用工具箱画出游戏功能需要的控件。建立游戏背景类(class gamefield),用于

2、初始化游戏背景和方块颜色,计算游戏得分并消行。建立方块类(class block),实现俄罗斯方块的七种形状,以及他们的移动方向,旋转。游戏界面类负责实现事件的触发和显示游戏时间。帮助窗口(class help)提示玩家怎样操作俄罗斯方块。三 详细设计 系统功能模块图初始化界面窗口并开始计时帮助菜单控制菜单游戏设置菜单操作说明退出慢快较快非常快出较慢退出速度设置类的成员列表: Class square: /组合俄罗斯方块的小正方形 public Point location; /小正方形左上角的坐标 public Size size; /小正方形的大小 public Color foreCol

3、or; /小正方形前景颜色 public Color backColor; /小正方形背景颜色 public Square(Size initSize,Color initForeColor,Color initBackColor); /构造函数 public void Draw(System.IntPtr winHandle); /画小正方形 public void Erase(System.IntPtr winHandle); /擦除小正方形class block: /俄罗斯方块 public Square square1; /组成block的四个小方块 public Square squ

4、are2; public Square square3; public Square square4; private const int squareSize; /小方块的边长 public enum BlockTypes;/一共有7种形状 public BlockTypes blockType; /方块的形状 /七个小方块的颜色数组 private Color foreColor; private Color backColor; /方块的方向 public enum RotateDirections; /东南西北四个方向 public RotateDirections myRotatio

5、n; /旋转方向 public Block(Point thisLocation,BlockTypes bType);/构造函数/带有自定义颜色的方块构造函数 public Block(Point thisLocation, BlockTypes bType,Color fc,Color bc); public void Draw(System.IntPtr winHandle);/画方块public void Erase(System.IntPtr winHandle);/擦方块public bool down();/方块下移 public bool left();/方块左移 public

6、bool right();/方块右移 public void Rotate();/转动public int Top();/检查方块是否到顶端class GameField: /游戏场景设置 public const int width ; /场景的宽,以方块个数为单位 public const int height ; /场景的高,以方块个数为单位 public const int SquareSize ; /每个四分之一小方块的边长 public static Color BackColor; /场景的背景色 public static System.IntPtr winHandle; /场

7、景的句柄 public static Color BlockForeColor; /组成俄罗斯方块的七个小正方形的前景颜色 public static Color BlockBackColor ;/组成俄罗斯方块的七个小正方形的背景颜色 public static bool isChanged; /设置是否被更改的标志位 public static SoundPlayer sound ;/播放声音 public static Square, arriveBlock; /保存已经不能再下落了的方块 public static int arrBitBlock; /位数组:当某个位置有方块时,该行的

8、该位为1 private const int bitEmpty = 0x0; /0000 0000 0000 0000 0000 对应每一格为空 private const int bitFull = 0xFFFFF; /1111 1111 1111 1111 1111对应每一格填满 /检测是否为空 public static bool isEmpty(int x, int y); /检测是否为空格public static void stopSquare(Square sq, int x, int y);/停住方块public static int CheckLines();/记分消行pub

9、lic static void PlaySound(string soundstr);/播放声音public static void Redraw();/重画要落下来的方块class help: /帮助窗口 private System.Windows.Forms.Label label1; /帮助说明 private System.Windows.Forms.Button button1; /关闭窗口按钮private System.Windows.Forms.Label label2; /帮助说明class form1:form /游戏界面窗口 private Block currentB

10、lock; /当前在运行的方块 private Block nextBlock; /下一个即将出现的方块 private Point startLocation ; /方块产生的位置 private int score; /玩家积分 private bool stillRuning ; /游戏运行开关 private enum speeds; /游戏速度,数字越小越快 static private int timeCount; /游戏时间计时 static private int timer3Count; /过关显示提示定时 bool test ; /过每一关时的判断public Form1(

11、);/窗口构造函数private void Form1_Load(object sender, EventArgs e);/载入窗口private void Form1_Activated(object sender, EventArgs e);/激活窗口private void Form1_KeyDown(object sender, KeyEventArgs e);/键盘响应private void timer1_Tick(object sender, EventArgs e);/游戏时钟,控制方块落下速度private void msgShow(string str);/通关提示priv

12、ate void showMsg(string str);/暂停提示private void 开始ToolStripMenuItem1_Click(object sender, EventArgs e);/开始游戏private void beginGame();开始游戏的方法private void 暂停ToolStripMenuItem1_Click(object sender, EventArgs e);/暂停游戏private void 结束ToolStripMenuItem_Click(object sender, EventArgs e);/结束游戏private void 重新开

13、始ToolStripMenuItem_Click(object sender, EventArgs e);/重新开始private void 退出ToolStripMenuItem_Click(object sender, EventArgs e);/退出游戏关闭游戏窗口private void 操作说明ToolStripMenuItem_Click(object sender, EventArgs e);/操作说明private void 较慢ToolStripMenuItem_Click(object sender, EventArgs e);/速度调为较慢private void 慢To

14、olStripMenuItem_Click(object sender, EventArgs e);private void 快ToolStripMenuItem_Click(object sender, EventArgs e);private void 较快ToolStripMenuItem_Click(object sender, EventArgs e);private void 非常快ToolStripMenuItem_Click(object sender, EventArgs e);public void changeChecked(ToolStripMenuItem oo);/

15、速度选择需要用到的方法private void timer2_Tick(object sender, EventArgs e);/游戏计时时钟设置private void timer3_Tick(object sender, EventArgs e);/通关提示时钟设置 private System.Windows.Forms.PictureBox picBackGround; /游戏背景 private System.Windows.Forms.PictureBox pic_preView; /下一个方块的显示 private System.Windows.Forms.Label label

16、1;/“下一个方块:”文本 private System.Windows.Forms.Label label2; /“得分:” private System.Windows.Forms.Label t_score;/显示得分 private System.Windows.Forms.MenuStrip menuStrip1; /界面菜单栏 private System.Windows.Forms.ToolStripMenuItem 游戏设置ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 速度设置ToolStri

17、pMenuItem; private System.Windows.Forms.ToolStripSeparator toolStripMenuItem1; private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 帮助ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 操作说明ToolStripMenuItem; private System.

18、Windows.Forms.ToolStripMenuItem 控制ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 开始ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 暂停ToolStripMenuItem1; private System.Windows.Forms.ToolStripMenuItem 结束ToolStripMenuItem; private System.Windows.Forms.ToolStripMenu

19、Item 重新开始ToolStripMenuItem; private System.Windows.Forms.Timer timer1; /游戏时钟 private System.Windows.Forms.Label msg; /按钮响应文本提示 private System.Windows.Forms.Label label3; /显示第几关 private System.Windows.Forms.Label label4; /显示游戏时间 private System.Windows.Forms.Label label5; /“游戏时间:” private System.Windo

20、ws.Forms.Timer timer2; /游戏时间时钟private System.Windows.Forms.Timer timer3; /通关文本提示停留时钟系统流程图Main()New form1()InitializeComponent()Form_load()Form_Activated ()绘出游戏界面,等待窗口事件 游戏时钟 触发键盘事件触发菜单事件帮助菜单游戏控制菜单游戏设置菜单操作说明重新开始结束暂停开始showMsg.();timer2.stop()退出new help()helps.show()Timer1.stop();timer2.stop();showMsg(

21、);pic_preView.refresh()Timer1.stop();timer2.stop();picBackGroup.refresh();pic_preView.refresh();msg.sendToBack();重画初始窗口;timer1.start();timer2.start();msg.SendToBack();currentBlock.Draw(); nextBlock.Draw();timer1.Start();Timer2.start();beginGame();较快快慢较慢较快ToolStripMenuItem_Click()快ToolStripMenuItem_C

22、lick()慢ToolStripMenuItem_Click()较慢ToolStripMenuItem_Click()速度设置非常快ToolStripMenuItem_Click()Timer1.stop();timer2.stop();this.close()changeChecked(非常快ToolStripMenuItem)erval=(int)speeds.slower()非常快changeChecked(较快ToolStripMenuItem)erval=(int)speeds.faster()changeChecked(快ToolStripM

23、enuItem)erval=(int)speeds.fast()changeChecked(慢ToolStripMenuItem)erval=(int)speeds.slow ()changeChecked(较慢ToolStripMenuItem)erval=(int)speeds.slower()回车空格下上右左beginGame()currentBlock.left()Showmsg.show()currentBlock.down()currentBlock.rotate()currentBlock.right()先检测是否可以移

24、动;Erase(GameField.winHandle);擦去现在位置的块,调整位置再画下一块Draw(GameField.winHandle);先检测是否可以移动;Erase(GameField.winHandle);擦去现在位置的块,调整位置再画下一块Draw(GameField.winHandle);先检测是否可以移动;GameField.PlaySound();Erase(GameField.winHandle);擦去现在位置的块,判定旋转方向调整位置再画Draw(GameField.winHandle);先检测是否可以移动;Erase(GameField.winHandle);擦去现

25、在位置的块,调整位置再画下一块Draw(GameField.winHandle);picBackGround.Focus();timer1_Tick(object sender, EventArgs e)!currentBlock.down()falsecurrentBlock.down()truecurrentBlock.Top()=0playSound();showMsg(“GameOver!”);timer1.stop();timer2.stop();true falsemsgShow();/达到一定分数 通关提示,并调节方块下落速度;picBackGround.Invalidate()

26、;Application.DoEvents();GameField.Redraw();GameField.CheckLines()>0true产生下一个block; currentBlock=new Block(startLocation,nnextBlock.blockType);currentBlock.Draw(GameField.winHandle); pic_preView.Refresh();nextBlock = new Block(new Point(80, 50), Block.BlockTypes.undefined); nextBlock.Draw(pic_preV

27、iew.Handle);currentBlock.down();四 关键编码小正方形类:namespace MyBlock /* * 本类为小正方形方块实体类 * 每个block由四个颜色相同的square组成 */ class Square public Point location; public Size size; public Color foreColor; public Color backColor; public Square(Size initSize,Color initForeColor,Color initBackColor) size = initSize; for

28、eColor = initForeColor; backColor = initBackColor; /画方块 public void Draw(System.IntPtr winHandle) Graphics g = Graphics.FromHwnd(winHandle); /从窗口的指定句柄创建新的 Graphics GraphicsPath gp = new GraphicsPath();/围成图形的路径 Rectangle rec = new Rectangle(location, size); gp.AddRectangle(rec); Color surroundColor =

29、 new Color backColor ; PathGradientBrush pb = new PathGradientBrush(gp); pb.CenterColor = foreColor; pb.SurroundColors = surroundColor; g.FillPath(pb, gp); /用颜色填充正方形 /擦除方块 public void Erase(System.IntPtr winHandle) Graphics g = Graphics.FromHwnd(winHandle); Rectangle rec = new Rectangle(location,siz

30、e); g.FillRectangle(new SolidBrush(GameField.BackColor),rec);/用背景的颜色覆盖正方形 俄罗斯方块类:namespace MyBlock class Block public Square square1; /组成block的四个小方块 public Square square2; public Square square3; public Square square4; private const int squareSize = GameField.SquareSize; /小方块的边长 public enum BlockType

31、s undefined = 0, square = 1, line = 2, J = 3, L = 4, T = 5, Z = 6, S = 7 ;/一共有7种形状 public BlockTypes blockType; /方块的形状 /七个小方块的颜色数组 private Color foreColor; private Color backColor; /方块的方向 public enum RotateDirections North = 1, East = 2, South = 3, West = 4 ; public RotateDirections myRotation = Rot

32、ateDirections.North; 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 = GameField.BlockForeCol

33、ori; backColor = GameField.BlockBackColori; Size squareS=new Size(squareSize,squareSize); /小正方形,有四个小正方形组合成7种不同形状的方块 square1 = new Square(squareS, foreColor, backColor); square2 = new Square(squareS, foreColor, backColor); square3 = new Square(squareS, foreColor, backColor); square4 = new Square(squa

34、reS, foreColor, backColor); /设置小方块的位置,组合成指定形状的一个大方块 switch (blockType) case BlockTypes.square: /组合成正方形 square1.location = new Point(thisLocation.X, thisLocation.Y);/左上角 square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y);/右上角 square3.location = new Point(thisLocation.X,thisLoca

35、tion.Y+squareSize);/左下角 square4.location = new Point(thisLocation.X+squareSize,thisLocation.Y+squareSize);/右下角 break; case BlockTypes.line: /组合成线形 square1.location = new Point(thisLocation.X, thisLocation.Y); square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y); square3.location

36、 = new Point(thisLocation.X + 2 * squareSize, thisLocation.Y); square4.location = new Point(thisLocation.X + 3 * squareSize, thisLocation.Y); break; case BlockTypes.J: /组合成J形 square1.location = new Point(thisLocation.X + squareSize, thisLocation.Y); square2.location = new Point(thisLocation.X + squa

37、reSize, thisLocation.Y + squareSize); square3.location = new Point(thisLocation.X + squareSize, thisLocation.Y + 2 * squareSize); square4.location = new Point(thisLocation.X, thisLocation.Y + 2 * squareSize); break; case BlockTypes.L: /组合成l形 square1.location = new Point(thisLocation.X, thisLocation.

38、Y); square2.location = new Point(thisLocation.X, thisLocation.Y + squareSize); square3.location = new Point(thisLocation.X, thisLocation.Y + 2 * squareSize); square4.location = new Point(thisLocation.X + squareSize, thisLocation.Y + 2 * squareSize); break; case BlockTypes.T: /组合成T形 square1.location

39、= new Point(thisLocation.X, thisLocation.Y); square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y); square3.location = new Point(thisLocation.X + 2*squareSize, thisLocation.Y); square4.location = new Point(thisLocation.X + squareSize, thisLocation.Y +squareSize); break; case Bloc

40、kTypes.Z: /组合成z形 square1.location = new Point(thisLocation.X, thisLocation.Y); square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y); square3.location = new Point(thisLocation.X + squareSize, thisLocation.Y + squareSize); square4.location = new Point(thisLocation.X + 2*squareSize

41、, thisLocation.Y + squareSize); break; case BlockTypes.S: /组合成S形 square1.location = new Point(thisLocation.X, thisLocation.Y + squareSize); square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y + squareSize); square3.location = new Point(thisLocation.X + squareSize, thisLocation.Y

42、); square4.location = new Point(thisLocation.X + 2 * squareSize, thisLocation.Y); break; /含有自定义颜色的重载 public Block(Point thisLocation, BlockTypes bType,Color fc,Color bc) /当blockType为undefined时,随机产生方块形状 Random rand = new Random(); if (bType = BlockTypes.undefined) blockType = (BlockTypes)(rand.Next(7

43、) + 1); else blockType = bType; /设置四小方块的颜色 Size squareS = new Size(squareSize, squareSize); square1 = new Square(squareS, fc, bc); square2 = new Square(squareS, fc, bc); square3 = new Square(squareS, fc, bc); square4 = new Square(squareS, fc, bc); /设置小方块的位置,组合成指定形状的一个大方块 switch (blockType) case Bloc

44、kTypes.square: /组合成正方形 square1.location = new Point(thisLocation.X, thisLocation.Y); square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y); square3.location = new Point(thisLocation.X, thisLocation.Y + squareSize); square4.location = new Point(thisLocation.X + squareSize, thisLoc

45、ation.Y + squareSize); break; case BlockTypes.line: /组合成线形 square1.location = new Point(thisLocation.X, thisLocation.Y); square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y); square3.location = new Point(thisLocation.X + 2 * squareSize, thisLocation.Y); square4.location = new Point(thisLocation.X + 3 * squareSize, thisLocation.Y); break; case BlockTypes.J: /组合成J形 square1.location = new P

温馨提示

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

评论

0/150

提交评论