簡體   English   中英

圖片框將其圖像傳遞給其下方的圖片框

[英]Pictureboxes giving their image to pictureboxes below them

我正在用C#winforms做一個簡單的游戲。 我要制作的是一款使用黑色和灰色瓷磚的自來水瓷磚游戲。 目的是擊中黑色瓷磚,但有一個陷阱,黑色瓷磚每秒都會改變位置。

我已經按照我想要的方式進行了工作,但現在該進行調整了! 在執行此操作時,我遇到了一個我似乎無法弄清的問題。 這就是我想要做的:

http://prntscr.com/3llwoe

我有一個由圖片框組成的3 * 4網格。 第一行是隨機生成的1個黑色和3個灰色圖塊。 這需要保持這種方式一秒鍾,然后第一排磁貼需要移動到第二排,並且第一排需要再次隨機生成。 然后,第二行上的圖塊需要移動到第三行,第一行上的圖塊需要移動到第二行,並且第一行需要再次生成。

我做了這項工作,但沒有達到我想要的方式。

http://prntscr.com/3llyxy

因為我所有的圖片框都具有相同的顏色組合。 誰能幫我解決這個問題? 或將我推向正確的方向? 我將提供我的代碼(如果需要),因為我現在不知道要添加什么內容。

很抱歉,我的鏈接,但我無法加載圖像!

這是我的瓷磚方法:

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

有幾個問題。 您的i變量無效,因為您希望在每次調用此方法將第1行的圖塊隨機化,然后,顏色應從一種滾動到另一種。 這要求您首先將以下行設置為其上方的行的值,然后再為第一行賦予新的隨機性:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM