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

📄 block.cs

📁 自己写的源码
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ChinaBlock
{
    class Block
    {
        public Square square1;  //组成block的四个小方块
        public Square square2;
        public Square square3;
        public Square square4;

        private const int squareSize = GameField.SquareSize; //小方块的边长
        public enum BlockTypes
        {
            undefined = 0,
            square = 1,
            line = 2,
            J = 3,
            L = 4,
            T = 5,
            Z = 6,
            S = 7
        };//一共有7种形状
        public BlockTypes blockType;  //方块的形状
        //七个小方块的颜色数组
        private Color foreColor;
        private Color backColor;
        //方块的方向
        public enum RotateDirections
        {
            North = 1,
            East = 2,
            South = 3,
            West = 4
        };
        public RotateDirections myRotation = RotateDirections.North;
        
        public Block(Point thisLocation,BlockTypes bType)
        { //当blockType为undefined时,随机产生方块形状
            Random rand=new Random();
            if (bType == BlockTypes.undefined)
            {
                blockType = (BlockTypes)(rand.Next(7) + 1);
            }
            else
                blockType = bType;
            //设置四小方块的颜色
            int i=(int)blockType-1;
            foreColor = GameField.BlockForeColor[i];
            backColor = GameField.BlockBackColor[i];
            Size squareS=new Size(squareSize,squareSize);
            square1 = new Square(squareS, foreColor, backColor);
            square2 = new Square(squareS, foreColor, backColor);
            square3 = new Square(squareS, foreColor, backColor);
            square4 = new Square(squareS, foreColor, backColor);

            //设置小方块的位置,组合成指定形状的一个大方块
            switch (blockType)
            {
                case BlockTypes.square:
                    //组合成正方形
                    square1.location = new Point(thisLocation.X, thisLocation.Y);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square3.location = new Point(thisLocation.X,thisLocation.Y+squareSize);
                    square4.location = new Point(thisLocation.X+squareSize,thisLocation.Y+squareSize);
                    break;
                case BlockTypes.line:
                    //组合成线形
                    square1.location = new Point(thisLocation.X, thisLocation.Y);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square3.location = new Point(thisLocation.X + 2 * squareSize, thisLocation.Y);
                    square4.location = new Point(thisLocation.X + 3 * squareSize, thisLocation.Y);
                    break;
                case BlockTypes.J:
                    //组合成J形
                    square1.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y + squareSize);
                    square3.location = new Point(thisLocation.X + squareSize, thisLocation.Y + 2 * squareSize);
                    square4.location = new Point(thisLocation.X, thisLocation.Y + 2 * squareSize);
                    break;
                case BlockTypes.L:
                    //组合成l形
                    square1.location = new Point(thisLocation.X, thisLocation.Y);
                    square2.location = new Point(thisLocation.X, thisLocation.Y + squareSize);
                    square3.location = new Point(thisLocation.X, thisLocation.Y + 2 * squareSize);
                    square4.location = new Point(thisLocation.X + squareSize, thisLocation.Y + 2 * squareSize);
                    break;
                case BlockTypes.T:
                    //组合成T形
                    square1.location = new Point(thisLocation.X, thisLocation.Y);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square3.location = new Point(thisLocation.X + 2*squareSize, thisLocation.Y);
                    square4.location = new Point(thisLocation.X + squareSize, thisLocation.Y +squareSize);
                    break;
                case BlockTypes.Z:
                    //组合成z形
                    square1.location = new Point(thisLocation.X, thisLocation.Y);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square3.location = new Point(thisLocation.X + squareSize, thisLocation.Y + squareSize);
                    square4.location = new Point(thisLocation.X + 2*squareSize, thisLocation.Y + squareSize);
                    break;
                case BlockTypes.S:
                    //组合成S形
                    square1.location = new Point(thisLocation.X, thisLocation.Y + squareSize);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y + squareSize);
                    square3.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square4.location = new Point(thisLocation.X + 2 * squareSize, thisLocation.Y);
                    break;
            }
        }
        //含有自定义颜色的重载
        public Block(Point thisLocation, BlockTypes bType,Color fc,Color bc)
        { //当blockType为undefined时,随机产生方块形状
            Random rand = new Random();
            if (bType == BlockTypes.undefined)
            {
                blockType = (BlockTypes)(rand.Next(7) + 1);
            }
            else
                blockType = bType;
            //设置四小方块的颜色
            Size squareS = new Size(squareSize, squareSize);
            square1 = new Square(squareS, fc, bc);
            square2 = new Square(squareS, fc, bc);
            square3 = new Square(squareS, fc, bc);
            square4 = new Square(squareS, fc, bc);

            //设置小方块的位置,组合成指定形状的一个大方块
            switch (blockType)
            {
                case BlockTypes.square:
                    //组合成正方形
                    square1.location = new Point(thisLocation.X, thisLocation.Y);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square3.location = new Point(thisLocation.X, thisLocation.Y + squareSize);
                    square4.location = new Point(thisLocation.X + squareSize, thisLocation.Y + squareSize);
                    break;
                case BlockTypes.line:
                    //组合成线形
                    square1.location = new Point(thisLocation.X, thisLocation.Y);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square3.location = new Point(thisLocation.X + 2 * squareSize, thisLocation.Y);
                    square4.location = new Point(thisLocation.X + 3 * squareSize, thisLocation.Y);
                    break;
                case BlockTypes.J:
                    //组合成J形
                    square1.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y + squareSize);
                    square3.location = new Point(thisLocation.X + squareSize, thisLocation.Y + 2 * squareSize);
                    square4.location = new Point(thisLocation.X, thisLocation.Y + 2 * squareSize);
                    break;
                case BlockTypes.L:
                    //组合成l形
                    square1.location = new Point(thisLocation.X, thisLocation.Y);
                    square2.location = new Point(thisLocation.X, thisLocation.Y + squareSize);
                    square3.location = new Point(thisLocation.X, thisLocation.Y + 2 * squareSize);
                    square4.location = new Point(thisLocation.X + squareSize, thisLocation.Y + 2 * squareSize);
                    break;
                case BlockTypes.T:
                    //组合成T形
                    square1.location = new Point(thisLocation.X, thisLocation.Y);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square3.location = new Point(thisLocation.X + 2 * squareSize, thisLocation.Y);
                    square4.location = new Point(thisLocation.X + squareSize, thisLocation.Y + squareSize);
                    break;
                case BlockTypes.Z:
                    //组合成z形
                    square1.location = new Point(thisLocation.X, thisLocation.Y);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square3.location = new Point(thisLocation.X + squareSize, thisLocation.Y + squareSize);
                    square4.location = new Point(thisLocation.X + 2 * squareSize, thisLocation.Y + squareSize);
                    break;
                case BlockTypes.S:
                    //组合成S形
                    square1.location = new Point(thisLocation.X, thisLocation.Y + squareSize);
                    square2.location = new Point(thisLocation.X + squareSize, thisLocation.Y + squareSize);
                    square3.location = new Point(thisLocation.X + squareSize, thisLocation.Y);
                    square4.location = new Point(thisLocation.X + 2 * squareSize, thisLocation.Y);
                    break;
            }
        }

        /*画方块*/
        public void Draw(System.IntPtr winHandle)
        {
            square1.Draw(winHandle);
            square2.Draw(winHandle);
            square3.Draw(winHandle);
            square4.Draw(winHandle);
        }
        /*擦方块*/
        public void Erase(System.IntPtr winHandle)
        {
            square1.Erase(winHandle);
            square2.Erase(winHandle);
            square3.Erase(winHandle);
            square4.Erase(winHandle);
        }

        /*移动*/
        public bool down()
        {
            //检测是否可以下移
            if (GameField.isEmpty(square1.location.X / squareSize, square1.location.Y / squareSize + 1) &&
                GameField.isEmpty(square2.location.X / squareSize, square2.location.Y / squareSize + 1) &&
                GameField.isEmpty(square3.location.X / squareSize, square3.location.Y / squareSize + 1) &&
                GameField.isEmpty(square4.location.X / squareSize, square4.location.Y / squareSize + 1))
            {
                Erase(GameField.winHandle);
                square1.location = new Point(square1.location.X, square1.location.Y + squareSize);
                square2.location = new Point(square2.location.X, square2.location.Y + squareSize);
                square3.location = new Point(square3.location.X, square3.location.Y + squareSize);
                square4.location = new Point(square4.location.X, square4.location.Y + squareSize);
                Draw(GameField.winHandle);
                return true;
            }
            else  //如果不能下移了
            {
                GameField.stopSquare(square1, square1.location.X / squareSize, square1.location.Y / squareSize);
                GameField.stopSquare(square2, square2.location.X / squareSize, square2.location.Y / squareSize);
                GameField.stopSquare(square3, square3.location.X / squareSize, square3.location.Y / squareSize);
                GameField.stopSquare(square4, square4.location.X / squareSize, square4.location.Y / squareSize);
                return false; //表示可以弹出下一个block了
            }

⌨️ 快捷键说明

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