俄罗斯方块之四 运动块的绘制实现

xiaoxiao2021-02-28  94

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; namespace Els { class Block { //定义类的属性: Random ran = new Random(); //随机数对象 BlockType typeId; //枚举类型变量type private int[,] currentShapes; //当前形状的二维数组,具体到是哪个具体的俄罗斯方块 protected int left, top; //block的位置 protected int colorIndex; //随机俄罗斯方块的颜色; public const int SquareSize = 16; // 单个方块长宽为16像素 /// <summary> /// 定义块的构造方法 /// </summary> public Block() { typeId = (BlockType)ran.Next(4); //需要强制转化一下,随机产生一个俄罗斯方块 this.currentShapes = ShapeTable.getShape(typeId, ran.Next(0, 5)); top = 0; left = 64; //当前块panel的一半 this.colorIndex = ran.Next(1, 9); //可以暂时省略该代码; } /// <summary> /// /// </summary> /// <param name="g"></param> public void DrawSmall(Graphics g) { int left = 3 - currentShapes.GetLength(1) / 2+1; int top = 3 - currentShapes.GetLength(0) / 2+1; //上面两行代码,定位居左和居中的位置; Image img = System.Drawing.Image.FromFile("img/"+colorIndex+".jpg"); //拆分成一个类,存放了8个颜色的俄罗斯方块的路径; for (int y = 0; y < this.currentShapes.GetLength(0); y++) { for (int x = 0; x < this.currentShapes.GetLength(1); x++) { //如果判断二维数组的单元格值是1,则,构造一个矩形,在指定位置,长度; if (this.currentShapes[y, x] == 1) { Rectangle rec = new Rectangle((left + x) * SquareSize, (top + y) * SquareSize, SquareSize, SquareSize); g.DrawImage(img, rec); } } } } } }

可以实现暂时绘制简单图形,复杂图形还没有做,以及运动轨迹的实现还没有实现。

视频课:https://edu.csdn.net/course/play/8034

 

视频:https://edu.csdn.net/course/detail/27107

 

 

tea_year 认证博客专家 大司徒 微软MVP!ORACLE认证高级工程师!主要研究方向为大数据、人工智能、JAVA、.Net、数据库 、前端开发、产品研发,曾经服务过中铝、中烟等大型上市国企IT部门,软件企业联合创始人,喜欢软件研发管理、技术营销!
转载请注明原文地址: https://www.6miu.com/read-74173.html

最新回复(0)