如何在c#

时间:2015-10-12 21:40:06

标签: c# xna

所以我制作俄罗斯方块,我需要制作7个街区。问题是我必须复制粘贴7个不同类中的代码(然后更改网格),所以我想也许使用子类会更容易。问题是我不知道该怎么做。

这是我的通用块类:

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

    Texture2D block;
    float FallTimer;
    public bool FallingBlock = false;
    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 void HandleInput()
    {
        if (Tetris.inputHelper.KeyPressed(Keys.Down))
        {
            FallTimer = 0;
            for (int i = 0; i < blokvorm.GetLength(0); i++)
            {
                for (int j = 0; j < blokvorm.GetLength(1); j++) ;
                // blokvorm[i, j] += 15;
            }
        }
    }

    public void speed(GameTime gameTime)
    {
        double SpeedCounter = 3;
        int LevelCounter = 1;
        if (LevelCounter < Tetris.GameWorld.level)
        {
            SpeedCounter -= 0.5;
            LevelCounter++;
        }
        FallTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
        if (Math.Round(FallTimer, 1) == SpeedCounter)
        {
            FallTimer = 0;
            //BlockPosition.Y += 15;

        }
    }

    public bool[,] blokvorm
    {
        get
        {
            vorm = new bool[x, y];
            for (int i = 0; i < x; i++)
                for (int j = 0; j < y; j++)
                {
                    vorm[i, j] = false;
                }


                    int r = Random.Next(7);
            if (r == 0)
            {
                vorm = ZBlock.zblock();
            }
            else if (r == 1)
            {
                vorm = TBlock.tblock();
            }
            else if (r == 2)
            {
                vorm = SBlock.sblock();
            }
            else if (r == 3)
            {
                vorm = OBlock.oblock();
            }
            else if (r == 4)
            {
                vorm = JBlock.jblock();
            }
            else if (r == 5)
            {
                vorm = LBlock.lblock();
            }
            else if (r == 6)
            {
                vorm = 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(SpriteBatch spriteBatch, ContentManager Content)
    {
        block = Content.Load<Texture2D>("Block");
        spriteBatch.Begin();
        if (!FallingBlock)
        {
            for (int i = 0; i < blokvorm.GetLength(0); i++)
            {
                for (int j = 0; j < blokvorm.GetLength(1); j++)
                    if (blokvorm[i, j])
                        spriteBatch.Draw(block, new Vector2(30 + (i * 15), 30 + (j * 15)), Color.White);
            }
            spriteBatch.End();
        }
    }
}

}

那么如何使用这个类轻松创建其他7个块类(T,L,I,O等)?

编辑:我的块看起来像这样:

{

    const int x = 4;

    const int y = 4;


    public bool[,] tblock()

    {
        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, 1] = true;
                vorm[1, 1] = true;
                vorm[1, 0] = true;
                vorm[2, 1] = true;
            }


        Color = Color.Indigo;
        return vorm;
    }
    public void RotationLinks(InputHelper inputhelper)
    {
        bool[,] nieuw = new bool[x, y];
        if (inputhelper.KeyPressed(Keys.Left))
        {
            for (int a = 0; a < 4; ++a)

                for (int b = 0; b < 4; ++b)

                    nieuw[a, b] = vorm[x - a - 1, b];


            vorm = nieuw;


        }
    }


}

}

为了使其他6我复制整个代码并更改网格值。是否有可能使用bock类更容易?

0 个答案:

没有答案