收藏 分销(赏)

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

上传人:二*** 文档编号:4535507 上传时间:2024-09-27 格式:DOC 页数:19 大小:744KB
下载 相关 举报
基于C#俄罗斯方块设计.doc_第1页
第1页 / 共19页
亲,该文档总共19页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

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 wi

2、dth; 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.W

3、idth = 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.Wid

4、th, 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; shape

5、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 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 this.Width; i+)

7、 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); /class Block设计游戏类(game.cs)

8、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 PlayingFieldHeight = 20; pri

9、vate 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();/产生新的俄罗斯方块 private void

10、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(); public void DrawPile(Graphics

11、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, BlockImageWidth, BlockImageHeight);/(j - 1) g.DrawImage(brick

12、Image, 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.Widt

13、h) / 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 PlayingFieldHeight) hit = true;/当前块触游戏面板底 else/检查

14、是否接触到下一行其他已落方块 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) & (pilefx, fy = 1)/(fy + 1) hit = true; if (hit)/触到其他已落方块或游戏面板底 currentBlock.Top-; MoveBlockToPile();

15、/固定到游戏面板上 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 intnewWidth, newHeight; int x,y; if (currentBlock.Lef

16、t + newWidth) = Game.PlayingFieldWidth) & (currentBlock.Top + newHeight) Game.PlayingFieldHeight) for (int i = 0; i currentBlock.Width; i+) for (int j = 0; j 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 = (currentB

17、lock.Top + 1) + j; 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 0; j-)/j = PlayingFieldHeight bool fullLine

18、 = 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 CheckForGameOver()/检查游戏是否结束 if (currentBlock.Top = 0) return true; else return false; 设计窗体类(form1.cs)下图:如果觉得图片

19、不清楚可以另存为桌面,慢慢研究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_Cl

20、ick(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_Cli

21、ck(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)

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

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

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

关于我们      便捷服务       自信AI       AI导航        获赠5币

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

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

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

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服