在C#中的richTextBox中移动数字 [英] Moving numbers in richTextBox in C#

查看:72
本文介绍了在C#中的richTextBox中移动数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



- 移动数字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



- 或顺时针旋转数字后1



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





这是我的代码:



I need to solve behavior of four numbers, which will move down or rotate as the meaning of the game Tetris. This is that I want to do on richTextBox in C#, but my code is still not working good. I want to do as illustrated below. How can I do to that numbers are moving in right direction?

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

- after moving numbers of 1 down

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

- or also after rotating clockwise numbers of 1

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


Here is a my code:

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

 }

推荐答案

嗨Andrej,



虽然我可以想象你在这里尝试做什么,但我建议你考虑一个完整的替代方法。



1.不要乱用字符串(在你的情况下存储在字符串数组中的字符 - 更糟糕的是)..所以创建一个抽象你的董事会(游戏领域无论你怎么称呼它),例如2维数量的布尔值(你只有2个状态,不是吗? - 填充(1)或不是(0))。



2.不要试图一步到位。将代码拆分为不同的函数/方法。例如。为你想要支持的每个逻辑操作创建一个方法 - 向下移动,旋转,...... - 然后只需在板阵列中移动(或覆盖)布尔值。在这些方法中你可以考虑边缘情况(比如旋转是不可能的,因为你的棋子变大等。)



3.你有newboard-array(所有应用的操作)尝试在一步中渲染 - 例如,如果你真的想使用RichTextBox(考虑BillWoodruff的想法),只需根据你的新板重写。



所以你看,如果你以后决定用它制作一个真正的俄罗斯方块 - 用图形 - 你将拥有所有的逻辑 - 只是渲染改变。



示例(只是快速提出想法):

将您的电路板表示为2d数组
Hi Andrej,

While I can imagine what you try to do here, I would suggest you consider a complete alternative approach.

1. Don't mess arround with strings (in your case characters stored in a string Array - even worse).. So create an "abstraction" of your "board" (game-field whatever you call it) e.g as 2 dimensional Array of booleans (you only have 2 states, don't you? - filled (1) or not (0)).

2. Don't try to do everything in one step. Split your code into different functions/methods. E.g. Create a method for each logical operation you want to support - to "move down", "rotate", ... - then just move arround (or overwrite) the booleans in the "board"-array. In these methods you can consider edge-cases (like rotation isn't possible cause your Piece is "to big" etc.).

3. After you have the "new" board-array (all operations applied) try to "render it" in one step - e.g if you really want to use the RichTextBox (consider BillWoodruff's idea) just rewrite according to your new board.

So you see, if you later decide to make a "real" tetris out of it - with graphics - you will have all the logic in place - just the rendering to change.

Examples (just quick ones to give the idea):
Represent your board as 2d Array
bool[,] board = new bool[20, 13];

- 甚至更好地创建一个处理内部数组逻辑的板类。

代表一个pice作为你董事会上坐标的列表;



- or even better create a board class that handles the "array-logic" internal.
Represent a pice as list of "coordinates" on your board;

struct Coordinate
{
    public int X { get; set; }
    public int Y { get; set; }
}





A Piece不是坐标的集合



A Piece is than a "collection" of coordinates

class Piece
{
    public Coordinate[] Coordinates { get; set; }
}





所以你的操纵方法可能有这样的签名:



So your Manipulation methods could have signatures like this:

public void MoveDown(Piece piece, bool[,] board) {  /* change board */ }





和渲染可以这样做 - 只需通过你的主板上的所有地方,填写你的RichTextBox,网格,标签或其他 - 这里我只使用控制台。





And Rendering could be done like this - just go through all "places" on your board and fill up your RichTextBox, Grid, Labels or whatever - here i used just the console.

static void Render(bool[,] board)
{
    for (int iColumn = 0; iColumn < board.GetLength(1); iColumn++)
    {
        for (int iRow = 0; iRow < board.GetLength(0); iRow++)
        {
            Console.Write(board[iRow, iColumn] ? "1" : "0");
        }
        Console.WriteLine();
    }
}





所以你看看在俄罗斯方块游戏中一切都是如何排序到真正的对象 , - 董事会,棋子,职位 - 这是OOP - 我假设你正在学习如何用这样的玩具项目(或家庭作业)编程对象...


快乐的编码和

亲切的问候



Johannes



So you see how everything sorts itself out to real "objects" in a Tetris game, - the board, the pieces, the positions - this is OOP - and I assume you are learning how to program object orientated with such a toy-project (or homework)...

Happy coding and
Kind regards

Johannes


这篇关于在C#中的richTextBox中移动数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