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

📄 shape.cs

📁 俄罗斯方块游戏
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace 俄罗斯方块
{
    public abstract class Shape
    {
        public bool[,] type;
        public int width;
        public int height;
        public int x;
        public int y;


        public Shape(int width, int height, int x, int y)
        {
            this.height = height;
            this.width = width;
            this.x = x;
            this.y = y;
            this.type = new bool[height, width];
        }


        public void DrawShape(Graphics g, SolidBrush sb)//画当前正在下落的图
        {
            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                {
                    if (type[i, j] == true)
                        g.FillRectangle(sb, new Rectangle(100 + (this.x + j) * 20, 50 + (this.y + i) * 20, 20, 20));
                }

        }


        #region        私有类,专门用于检查  

        private bool Candown(int y, Show cs)
        {
            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                {
                    if(type[i,j]==true)//把图中等于TRUE的i和j拿出来放到图阵中,看下一秒是否有重叠
                    if (y + height > 20||cs.showArray[y + i, x + j] == true )//遍历图阵中TRUE的地方
                        return false;
                }

            return true;
        }

        private bool CanLeft(int x,Show cs)
        {
            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                {
                    if (type[i, j] == true)
                    if (x<0||cs.showArray[y + i, x + j] == true )
                        return false;
                }

            return true;
        }

        private bool CanRight(int x,Show cs)
        {
            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                {
                    if (type[i, j] == true)
                    if (x+width >10 || cs.showArray[y + i, x + j] == true)
                        return false;
                }

            return true;
        }

        private bool CanShift(ref bool[,] tmpType, Show cs)
        {
            //先变化下图形看看是否可行
            int tmpWidth = this.height;
            int tmpHeight = this.width;

            for (int i = 0; i < height; i++)
                for (int j = 0; j < width; j++)
                    tmpType[j, tmpWidth - i - 1] = type[i, j];

            for (int i = 0; i < tmpHeight; i++)
                for (int j = 0; j < tmpWidth; j++)
                {
                    if (tmpType[i, j] == true)
                          if (y + tmpHeight > 20 || !TmpMoveLeft(tmpWidth,tmpHeight,ref tmpType,cs) ||cs.showArray[y + i, x + j] == true)
                                 return false;

                }

            return true;
        }


            private bool TmpMoveLeft(int tmpWidth,int tmpHeight,ref bool[,] tmpType,Show cs)//让图形在边线上也能变换
            {
                while(x + tmpWidth >10)
                {
                     for (int i = 0; i < tmpHeight; i++)
                         if (tmpType[i, 0] == true)
                            if (cs.showArray[y + i, x-1] == true)
                                   return false;
                     x--;
                }
                return true;
            }
            



        #endregion    


        public bool Autodown(Show cs)
        {
            if (Candown(y + 1, cs))
            {
                y++;
                return true;
            }
            else
                return false;
        }

        public void Movedown(Show cs)
        {
            if (Candown(y + 1, cs))
            {
                y++;
            }

        }


        public void MoveLeft(Show cs)
        {
            if (CanLeft(x - 1, cs))
            {

                this.x--;
            }
        }


        public void MoveRight(Show cs)
        {
            if (CanRight(x + 1, cs))
            {

                this.x++;
            }
        }


        public void ShiftShape(Show cs)
        {
            bool[,] tmpType = new bool[width,height];
            if (CanShift(ref tmpType, cs))
            {
                int tmp;
                tmp = this.width;
                this.width = this.height;
                this.height = tmp;
                type = tmpType;
            }

        



        }


    }
}

⌨️ 快捷键说明

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