⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 block.cs

📁 陈广老师讲课的源代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;


namespace MyTetris
{
    /// <summary>
    /// 方块类,用来定义方块的基本操作
    /// </summary>
    public class Block
    {
        /// <summary>
        /// 存放方块每个小方块的坐标信息
        /// </summary>
        protected Point[] structArr;    
        /// <summary>
        /// 方块中心所在的X坐标
        /// </summary>
        protected int _xPos;     
        /// <summary>
        /// 方块中心所在的Y坐标
        /// </summary>       
        protected int _yPos;           
        /// <summary>
        /// 方块颜色
        /// </summary>
        protected Color _blockColor;    
        /// <summary>
        /// 探险方块颜色(背景色)
        /// </summary>
        protected Color disapperColor;  
        /// <summary>
        /// 每个方块的像素
        /// </summary>
        protected int rectPix;          
        
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Block()
        {
        }

        /// <summary>   public Block(Point[] sa,Color bColor,Color dColor,int pix)
        /// 构造函数
        /// </summary>
        /// <param name="sa">组成方块的小方块的坐标集合</param>
        /// <param name="bColor">方块颜色</param>
        /// <param name="dColor">前景色</param>
        /// <param name="pix">小方块像素数</param>
        public Block(Point[] sa,Color bColor,Color dColor,int pix)
        {
            structArr=sa;
            _blockColor=bColor;
            disapperColor=dColor;
            rectPix=pix;
        }

        #region  属性、索引 
        public Point this[int index]
        {
            get{
                return structArr[index];
            }
        }
        public int Length
        {
            get{
                return structArr.Length;
            }
        }
        public int XPos
        {
            get { return _xPos; }
            set { _xPos = value; }
        }
        public int YPos
        {
              get { return _yPos; }
              set { _yPos = value; }
        }
        public Color BlockColor
        {
          get { return _blockColor; }
        }
        #endregion    

        /// <summary>   public void DeasilRotate()  
        /// 顺时针旋转
        /// </summary>
        public void DeasilRotate()  
        {
            int temp;       //旋转公式:x1=y,y1=-x
            for (int i = 0; i < structArr.Length;i++ )
            {
                temp = structArr[i].X;
                structArr[i].X =structArr[i].Y;
                structArr[i].Y =-temp;
            }
        }

        /// <summary>   public void DisecRotate()
        /// 逆时针旋转
        /// </summary>
        public void DisecRotate()   
        {
            int temp;       //旋转公式:x1=-y,y1=x;
            for (int i = 0; i < structArr.Length; i++)
            {
                temp = structArr[i].X;
                structArr[i].X = -structArr[i].Y;
                structArr[i].Y = temp;
            }
        }

        /// <summary>   public Rectangle PointToRect(Point p)
        /// 将一个在矩阵(五乘五)里边的方块的绝对坐标转换成画布的坐标植
        /// </summary>
        /// <param name="p">坐标点</param>
        /// <returns>矩阵</returns>
        public Rectangle PointToRect(Point p)  
        {
            // 转换公式:x1=xpos+x  y1=ypos-y
            return new Rectangle((_xPos+p.X)*rectPix+1,(_yPos-p.Y)*rectPix+1,rectPix-2,rectPix-2);
        }

        /// <summary>   public void Paint(Graphics gra)
        /// 重绘方法,在指定的画布上画方块
        /// </summary>
        /// <param name="gra">在指定的画布上重绘</param>
        public void Paint(Graphics gra)     
        {
            SolidBrush sb = new SolidBrush(_blockColor);
            foreach(Point p in structArr)
            {
                lock(gra)
                {
                    gra.FillRectangle(sb,PointToRect(p));
                }
            }
        }

        /// <summary>   public void erase(Graphics gra) 
        /// 擦除指定的方块
        /// </summary>
        /// <param name="gra">在指定的画布上擦除</param>
        public void erase(Graphics gra)     
        {
            SolidBrush sb = new SolidBrush(disapperColor);           
            foreach(Point p in structArr)
            {
                lock(gra)
                {
                    gra.FillRectangle(sb, PointToRect(p));
                }
            }
        }

      
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -