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

来源:互联网 发布:房屋中介软件哪个好 编辑:程序博客网 时间:2024/06/12 00:09
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);                    }                }            }        }            }}

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


0 0
原创粉丝点击