简体   繁体   中英

Pictureboxes giving their image to pictureboxes below them

I'm making a simple game in C# winforms. What I'm trying to make is a tap tile kind of game that uses black and gray tiles. The objective is the hit the black tile but theres a catch, every second the black tile changes possition.

I've made it work the way I want but now its time to tweak! Whilst doing this i've ran into a problem I can't seem to figure out. This is what I'm trying to do:

http://prntscr.com/3llwoe

I have a grid of 3*4 made out of picture boxes. the first row is randomly generated considoring of 1 black and 3 gray tiles. This needs to remain this way for a second then the first row of tiles needs to move to the second row and the first row need to be randomly generated again. Then the tiles on the second rown need to move to the third row, the tiles on the first row to the second, and the first row needs to be generated again.

I made this work but not in the way i want.

http://prntscr.com/3llyxy

As all of my picture boxes have the same color combination. Could anyone help me figure this out? or push me in the right direction? I will provide my code if its requested as i have no idea what to add to my post right now.

I'm sorry for the links but I couldn't get the images to load in!

This is my method for the tiles:

public void RandomPanel()
    {

        if (i == 1)
        {
                i++;
                Random rnd = new Random();
                int temp = rnd.Next(1, 5);
                switch (temp)
                {
                    case 1:
                        TilelLoc = 1;
                        PbRow3_1.BackgroundImage = _2048Tiles.Properties.Resources.BlackTile;
                        PbRow3_2.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        PbRow3_3.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        PbRow3_4.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;

                        break;
                    case 2:
                        TilelLoc = 2;
                        PbRow3_2.BackgroundImage = _2048Tiles.Properties.Resources.BlackTile;
                        PbRow3_1.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        PbRow3_3.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        PbRow3_4.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        break;
                    case 3:
                        TilelLoc = 3;
                        PbRow3_3.BackgroundImage = _2048Tiles.Properties.Resources.BlackTile;
                        PbRow3_1.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        PbRow3_2.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        PbRow3_4.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        break;
                    case 4:
                        TilelLoc = 4;
                        PbRow3_4.BackgroundImage = _2048Tiles.Properties.Resources.BlackTile;
                        PbRow3_1.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        PbRow3_2.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        PbRow3_3.BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
                        break;
                    default:
                        break;
                }      
        }

        else if (i == 2)
        {
            PbRow2_1.BackgroundImage = PbRow3_1.BackgroundImage;
            PbRow2_2.BackgroundImage = PbRow3_2.BackgroundImage;
            PbRow2_3.BackgroundImage = PbRow3_3.BackgroundImage;
            PbRow2_4.BackgroundImage = PbRow3_4.BackgroundImage;
            i++;
        }
        else if (i == 3)
        {
            i = 1;
            PbRow1_1.BackgroundImage = PbRow2_1.BackgroundImage;
            PbRow1_2.BackgroundImage = PbRow2_2.BackgroundImage;
            PbRow1_3.BackgroundImage = PbRow2_3.BackgroundImage;
            PbRow1_4.BackgroundImage = PbRow2_4.BackgroundImage;
        }
    }

A couple of issues. Your i variable doesn't work because you want to randomize the tiles of row 1 every time you call this method, and after that, the colors should kind of scroll from one to the other. That requires you to first set the following rows to the values of the row above it before giving the first row the new randomness:

private int counter = 0;
Random rnd = new Random();

public void RandomPanel() {
  ++counter;

  if (counter > 2) {
    PbRow1_1.BackgroundImage = PbRow2_1.BackgroundImage;
    PbRow1_2.BackgroundImage = PbRow2_2.BackgroundImage;
    PbRow1_3.BackgroundImage = PbRow2_3.BackgroundImage;
    PbRow1_4.BackgroundImage = PbRow2_4.BackgroundImage;
  }
  if (counter > 1) {
    PbRow2_1.BackgroundImage = PbRow3_1.BackgroundImage;
    PbRow2_2.BackgroundImage = PbRow3_2.BackgroundImage;
    PbRow2_3.BackgroundImage = PbRow3_3.BackgroundImage;
    PbRow2_4.BackgroundImage = PbRow3_4.BackgroundImage;
  }

  int temp = rnd.Next(0, 4);
  Control[] boxes = new Control[] { PbRow3_1, PbRow3_2, PbRow3_3, PbRow3_4 };
  for (int i = 0; i < boxes.Length; ++i) {
    if (i == temp) {
      boxes[i].BackgroundImage = _2048Tiles.Properties.Resources.BlackTile;
    } else {
      boxes[i].BackgroundImage = _2048Tiles.Properties.Resources.GrayTile;
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM