在C#中的richTextBox中移动数字

时间:2014-10-08 12:35:42

标签: c# numbers richtextbox move tetris

我需要解决四个数字的行为,它们将向下移动或旋转,作为游戏俄罗斯方块的含义。这是我想在C#中的richTextBox上做的,但我的代码仍然不能正常工作。我想做如下图所示。我怎么能这些数字朝着正确的方向发展?

0 0 0 0 1 1 1 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

向下移动四个" 1"

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 1 1 1 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0

或顺时针旋转四个" 1"

0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0 0 0 0

这是我的代码。

string[] pole8x8 = new string[400];
string[] pole4x4 = new string[4*2];
List<string> numbers = new List<string>();
int len = 52;

public Form1()
{
    InitializeComponent();

    for (int i = 0; i < pole8x8.Length; i+=2)
    {
        pole8x8[i] = "0 ";
        richTextBox1.Text += pole8x8[i];
        richTextBox1.BackColor = Color.Black;
        richTextBox1.ForeColor = Color.White;
    }

    for (int i = 0; i < pole4x4.Length; i+=2)
    {
        pole4x4[i] = "1 ";
        richTextBox1.SelectionStart = 18;
        richTextBox1.SelectedText = pole4x4[i];
        numbers.Add(pole4x4[i]);
     }

 }

 private void button1_Click(object sender, EventArgs e)
 {
     richTextBox1.SelectionStart += len;

     foreach (string s in numbers)
     {
        richTextBox1.SelectedText = s;
     }

 }

1 个答案:

答案 0 :(得分:0)

嗯,我认为你的实现需要一些改进,但是这里有一些代码将从几个形状开始并将它们向下移动。他们......在底部“崩溃”,不像俄罗斯方块,形状保持僵硬。但我认为这回答了你的问题。其余的由你决定。

编辑:我忘了提。我的RichTextBox设置为使用Courier New字体。 (和非真实类型字体应该有效。)

public partial class Form1 : Form
{
    private int _tickCounter = 0;
    private int _tickLimit = 500;  // set to 10 or something for the game
    private string[,] _dataArray;

    public Form1()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        _tickCounter++;
        if (_tickCounter >= _tickLimit)
        {
            // add a piece
            AddNewPieceToWell(true);
            _tickCounter = 0;
        }
        else
        {
            // Move the current pieces downward.
            for (int rowCounter = _dataArray.GetUpperBound(0); rowCounter >= 1; rowCounter--)
            {
                for (int colCounter = 0; colCounter <= _dataArray.GetUpperBound(1); colCounter++)
                {
                    if (_dataArray[rowCounter, colCounter] == " " && _dataArray[rowCounter - 1, colCounter] == "0")
                    {
                        _dataArray[rowCounter, colCounter] = "0";
                        _dataArray[rowCounter - 1, colCounter] = " ";
                    }
                }
            }
        }

        DrawWell();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        InitializeWell();
        timer1.Start();
    }

    private void InitializeWell()
    {
        _dataArray = new string[,]{
            {" ", " ", "0", "0", "0", "0", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " "},
            {"0", "0", "0", "0", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " "},
            {" ", " ", " ", " ", " ", " ", " ", " "}
        };
        DrawWell();
    }

    private void DrawWell()
    {
        rtbWell.Text = string.Empty;
        for (int rowCounter = 0; rowCounter <= _dataArray.GetUpperBound(0); rowCounter++)
        {
            for (int colCounter = 0; colCounter <= _dataArray.GetUpperBound(1); colCounter++)
            {
                rtbWell.Text += _dataArray[rowCounter, colCounter];
            }
            rtbWell.Text += Environment.NewLine;
        }
    }

    private void AddNewPieceToWell(bool RandomPiece = true)
    {
        // ToDo
    }
}