0

我需要解决四个数字的行为,这将作为游戏俄罗斯方块的含义向下移动或旋转。这是我想在 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;
     }

 }
4

1 回答 1

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
    }
}
于 2014-10-08T18:41:22.343 回答