1

I'm working on a C# tetris game, in my code i got a List<> of Rectangle Objects

private List<Rectangle> lstBlocks = new List<Rectangle>();

This list can contain a several types of rectangle collections, based on a random number (by generating a new TetrisBlock() )

public TetrisBlock(int size, int speelveldwith,Brush blockColor,int maxY)
    {
        this.size = size;
        this.blockColor = blockColor;
        this.maxY = maxY;

        int x_pos;
        x_pos = rnd.Next(0, speelveldwith - (size));
        if (x_pos % size != 0)
        {
            x_pos = (x_pos / size) * size;
        }

        switch (rnd.Next(1, 8))
        {
            case 1:
                // 4 blocks onder elkaar
                lstBlocks.Add(new Rectangle(x_pos, 0, size, size));
                lstBlocks.Add(new Rectangle(x_pos, size, size, size));
                lstBlocks.Add(new Rectangle(x_pos, size * 2, size, size));
                lstBlocks.Add(new Rectangle(x_pos, size * 3, size, size));
                iObjectHeight = size * 4;
                break;
            case 2:
                // L - Block
                lstBlocks.Add(new Rectangle(x_pos, 0, size, size));
                lstBlocks.Add(new Rectangle(x_pos, size, size, size));
                lstBlocks.Add(new Rectangle(x_pos+size, size * 2, size, size));
                lstBlocks.Add(new Rectangle(x_pos, size * 2, size, size));
                iObjectHeight = 3 * size;
                break;
            case 3:
                // I- met uitstekend puntje
                lstBlocks.Add(new Rectangle(x_pos, 0, size, size));
                lstBlocks.Add(new Rectangle(x_pos, size, size, size));
                lstBlocks.Add(new Rectangle(x_pos + size, size, size, size));
                lstBlocks.Add(new Rectangle(x_pos, size * 2, size, size));
                iObjectHeight = 3 * size;
                break;
            case 4:
                //omgedraaide L
                lstBlocks.Add(new Rectangle(x_pos, 0, size, size));
                lstBlocks.Add(new Rectangle(x_pos, size, size, size));
                lstBlocks.Add(new Rectangle(x_pos, size * 2, size, size));
                lstBlocks.Add(new Rectangle(x_pos + size, 0, size, size));
                iObjectHeight = 3 * size;
                break;
            case 5:
                //vierkantje
                lstBlocks.Add(new Rectangle(x_pos, 0, size, size));
                lstBlocks.Add(new Rectangle(x_pos, size, size, size));
                lstBlocks.Add(new Rectangle(x_pos + size, 0, size, size));
                lstBlocks.Add(new Rectangle(x_pos + size, size, size, size));
                iObjectHeight = 2 * size;
                break;
            case 6:
                //rupsje
                lstBlocks.Add(new Rectangle(x_pos, size, size, size));
                lstBlocks.Add(new Rectangle(x_pos + size, size, size, size));
                lstBlocks.Add(new Rectangle(x_pos + size, 0, size, size));
                lstBlocks.Add(new Rectangle(x_pos + size * 2, 0, size, size));
                iObjectHeight = 2 * size;
                break;
            case 7:
                //omgedraaid rupsje
                lstBlocks.Add(new Rectangle(x_pos, 0, size, size));
                lstBlocks.Add(new Rectangle(x_pos + size, 0, size, size));
                lstBlocks.Add(new Rectangle(x_pos + size, size, size, size));
                lstBlocks.Add(new Rectangle(x_pos + size * 2, size, size, size));
                iObjectHeight = 2 * size;
                break;
        }
    }

I have been working on paper to figure it out, but i'm stuck on rotating this collection of rectanglesenter image description here.

In this picture I have block case3

Does someone have some tips or hints for me?

Ferry Jongmans
  • 625
  • 2
  • 12
  • 22
  • 2
    Try thinking of it as a 3x3 matrix with some of the blocks not filled in (0 value), now you can use standard matrix code to rotate the block. – Ron Beyer Nov 16 '15 at 21:56

3 Answers3

1

I'd recommend to use a twodimensional array to define the shape of your tetromino, that would be a lot easier to handle with rotation:

bool[,] rotated = new bool[4,4];
for (int y = 0; y < Height; y++) {
    for (int x = 0; x < Width; x++) {
        rotated[Height - y - 1, x] = grid[x, y];
    }
}

This code works with 'sizes' of 1, so you can easily figure out the positions of the blocks.

However, in your case it'd be a bit harder to use this code, since you've already included your x offset in the position of the block (I'd recommend keeping the position somewhere else in your TetrisBlock, and summing it with the rectangles at rendering stage). If you would remove the x_pos you'd be able to do something like this:

List<Rectangle> tempRotated = new List<Rectangle>();
foreach(Rectangle rect in lstBlocks) {
    tempRotated.Add(new Rectangle((Height - 1) * size - rect.y, rect.x, size, size));
}
lstBlocks = tempRotated;

If we apply this to the block in your handwritten example, you'd get the following result:

The height in this case is 3 (3 blocks above eachother), so (Height - 1) * size=2*20=40

Rectangle 1 at 0, 0: the new x value will be 40-y=40, and the y value will be the x value, 0.
Rectangle 2 at 0,20: x=40-20=20, y=x=0
Rectangle 3 at 0, 40: x=40-40=0, y=x=0
Rectangle 4 at 20, 20: x=40-20=20, y=x=20

Now it's up to you to draw this one out and see how it works.

If you keep the x_pos in, you should change the code to new Rectangle((Height - 1) * size - rect.y + x_pos, rect.x - x_pos, size, size)

MarijnS95
  • 4,703
  • 3
  • 23
  • 47
0

My tip is create a class TetrisBlock. In this class, don't work with size, work with 1. Use size not before drawing. So you get easy blocks.

Give the class a List<Rectangle> for each of the four rotation. Here you can store rectangles for each rotation. That should be easy to figure out when you draw it on paper. Then, instead of rotating, select the next rotation.

Now create a List of all TetrisBlocks. Create all available blocks with all the rotations. Later then, choose an element (with a random number) of that list, create a copy and use it.

That should make things easier...

Dieter Meemken
  • 1,937
  • 2
  • 17
  • 22
0

I have fixed it by creating all possible combinations for the blocks in a list. Thanks anyway :)

Ferry Jongmans
  • 625
  • 2
  • 12
  • 22
  • 1
    This is not an answer, this is just an announcement. You should either put up a comment below your question saying this, or improve this answer to actually explain how you solved the problem. – Joeytje50 Nov 19 '15 at 11:57