正在移动C#中richTextBox中的数字

本文关键字:richTextBox 数字 移动 | 更新日期: 2023-09-27 18:25:53

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

0 0 0 011<1>10 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 01 1 1 10 0 0 0

0 0 0 0

0 0 0 0

0 0 0 0

或者在顺时针旋转四个数字的"1"之后

0 0 0 0

0 0 0 010 0 0

0 0 0 010 0 0

0 0 0 010 0 0

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

正在移动C#中richTextBox中的数字

好吧,我认为您的实现需要一些改进,但这里有一些代码将从几个形状开始,并将它们向下移动。他们…"与俄罗斯方块不同,俄罗斯方块的形状保持了僵硬。但我认为这回答了你的问题。剩下的由你决定。

编辑:我忘了提。我的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
    }
}