《基于C俄罗斯方块设计 .doc》由会员分享,可在线阅读,更多相关《基于C俄罗斯方块设计 .doc(18页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、此程序主要三个小结讲解:1、 设计方块类()2、 设计游戏类()3、 设计窗体类()通俗易懂,希望对广大的学生们都有帮助欢迎下载技术 邮箱游戏所需图片:游戏运行图:话不多说:设计方块类()程序都有注释,通俗易懂using System;using ic;using ;using ng;/addnamespace 俄罗斯方块 public class Block private short width; private short height; private short top; private short left; private int ID; /方块部件的ID public int,
2、 shape;/存储方块部件的形状,为空白,为有砖块 public Block()/构造函数 Random randomGenerator = new Random(); int randomBlock = (1, 5);/产生14的数 = randomBlock; switch () case 1: /横条形 = 4; t = 1; = 0; = 3; shape = new int, t; shape0, 0 = 1; shape1, 0 = 1; shape2, 0 = 1; shape3, 0 = 1; break; case 2:/正方形 = 2; t = 2; = 0; = 4;
3、/ Creates the new shape for this block. shape = new int, t; shape0, 0 = 1; shape0, 1 = 1; shape1, 0 = 1;shape1, 1 = 1; break; case 3:/形 = 3; t = 3; = 0; = 4; / Creates the new shape for this block. shape = new int, t; shape0, 0 = 1; shape1, 0 = 1; shape2, 0 = 1; shape1, 1 = 1; shape1, 2 = 1; break;
4、case 4:/L形 = 2; t = 3; = 0; = 4; / Creates the new shape for this block. shape = new int, t; shape0, 0 = 1; shape0, 1 = 1; shape0, 2 = 1; shape1, 2 = 1; break; public short Width/Width属性 get return width; set width = value; public short Height/Height属性 get return height; set height = value; public s
5、hort 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 = ile(image/); for (int i = 0; i ; i+) for (int j = 0; j t; j+) if (i, j = 1)/黑色格子 /得到绘制这个格子的在游戏面板中的矩形区域 Rectangle rect = new Rectangle( + i) * Im
6、ageWidth, ( + j) * ImageHeight, ImageWidth, ImageHeight); g.DrawImage(brickImage, rect); /class Block设计游戏类()using System;using ic;using ;using ng;/addnamespace 俄罗斯方块 class Game public const int BlockImageWidth = 21;/方砖中每个小方格的大小 public const int BlockImageHeight = 21; public const int PlayingFieldWid
7、th = 10;/游戏面板大小 public const int PlayingFieldHeight = 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 , PlayingFieldH
8、eight; ClearPile(); CreateNewBlock();/产生新的俄罗斯方块 private void ClearPile()/清空游戏面板中的所有方砖 for (int i = 0; i PlayingFieldWidth ; i+) for (int j = 0; j PlayingFieldHeight ; j+) pilei, j= 0; private void CreateNewBlock()/产生新的俄罗斯方块if (lock != null)currentBlock = nextBlock;elsecurrentBlock = new Block();next
9、Block = new Block(); public void DrawPile(Graphics g) Image brickImage = ile(image/);/方砖的图形 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, BlockImageWidth, BlockImageHeig
10、ht);/(j - 1) g.DrawImage(brickImage, rect); public void DrawCurrentBlock(Graphics g) if (currentBlock != null)/检查当前块是否为空 (g); public void DrawNextBlock(Graphics drawingSurface) if (nextBlock != null) short currentLeft = ; short currentTop = ; = (short)(6 - ) / 2); = (short)(6 - t) / 2); (drawingSurf
11、ace); = currentLeft; = currentTop; private void MoveBlockToPile()/固定到游戏面板上 for (int i = 0; i ; i+) for (int j = 0; j PlayingFieldHeight) hit = true;/当前块触游戏面板底 else/检查是否接触到下一行其他已落方块 for (int i = 0; i ; i+) for (int j = 0; j t; j+) int fx, fy; fx = + i; fy = + j; if (i, j = 1) & (pilefx, fy = 1)/(fy +
12、 1) hit = true; if (hit)/触到其他已落方块或游戏面板底 -; MoveBlockToPile();/固定到游戏面板上 CreateNewBlock(); /产生新的俄罗斯方块 return hit; public void RotateCurrentBlock()/旋转方块 bool canRotate = true; short newWidth = 0; short newHeight = 0; int, newShape; newWidth = t; newHeight = ; newShape = new intnewWidth, newHeight; int
13、x,y; if ( + newWidth) = ngFieldWidth) & ( + newHeight) ngFieldHeight) for (int i = 0; i ; i+) for (int j = 0; j 0) for (int i = 0; i ; i+) for (int j = 0; j t; j+) int fx, fy; fx = + i; fy = ( + 1) + j; if (i, j = 1) & (pile(fx - 1), fy =1) canMove = false; if (canMove) -; else/右移动 if ( + ) PlayingF
14、ieldWidth) for (int i = 0; i ; i+) for (int j = 0; j 0; j-)/j = PlayingFieldHeight bool fullLine = true; for (int i = 0; i 0) for (int i = 1; i 0; j-) for (int i = 0; i PlayingFieldWidth; i+) pilei, j = pilei, (j - 1); for (int i = 0; i PlayingFieldWidth; i+) pilei, 0 = 0; public bool CheckForGameOv
15、er()/检查游戏是否结束 if ( = 0) return true; else return false; 设计窗体类()下图:如果觉得图片不清楚可以另存为桌面,慢慢研究using System;using ic;using nentModel;using ;using ng;using ;using ws.Forms;namespace 俄罗斯方块 public partial class Form1 : Form public Form1() InitializeComponent(); Game game=null; private void button1_Click(object
16、 sender, EventArgs e) game= new Game(); t = ImageHeight * ngFieldHeight + 3; = ImageWidth * ngFieldWidth+3; idate();/重画游戏面板区域 ed = true; ed = false; private void button2_Click(object sender, EventArgs e) if ( = 暂停游戏) ed = false; = 继续游戏; else ed = true; = 暂停游戏; private void button3_Click(object sende
17、r, EventArgs e) (); private void pictureBox1_Paint(object sender, PaintEventArgs e) /重画游戏面板 if (game != null) ile(e.Graphics); urrentBlock(e.Graphics); private void pictureBox2_Paint(object sender, PaintEventArgs e) /重画下一个方块 if (game != null) extBlock(e.Graphics); private void timer1_Tick(object sen
18、der, EventArgs e) if (!urrentBlock() idate();/重画游戏面板区域 idate();/重画下一个方块 = .ToString(); if ( = true) ed = false; (游戏结束, 提示); ed = true; protected override bool ProcessCmdKey(ref Message msg, Keys e) /重写ProcessCmdKey方法 if ( = 继续游戏) return true;/暂停时不响应键盘 if (e = | e = | e = | e = | e = ) MyKeyPress(thi
19、s, new KeyPressEventArgs(char)e); return true; /然后在MyKeyPress方法中处理 private void MyKeyPress(object sender, KeyPressEventArgs e) switch (e.KeyChar) case (char): eCurrentBlock(); break; case (char): if (!urrentBlock() idate();/重画游戏面板区域 break; case (char): urrentBlockSide(false); break; case (char): urrentBlockSide(true); break; case (char):