0

So Im making tetris and I dont know how to draw the blocks( L,I,Z etc) I have one block as Texture2D and every class for the blocks look like this:

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;
        }
    }

and this is the block class:

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();
        }

So the problem is: I dont know how to draw those blocks (I know how to draw one block but I want the complete ones). I thought maybe ZBlock.vorm or ZBLock.zblock but both give errors.

Does anyone know how to draw the blocks?

crashmstr
  • 28,043
  • 9
  • 61
  • 79
  • I really struggle with classes and sub classes – phate river Oct 11 '15 at 23:14
  • I don't have that much experience with xna, but the way that would do it is to take the boolean arrays and then iterate through all of them and build up the section of sprites from there, you could also just codify the entire game space onto a grid and say that only 1 sprite is allowed to occupy each time. – Cjen1 Oct 12 '15 at 22:21
  • Basically just draw each selection of blocks as n sprites that all move as one and are separated by x pixels – Cjen1 Oct 12 '15 at 22:22
  • Could you write it in code so Its better to understand? Because Im still a newbie its difficult for me to get information like this – phate river Oct 12 '15 at 23:21

1 Answers1

0

Ok so here is a partial answer. What you want to do is basically just draw each block with a certain offset from the next block equal to: blockWidth / 2 in pixels. This means that the blocks will be correctly orientated without overlap.

Here is what you should put in the draw statement:

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), 
    }
}

This is from a blog: http://www.xnadevelopment.com/tutorials/fallingblocksyoumovetomakelines/fallingblocksyoumovetomakelines.shtml

The source code is at the top of the page.

Cjen1
  • 1,826
  • 3
  • 17
  • 47