收藏 分销(赏)

基于C#俄罗斯方块设计.doc

上传人:二*** 文档编号:4535507 上传时间:2024-09-27 格式:DOC 页数:19 大小:744KB 下载积分:5 金币
下载 相关 举报
基于C#俄罗斯方块设计.doc_第1页
第1页 / 共19页
本文档共19页,全文阅读请下载到手机保存,查看更方便
资源描述
. 此程序主要三个小结讲解: 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;//add namespace 俄罗斯方块 { public class Block { private short width; private short height; private short top; private short left; private int ID;    //方块部件的ID public int[,] shape;  //存储方块部件的形状,0为空白,1为有砖块 public Block()//构造函数 { Random randomGenerator = new Random(); int randomBlock = randomGenerator.Next(1, 5);//产生1—4的数 this.ID = randomBlock; switch (this.ID) { case 1: //横条形 this.Width = 4; this.Height = 1; this.Top = 0; this.Left = 3; shape = new int[this.Width, this.Height]; shape[0, 0] = 1; shape[1, 0] = 1; shape[2, 0] = 1; shape[3, 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 int[this.Width, this.Height]; shape[0, 0] = 1; shape[0, 1] = 1; shape[1, 0] = 1;shape[1, 1] = 1; break; case 3:  //T形 this.Width = 3; this.Height = 3; this.Top = 0; this.Left = 4; // Creates the new shape for this block. shape = new int[this.Width, this.Height]; shape[0, 0] = 1; shape[1, 0] = 1; shape[2, 0] = 1; shape[1, 1] = 1; shape[1, 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 int[this.Width, this.Height]; shape[0, 0] = 1; shape[0, 1] = 1; shape[0, 2] = 1; shape[1, 2] = 1; break; } } public short Width//Width属性 { get { return width; } set { width = value; } } 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 < this.Width; i++) { for (int j = 0; j < this.Height; j++) { if (this.shape[i, j] == 1)//黑色格子 { //得到绘制这个格子的在游戏面板中的矩形区域 Rectangle rect = new Rectangle((this.Left + i) * Game.BlockImageWidth, (this.Top + j) * Game.BlockImageHeight, Game.BlockImageWidth, Game.BlockImageHeight); g.DrawImage(brickImage, rect); } } } } }//class Block } 设计游戏类(game.cs) using System; using System.Collections.Generic; using System.Text; using System.Drawing;//add namespace 俄罗斯方块 { class Game { public const int BlockImageWidth = 21;//方砖中每个小方格的大小 public const int BlockImageHeight = 21; public const int PlayingFieldWidth = 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 int[PlayingFieldWidth , PlayingFieldHeight]; ClearPile(); CreateNewBlock();//产生新的俄罗斯方块 } private void ClearPile()//清空游戏面板中的所有方砖 { for (int i = 0; i < PlayingFieldWidth ; i++) { for (int j = 0; j < PlayingFieldHeight ; j++) { pile[i, j]= 0; } } } private void CreateNewBlock()//产生新的俄罗斯方块 { if (this.nextBlock != null) { currentBlock = nextBlock; } else { currentBlock = new Block(); } nextBlock = new Block(); } 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 (pile[i, j] == 1) { Rectangle rect = new Rectangle(i * BlockImageWidth, j * BlockImageHeight, BlockImageWidth, 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 = nextBlock.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 j = 0; j < currentBlock.Height; j++) { int fx, fy; fx = currentBlock.Left + i; fy = currentBlock.Top + j; if (currentBlock.shape[i, j] ==1) { pile[fx, fy] = 1; } } } CheckForLines(); if (CheckForGameOver())//检查游戏是否结束 over = true; } public bool DownCurrentBlock() { bool hit = false; currentBlock.Top++; if ((currentBlock.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.shape[i, j] == 1) && (pile[fx, 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; newHeight = currentBlock.Width; newShape = new int[newWidth, 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.Height; j++) { x = ((currentBlock.Height - 1) - j); y = i; newShape[x, y] = currentBlock.shape[i, j]; if (newShape[x, y] == 1 && pile[x + currentBlock.Left, y + currentBlock.Top] == 1) { canRotate = false; return;//不能旋转 } } } } if (canRotate) { currentBlock.Width = newWidth; currentBlock.Height = newHeight; currentBlock.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; if ((currentBlock.shape[i, 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; fx = currentBlock.Left + i; fy = (currentBlock.Top + 1) + j; if ((currentBlock.shape[i, j] == 1) && (pile[(fx + 1), fy]==1)) { canMove = false; } } } if (canMove) { currentBlock.Left++; } } } } private int CheckForLines()//检查是否满行并消去 { int numLines = 0; int[] completeLines = new int[PlayingFieldHeight]; for (int j = PlayingFieldHeight-1; j > 0; j--)//j = PlayingFieldHeight { bool fullLine = true; for (int i = 0; i < PlayingFieldWidth; i++) { if (pile[i, j] == 0) { fullLine = false; break; } } if (fullLine) { numLines++; completeLines[numLines] = j; } } if (numLines > 0) { for (int i = 1; i <= numLines; i++) { ClearLine((completeLines[i] + (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++) { pile[i, j] = pile[i, (j - 1)]; } } for (int i = 0; i < PlayingFieldWidth; i++) { pile[i, 0] = 0; } } public 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; namespace 俄罗斯方块 { 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.PlayingFieldWidth+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 = "暂停游戏"; } } private void button3_Click(object sender, EventArgs e) { this.Close(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { //重画游戏面板 if (game != null) {
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 学术论文 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服