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

📄 palette.cs

📁 陈广老师讲课的源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Timers;
using System.Threading;
using System.Timers;
using System.Windows.Forms;

namespace MyTetris
{
    /// <summary>
    /// 画布类 用来定义对程序中方块一系列的操作
    /// </summary>
    public class Palette
    {
        /// <summary>
        /// 画板宽度
        /// </summary>
        private int _width = 15;
        /// <summary>
        /// 画板高度
        /// </summary>
        private int _height = 25;
        /// <summary>
        /// 画布总共能容纳的方块大小
        /// </summary>
        private Color[,] coorArr;   
        /// <summary>
        /// 背景颜色
        /// </summary>
        private Color disapperColor;    
        /// <summary>
        /// 活动方块画布
        /// </summary>
        private Graphics gpPlatette;    
        /// <summary>
        /// 下一个方块画布
        /// </summary>
        private Graphics gpReady;       
        /// <summary>
        /// 显示当前状态的画布
        /// </summary>
        private Graphics gpStatus;      
        /// <summary>
        /// 方块生产
        /// </summary>
        private BlockFactory bFactory;  
        /// <summary>
        /// 正在运行的方块
        /// </summary>
        private Block runBlock;         
        /// <summary>
        /// 下一个方块
        /// </summary>
        private Block readyBlock;   
        /// <summary>
        /// 方块像素
        /// </summary>
        private int rectPix;        

        /// <summary>
        /// 开始时刻
        /// </summary>
        private DateTime atStart;   
        /// <summary>
        /// 暂停时刻
        /// </summary>
        private DateTime atPause;       
        /// <summary>
        /// 等级
        /// </summary>
        private int level = 1;         
        /// <summary>
        /// 第一级设置的行数
        /// </summary>
 
        private int levelNum = 1;      
        /// <summary>
        /// 难度系数
        /// </summary>
        private int levelModu = 10;     
        /// <summary>
        /// 删除的行数
        /// </summary>
        private int rowDelNum = 0;      
        /// <summary>
        /// 总共的方块数
        /// </summary>
        private int blockNum = 0;   
        /// <summary>
        /// 速度
        /// </summary>
        private double speed;       
        /// <summary>
        /// 显示状态的字体大小
        /// </summary>
        private float showStatusFontSize;      

        private System.Windows.Forms.Label labSpeed;
        private System.Timers.Timer myTimer;
        private int timpeSpan;

        #region         属性
        public int RowDelNum
        {
            get { return rowDelNum; }
            set { rowDelNum = value; }
        }
        public int BlockNum
        {
            get { return blockNum; }
            set { blockNum = value; }
        }
        public double Speed
        {
            get { return speed; }
            set { speed = value; }
        }
        public int Level
        {
            get { return level; }
            set { level = value; }
        }
        #endregion   
       

                
        /// <summary>   public Palette(int x,int y,int pix,Color Bcolor,Graphics gra,Graphics gra1,Graphics gra2)
        /// 画布构造函数    
        /// </summary>
        /// <param name="x">画布的宽度</param>
        /// <param name="y">画布的高度</param>
        /// <param name="pix">小方块的像素数</param>
        /// <param name="Bcolor">画布背景色</param>
        /// <param name="gra">画正在运行的方块的画布</param>
        /// <param name="gra1">画下一个方块的画布</param>
        /// <param name="gra2">显示当前过关状态的画布</param>
        public Palette(int x,int y,int pix,Color Bcolor,Graphics gra,Graphics gra1,Graphics gra2)
        {
            this._width = x;
            this._height = y;
            this.rectPix = pix;
            this.disapperColor = Bcolor;
            this.coorArr=new Color[x,y];
            this.gpPlatette = gra;
            this.gpReady = gra1;
            this.gpStatus = gra2;

        }

