基于C#俄罗斯方块设计_第1页
已阅读1页,还剩18页未读 继续免费阅读

下载本文档

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

文档简介

1、此程序主要三个小结讲解:1、 设计方块类(block.cs)2、 设计游戏类(game.cs)3、 设计窗体类(form1.cs)通俗易懂,希望对广大的学生们都有帮助欢迎下载技术qq 1278263100 邮箱1278263100游戏所需图片:游戏运行图:话不多说:设计方块类(block.cs)程序都有注释,通俗易懂using System;using System.Collections.Generic;using System.Text;using System.Drawing;/addnamespace 俄罗斯方块 public class Block private short wid

2、th; private short height; private short top; private short left; private int ID; /方块部件的ID public int, shape;/存储方块部件的形状,为空白,为有砖块 public Block()/构造函数 Random randomGenerator = new Random(); int randomBlock = randomGenerator.Next(1, 5);/产生14的数 this.ID = randomBlock; switch (this.ID) case 1: /横条形 this.Wi

3、dth = 4; this.Height = 1; this.Top = 0; this.Left = 3; shape = new intthis.Width, this.Height; shape0, 0 = 1; shape1, 0 = 1; shape2, 0 = 1; shape3, 0 = 1; break; case 2:/正方形 this.Width = 2; this.Height = 2; this.Top = 0; this.Left = 4; / Creates the new shape for this block. shape = new intthis.Widt

4、h, this.Height; shape0, 0 = 1; shape0, 1 = 1; shape1, 0 = 1;shape1, 1 = 1; break; case 3:/形 this.Width = 3; this.Height = 3; this.Top = 0; this.Left = 4; / Creates the new shape for this block. shape = new intthis.Width, this.Height; shape0, 0 = 1; shape1, 0 = 1; shape2, 0 = 1; shape1, 1 = 1; shape1

5、, 2 = 1; break; case 4:/L形 this.Width = 2; this.Height = 3; this.Top = 0; this.Left = 4; / Creates the new shape for this block. shape = new intthis.Width, this.Height; shape0, 0 = 1; shape0, 1 = 1; shape0, 2 = 1; shape1, 2 = 1; break; public short Width/Width属性 get return width; set width = value;

6、public short Height/Height属性 get return height; set height = value; public short Top/Top属性 get return top; set top = value; public short Left/Left属性 get return left; set left = value; public void Draw(Graphics g) Image brickImage = Image.FromFile("image/block0.gif"); for (int i = 0; i <

7、 this.Width; i+) for (int j = 0; j < this.Height; j+) if (this.shapei, j = 1)/黑色格子 /得到绘制这个格子的在游戏面板中的矩形区域 Rectangle rect = new Rectangle(this.Left + i) * Game.BlockImageWidth, (this.Top + j) * Game.BlockImageHeight, Game.BlockImageWidth, Game.BlockImageHeight); g.DrawImage(brickImage, rect); /clas

8、s Block设计游戏类(game.cs)using System;using System.Collections.Generic;using System.Text;using System.Drawing;/addnamespace 俄罗斯方块 class Game public const int BlockImageWidth = 21;/方砖中每个小方格的大小 public const int BlockImageHeight = 21; public const int PlayingFieldWidth = 10;/游戏面板大小 public const int Playing

9、FieldHeight = 20; private int, pile; /存储在游戏面板中的所有方砖; private Block currentBlock ;/当前的俄罗斯方块 private Block nextBlock ;/下一个的俄罗斯方块 public int score = 0, lines=0; public bool over=false;/游戏是否结束 public Game()/Game类构造函数 pile = new intPlayingFieldWidth , PlayingFieldHeight; ClearPile(); CreateNewBlock();/产生

10、新的俄罗斯方块 private void ClearPile()/清空游戏面板中的所有方砖 for (int i = 0; i < PlayingFieldWidth ; i+) for (int j = 0; j < PlayingFieldHeight ; j+) pilei, j= 0; private void CreateNewBlock()/产生新的俄罗斯方块if (this.nextBlock != null)currentBlock = nextBlock;elsecurrentBlock = new Block();nextBlock = new Block();

11、 public void DrawPile(Graphics g) Image brickImage = Image.FromFile("image/block1.gif");/方砖的图形 for (int i = 0; i < PlayingFieldWidth ; i+) for (int j = 0; j < PlayingFieldHeight ; j+) if (pilei, j = 1) Rectangle rect = new Rectangle(i * BlockImageWidth, j * BlockImageHeight, BlockIma

12、geWidth, BlockImageHeight);/(j - 1) g.DrawImage(brickImage, rect); public void DrawCurrentBlock(Graphics g) if (currentBlock != null)/检查当前块是否为空 currentBlock.Draw(g); public void DrawNextBlock(Graphics drawingSurface) if (nextBlock != null) short currentLeft = nextBlock.Left; short currentTop = nextB

