A
A
Artur Deunezhev2019-07-01 14:36:28
C++ / C#
Artur Deunezhev, 2019-07-01 14:36:28

How to use multiple variables in one code?

The thing is, I’m sitting like this writing Tetris, and everything works simply, cubes are called in turn)
But there are 10 such cubes (a certain structure should be obtained) and each code is the same

if (D4.gameObject.name == "D-4 l")
            {
                if (xD4 < xD4s - 1 || xD4 > xD4s + 1)
                {
                    Console.text = "D4";
                    GameOver();
                }

                if (xD4 > xD4s - 1 && xD4 < xD4s + 1 || xD4 == xD4s)
                {
                    StartD5();
                }
            }

And there are 6 more such cubes, and for each one spend 20 lines, well, that's too much! And I have a question for the geniuses,
1: How to replace the variable D4 with D5, D5 with D6, etc. (it sounds like nonsense, but it is necessary: ​​D)
2: Or, what sounds more logical, substitute the values ​​of these variables, and then assign them to them, in C # (Done for
Unity3D ), in the code presented above (you can use your own examples, I just took a piece from the project
) geniuses)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Yudin, 2019-07-01
@naumnek

I would do so.
1. I would create a class in which I would describe the general game piece. Its coordinates and movement. Would store coordinates in the form of X0, Y0; X1, Y1; X2, Y2; X3, Y3; and pass the type of the figure to the constructor, and use the switch to build the figure.
2. The call to the figure would be done by generating a random number and then compared through enum. enum Figure { green, red, yellow... }
But I didn't quite understand what you mean by cube. That is, one square with the help of which a tetromino is built? If so, then this can obviously be simplified to writing a single class in which to generate a figure. Also, if you are making regular Tetris, with 7 pieces, then instead of a random number, use a bag of pieces , recommendations for it are written on the Tetris Wikipedia.
3. What do you use to store pieces on the playing field? I would make a separate class that uses a normal two-dimensional array to store all the shapes on the field, with methods to remove the filled lines.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question