        /// <summary>   public void Start()
        /// 开始函数 产生第一个方块和下一个方块并进行显示   
        /// </summary>
        public void Start()
        {
            bFactory = new BlockFactory();
            runBlock = bFactory.GetBlock();
            runBlock.XPos = _width / 2;
            int y = 0;
            for (int i = 0; i < runBlock.Length;i++ )   //找出方块的最高点
            {
                if(runBlock[i].Y>y)
                {
                    y = runBlock[i].Y;
                }
            }
            runBlock.YPos = y;
            gpPlatette.Clear(disapperColor);    //清空画布
            runBlock.Paint(gpPlatette);

            Thread.Sleep(20);

            readyBlock = bFactory.GetBlock();
            readyBlock.XPos = 2;
            readyBlock.YPos = 2;
            gpReady.Clear(disapperColor);   //清空画布
            readyBlock.Paint(gpReady);

            timpeSpan =500 - 50 * (level - 1);      //产生方块的时间间隔
            myTimer = new System.Timers.Timer(timpeSpan);
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeEvent);
            myTimer.AutoReset = true;
            myTimer.Start();
            this.atStart = DateTime.Now;    //记录开始时间
        }

        /// <summary>   public void OnTimeEvent(Object source,ElapsedEventArgs e)
        /// Timer控件的绑定事件,删除满行、向下移动方块、显示当前状态    public void OnTimeEvent(Object source,ElapsedEventArgs e)  
        /// </summary>
        /// <param name="source">事件源</param>
        /// <param name="e">事件参数</param>      
        public void OnTimeEvent(Object source,ElapsedEventArgs e)
        {
            speed =((TimeSpan)(DateTime.Now - atStart)).TotalSeconds / (double)blockNum;
            //speed =Math.Round((double)blockNum / ((TimeSpan)(DateTime.Now - this.atStart)).Subtract(this.pauseTime).TotalSeconds, 3) + "块/秒";
            DeleFullRow();
            CheckAndOverBlock();          
            Down();
            ShowStatus();
            
        }

        /// <summary>        public bool Down()
        /// 向下移动方块
        /// </summary>
        /// <returns>返回布尔值判断是否可以下移</returns>
        public bool Down()
        {
            int xPos = runBlock.XPos;
            int yPos = runBlock.YPos+1;
            for (int i = 0; i < runBlock.Length;i++ )
            {
                if(yPos-runBlock[i].Y>_height-1)    //如果超出下边界则返回false;
                    return false;
                if (!coorArr[xPos + runBlock[i].X, yPos - runBlock[i].Y].IsEmpty) //下边有方块已经填充
                    return false;
            }                        
            runBlock.erase(gpPlatette);
            runBlock.YPos ++;
            runBlock.Paint(gpPlatette);
            return true;
        }

        /// <summary>   public void Drop()
        /// 丢下方块
        /// </summary>
        public void Drop()
        {
            myTimer.Stop();
            while(Down());
            myTimer.Start();
        }

        /// <summary>        public void MoveLeft()
        /// 向左移方块
        /// </summary>
        public void MoveLeft()
        {
            int xPos = runBlock.XPos-1;
            int yPos = runBlock.YPos;
            for (int i = 0; i < runBlock.Length; i++)
            {
                if (xPos+runBlock[i].X<0)    //如果超出左边界则返回false;
                    return ;
                if (!coorArr[xPos + runBlock[i].X, yPos - runBlock[i].Y].IsEmpty) //左边有方块已经填充则返回false;
                    return ;
            }
            runBlock.erase(gpPlatette);
            runBlock.XPos--;
            runBlock.Paint(gpPlatette);
            
        }

        /// <summary>   public void MoveRight()
        /// 向右移方块
        /// </summary>
        public void MoveRight()
        {
            int xPos = runBlock.XPos + 1;
            int yPos = runBlock.YPos;
            for (int i = 0; i < runBlock.Length; i++)
            {
                if (xPos + runBlock[i].X>_width-1)    //如果超出左边界则返回false;
                    return;
               
                if (!coorArr[xPos + runBlock[i].X, yPos - runBlock[i].Y].IsEmpty) //左边有方块已经填充则返回false;
                    return;
            }
            runBlock.erase(gpPlatette);
            runBlock.XPos++;
            runBlock.Paint(gpPlatette);

⌨️ 快捷键说明

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