13、lock.Top; nextBlock.Left = (short)(6 - nextBlock.Width) / 2); nextBlock.Top = (short)(6 - nextBlock.Height) / 2); nextBlock.Draw(drawingSurface); nextBlock.Left = currentLeft; nextBlock.Top = currentTop; private void MoveBlockToPile()/固定到游戏面板上 for (int i = 0; i < currentBlock.Width; i+) for (int

14、j = 0; j < currentBlock.Height; j+) int fx, fy; fx = currentBlock.Left + i; fy = currentBlock.Top + j; if (currentBlock.shapei, j =1) pilefx, fy = 1; CheckForLines(); if (CheckForGameOver()/检查游戏是否结束 over = true; public bool DownCurrentBlock() bool hit = false; currentBlock.Top+; if (currentBlock.

15、Top + currentBlock.Height) > PlayingFieldHeight) hit = true;/当前块触游戏面板底 else/检查是否接触到下一行其他已落方块 for (int i = 0; i < currentBlock.Width; i+) for (int j = 0; j < currentBlock.Height; j+) int fx, fy; fx = currentBlock.Left + i; fy = currentBlock.Top + j; if (currentBlock.shapei, j = 1) &&

16、 (pilefx, fy = 1)/(fy + 1) hit = true; if (hit)/触到其他已落方块或游戏面板底 currentBlock.Top-; MoveBlockToPile();/固定到游戏面板上 CreateNewBlock(); /产生新的俄罗斯方块 return hit; public void RotateCurrentBlock()/旋转方块 bool canRotate = true; short newWidth = 0; short newHeight = 0; int, newShape; newWidth = currentBlock.Height;

17、newHeight = currentBlock.Width; newShape = new intnewWidth, newHeight; int x,y; if (currentBlock.Left + newWidth) <= Game.PlayingFieldWidth) && (currentBlock.Top + newHeight) < Game.PlayingFieldHeight) for (int i = 0; i < currentBlock.Width; i+) for (int j = 0; j < currentBlock.H

18、eight; j+) x = (currentBlock.Height - 1) - j); y = i; newShapex, y = currentBlock.shapei, j; if (newShapex, y = 1 && pilex + currentBlock.Left, y + currentBlock.Top = 1) canRotate = false; return;/不能旋转 if (canRotate) currentBlock.Width = newWidth; currentBlock.Height = newHeight; currentBloc

19、k.shape = newShape; public void MoveCurrentBlockSide(bool left)/左右移动 bool canMove = true; if (left)/左移动 if (currentBlock.Left > 0) for (int i = 0; i < currentBlock.Width; i+) for (int j = 0; j < currentBlock.Height; j+) int fx, fy; fx = currentBlock.Left + i; fy = (currentBlock.Top + 1) + j

20、; if (currentBlock.shapei, j = 1) && (pile(fx - 1), fy =1) canMove = false; if (canMove) currentBlock.Left-; else/右移动 if (currentBlock.Left + currentBlock.Width) < PlayingFieldWidth) for (int i = 0; i < currentBlock.Width; i+) for (int j = 0; j < currentBlock.Height; j+) int fx, fy;

21、 fx = currentBlock.Left + i; fy = (currentBlock.Top + 1) + j; if (currentBlock.shapei, j = 1) && (pile(fx + 1), fy=1) canMove = false; if (canMove) currentBlock.Left+; private int CheckForLines()/检查是否满行并消去 int numLines = 0; int completeLines = new intPlayingFieldHeight; for (int j = PlayingF

22、ieldHeight-1; j > 0; j-)/j = PlayingFieldHeight bool fullLine = true; for (int i = 0; i < PlayingFieldWidth; i+) if (pilei, j = 0) fullLine = false; break; if (fullLine) numLines+; completeLinesnumLines = j; if (numLines > 0) for (int i = 1; i <= numLines; i+) ClearLine(completeLinesi +

23、(i - 1); score += 5 * (numLines * (numLines + 1); lines += numLines; return numLines; private void ClearLine(int lineNumber) for (int j = lineNumber; j > 0; j-) for (int i = 0; i < PlayingFieldWidth; i+) pilei, j = pilei, (j - 1); for (int i = 0; i < PlayingFieldWidth; i+) pilei, 0 = 0; pub

24、lic bool CheckForGameOver()/检查游戏是否结束 if (currentBlock.Top = 0) return true; else return false; 设计窗体类(form1.cs)下图:如果觉得图片不清楚可以另存为桌面,慢慢研究using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespac

25、e 俄罗斯方块 public partial class Form1 : Form public Form1() InitializeComponent(); Game game=null; private void button1_Click(object sender, EventArgs e) game= new Game(); pictureBox1.Height = Game.BlockImageHeight * Game.PlayingFieldHeight + 3; pictureBox1.Width = Game.BlockImageWidth * Game.PlayingFi

26、eldWidth+3; pictureBox1.Invalidate();/重画游戏面板区域 timer1.Enabled = true; button1.Enabled = false; private void button2_Click(object sender, EventArgs e) if (button2.Text = "暂停游戏") timer1.Enabled = false; button2.Text = "继续游戏" else timer1.Enabled = true; button2.Text = "暂停游戏&quo

27、t; private void button3_Click(object sender, EventArgs e) this.Close(); private void pictureBox1_Paint(object sender, PaintEventArgs e) /重画游戏面板 if (game != null) game.DrawPile(e.Graphics); game.DrawCurrentBlock(e.Graphics); private void pictureBox2_Paint(object sender, PaintEventArgs e) /重画下一个方块 if

28、(game != null) game.DrawNextBlock(e.Graphics); private void timer1_Tick(object sender, EventArgs e) if (!game.DownCurrentBlock() pictureBox1.Invalidate();/重画游戏面板区域 pictureBox2.Invalidate();/重画下一个方块 lblScore.Text = game.score.ToString(); if (game.over = true) timer1.Enabled = false; MessageBox.Show("游戏结束,", "提示"); but

温馨提示

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

评论

0/150

提交评论