【问题标题】:how to draw the blocks for tetris如何绘制俄罗斯方块的块
【发布时间】:2015-10-14 02:12:29
【问题描述】:

所以我在制作俄罗斯方块,但我不知道如何绘制块(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 但两者都会出错。

有谁知道如何画积木?

【问题讨论】:

  • 我真的很纠结类和子类
  • 我对 xna 没有太多经验,但是这样做的方法是获取布尔数组,然后遍历所有数组并从那里构建精灵部分,你也可以只是将整个游戏空间编码到一个网格上,并说每次只允许占用 1 个精灵。
  • 基本上只是将每个选择的块绘制为 n 个精灵,它们作为一个整体移动并由 x 个像素分隔
  • 你能不能把它写成代码以便更好地理解?因为我还是个新手,我很难得到这样的信息

标签: c# tetris


【解决方案1】:

好的,这里是部分答案。你想要做的基本上只是绘制每个块与下一个块有一定的偏移量,等于: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

源代码在页面顶部。

【讨论】:

    猜你喜欢
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多