如何绘制俄罗斯方块块

时间:2015-10-11 21:43:15

标签: c# tetris

所以我制作俄罗斯方块,我不知道如何绘制块(L,I,Z等)我有一个块作为Texture2D,块的​​每个类看起来像这样:

namespace Tetris
{
    public class ZBlock
    {
        Color Color;
        const int x = 4;
        const int y = 4;
        bool[,] vorm;

        public bool[,] zblock()
        {
            vorm = new bool[x, y];
            for(int i=0; i< x; i++)
                for (int j=0; j<y; j++)
                {
                    vorm[i, j] = false;
                    vorm[0, 0] = true;
                    vorm[1, 0] = true;
                    vorm[1, 1] = true;
                    vorm[2, 1] = true;
                }

            Color = Color.Purple;
            return vorm;
        }
    }

这是块类:

namespace Tetris
{
    public class Block
    {
        Texture2D block;
        Vector2 BlockPosition = new Vector2(30, 30);
        float FallTimer;
        Random Random = new Random();

        ZBlock zBlock = new ZBlock();
        TBlock tBlock = new TBlock();
        SBlock sBlock = new SBlock();
        OBlock oBlock = new OBlock();
        JBlock jBlock = new JBlock();
        LBlock lBlock = new LBlock();
        IBlock iblock = new IBlock();
        public bool[,] blockvorm()
        {
            bool[,] vorm;
            vorm = new bool[4, 4];
            vorm[3, 3] = false;
            int r = Random.Next(7);
            if (r == 0)
            {
                ZBlock.zblock();
            }
            else if (r == 1)
            {
                TBlock.tblock();
            }
            else if (r == 2)
            {
                SBlock.sblock();
            }
            else if (r == 3)
            {
                OBlock.oblock();
            }
            else if (r == 4)
            {
                JBlock.jblock();
            }
            else if (r == 5)
            {
                LBlock.lblock();
            }
            else if (r == 6)
            {
                IBlock.iblock();
            }

            return vorm;
        }

        public TBlock TBlock
        {
            get { return tBlock; }
        }
        public ZBlock ZBlock
        {
            get { return zBlock; }
        }
        public SBlock SBlock
        {
            get { return sBlock; }
        }
        public OBlock OBlock
        {
            get { return oBlock; }
        }
        public JBlock JBlock
        {
            get { return jBlock; }
        }
        public LBlock LBlock
        {
            get { return lBlock; }
        }
        public IBlock IBlock
        {
            get { return iblock; }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, ContentManager Content)
        {
            block = Content.Load<Texture2D>("Block");
            int[,] Grid = Tetris.GameWorld.TetrisGrid;
            spriteBatch.Begin();
            spriteBatch.Draw(?????????????);
            spriteBatch.End();
        }

所以问题是:我不知道如何绘制这些块(我知道如何绘制一个块,但我想要完整的块)。我想可能是ZBlock.vormZBLock.zblock,但都会出错。

有谁知道如何绘制块?

1 个答案:

答案 0 :(得分:0)

好的,这是一个部分答案。你想要做的基本上只是绘制每个块与下一个块的某个偏移量相等于:blockWidth / 2(以像素为单位)。这意味着块将正确定向而不会重叠。

以下是您应该在draw语句中添加的内容:

public void Draw(int theXPosition, int theYPosition, Color theColor, SpriteBatch theSpriteBatch, Texture2D theBlockTexture)
{
    int aTextureStartX = Color * Convert.ToInt32(mBlockSize);
    for (int aBlock = 0; aBlock < mNumberOfBlocks; aBlock++)
    {
            int aXPosition = (int)(theXPosition + (CurrentShape[Rotation, aBlock, 0] * mBlockSize));
            int aYPosition = (int)(theYPosition + (CurrentShape[Rotation, aBlock, 1] * mBlockSize));
            theSpriteBatch.Draw(theBlockTexture, new Rectangle(aXPosition, aYPosition, mBlockSize, mBlockSize), new Rectangle(aTextureStartX, 0, mBlockSize, mBlockSize), 
    }
}

这是来自博客:http://www.xnadevelopment.com/tutorials/fallingblocksyoumovetomakelines/fallingblocksyoumovetomakelines.shtml

源代码位于页面顶部。