




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
此程序主要三个小结讲解: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 width; 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.Width = 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.Width, 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, 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; 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.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)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; 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();/产生新的俄罗斯方块 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(); 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, 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 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) & (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; 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 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.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 = 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)下图:如果觉得图片不清楚可以另存为桌面,慢慢研究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) game.DrawPile(e.Graphics); game.DrawCurrentBlock(e.Graphics); private void pictureBox2_Paint(object sender, PaintEventArgs e) /重画下一个方块 if (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(游戏结束, 提示); button1.Enabled = true; protected override bool ProcessCmdKey(ref Message msg, Keys e) /重写ProcessCmdKey方法 if (button2.Text = 继续游戏) return true;/暂停时不响应键盘 if (e
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 瓣膜置换的围术期护理
- 人教A版 (2019)选择性必修 第一册3.2 双曲线获奖教案
- 2024中铝共享服务(天津)有限公司校园招聘1人笔试参考题库附带答案详解
- 凤仙花的一生(教学设计)-2024-2025学年科学三年级下册人教鄂教版
- 人教部编版一年级下册20 咕咚第2课时教学设计
- 人教版(2024)八年级上册(2024)第4节 眼睛和眼镜教案
- 2024中建一局二级公司总工程师公开竞聘1人笔试参考题库附带答案详解
- 钉钉使用详尽培训
- 2024中国邮政福建建省分公司校园招聘预笔试参考题库附带答案详解
- 人美版三年级下册第3课 竖弯钩教案及反思
- 中国高职院校毕业生薪酬报告(2024年度)
- 2025-2030中国团餐行业市场发展现状分析及发展前景与投资机会研究报告
- 山东省济南西城实验中学2024-2025学年高一下学期4月月考地理试题(原卷版+解析版)
- IT系统架构规划与设计手册
- 口腔门诊6S管理
- 沉浸式体验活动设计合同
- 档案档案管理基础知识试题及答案
- 2025-2030中国金红石发展现状及未来趋势研究报告
- 2025-2030中国慢性腰痛治疗行业市场现状供需分析及投资评估规划分析研究报告
- 演出经纪人与文化经济试题
- pcb抄板合同范例
评论
0/150
提交评论