菜鸟日记 || C# | 俄罗斯方块(1)

6.1

今天只在晚上做了一点,之前想到什么就打了,导致结构有点混乱,隔得时间太久就不记得当时的思路了

现在的话从小元素开始做,再组成游戏主体,只不过这样就太方便测试了

今天主要做了俄罗斯方块类

方块基类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace TetrisBLL.MyObject
{
    public class Block
    {
        //方块基类
        public const int TOLEFT = -1;   //表示向左的参数
        public const int TORIGHT = 1;   //表示向右的参数
        public const int TOUP = 2;      //表示向上
        public const int TODOWN = -2;   //表示向下

        public static readonly Color[] COLOR = new Color[7] {Color.PaleGreen, Color.PaleGoldenrod, Color.LightSkyBlue, Color.DarkSlateBlue, Color.Plum, Color.Pink, Color.BlanchedAlmond };
        //规定好7种方块的颜色

        protected Point[] body; //方块的基本组成是Point
        protected bool status;  //方块状态
        protected int shape;    //方块形状
        protected Point position;   //方块位置,由左上角的点表示
        protected int type = 0;   //方块类型

        public Point[] Body
        {
            get
            {
                return body;
            }
        }
        public int Type
        {
            get
            {
                return type;
            }
        }
        public bool Status
        {
            get
            {
                return status;
            }
            set
            {
                status = value;
            }
        }

        public Block(Point p)
        {
            status = true;
            position = p;
            body = new Point[4];    //每个方块都是由四个点组成的
            shape = 0;  //初始形状
        }

        protected virtual void init(int shape)
        {
            //构建块的函数
        }
        public void DrawSelf(Graphics graphics)
        {
            //绘制块
            Brush brush = new SolidBrush(Block.COLOR[type]);    //根据类型决定颜色
            Pen pen = new Pen(Color.White); //白边
            for (int i = 0; i < 4; i++)
            {
                graphics.FillRectangle(brush, body[i].X* Constant.CELL_LEN, body[i].Y * Constant.CELL_LEN, Constant.CELL_LEN, Constant.CELL_LEN);
                graphics.DrawRectangle(pen, body[i].X * Constant.CELL_LEN, body[i].Y * Constant.CELL_LEN, Constant.CELL_LEN, Constant.CELL_LEN);
            }

        }
        public void Change()
        {
            //改形状
            shape = ( shape + 1 ) % 4;
            init(shape);
        }
        public void ToDown()
        {
            //向下(加速)
            position.Y++;
            for (int i = 0; i < 4; i++)
            {
                body[i].Y ++;
            }
        }
        public void ToLeft()
        {
            //向左
            position.X--;
            for (int i = 0; i < 4; i++)
            {
                body[i].X--;
            }
        }
        public void ToRight()
        {
            //向右
            position.X++;
            for (int i = 0; i < 4; i++)
            {
                body[i].X++;
            }
        }

    }
}

继承基类创建7种方块

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;   //导入Point所在命名空间

namespace TetrisBLL.MyObject
{
    public class Block2:Block
    {
        public Block2(Point p):base(p)
        {
            type = 2;
            init(shape);
        }

        protected override void init(int shape)
        {
            switch (shape)
            {
                case 0:
                    body[0] = new Point(position.X + 0, position.Y + 0);
                    body[1] = new Point(position.X - 1, position.Y + 0);
                    body[2] = new Point(position.X + 1, position.Y + 0);
                    body[3] = new Point(position.X + 0, position.Y - 1);
                    break;
                case 1:
                    body[0] = new Point(position.X + 0, position.Y + 0);
                    body[1] = new Point(position.X + 0, position.Y - 1);
                    body[2] = new Point(position.X + 0, position.Y + 1);
                    body[3] = new Point(position.X + 1, position.Y + 0);
                    break;
                case 2:
                    body[0] = new Point(position.X + 0, position.Y + 0);
                    body[1] = new Point(position.X - 1, position.Y + 0);
                    body[2] = new Point(position.X + 0, position.Y + 1);
                    body[3] = new Point(position.X + 1, position.Y + 0);
                    break;
                case 3:
                    body[0] = new Point(position.X + 0, position.Y + 0);
                    body[1] = new Point(position.X + 0, position.Y - 1);
                    body[2] = new Point(position.X - 1, position.Y + 0);
                    body[3] = new Point(position.X + 0, position.Y + 1);
                    break;
            }
        }
    }
}

用一个工厂类根据输入类型参数创建对应的块

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace TetrisBLL.MyObject
{
    public class BlockFactory
    {
        public static Block CreateBlock(int type,Point position)
        {
            switch (type)
            {
                case 0: 
                    return new Block0(position);
                case 1:
                    return new Block1(position);
                case 2:
                    return new Block2(position);
                case 3:
                    return new Block3(position);
                case 4:
                    return new Block4(position);
                case 5:
                    return new Block5(position);
                case 6:
                    return new Block6(position);
                default:
                    return new Block0(position);
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bekote

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值