0

对于我的编程课,我们需要制作一个记忆匹配游戏。当玩家点击面板时,会显示一个图像。玩家点击两个面板,如果图像匹配,则将其删除,如果它们不同,则将其正面朝下。

我遇到的问题是只有第一个面板中的图像被显示,即使我使用同一行代码来显示两个面板中的图像,并使用 Thread.Sleep 在第二个图块之后暂停程序已被选中。我不明白为什么会这样,任何帮助将不胜感激。

    private void tile_Click(object sender, EventArgs e)
    {
        string tileName = (sender as Panel).Name;
        tileNum = Convert.ToInt16(tileName.Substring(5)) - 1;
        //figure out if tile is locked
        if (panelArray[tileNum].isLocked == false)
        {                 
            pickNum++;
            //although the following line of code is used to display the picture that is stored in the tile array
            //what is happening is that it will only display the picture of the first tile that has been picked.  
            //when a second tile is picked my program seems to ignore this line completely, any ideas?
             panelArray[tileNum].thisPanel.BackgroundImage = tiles[tileNum].tileImage;

            if (pickNum == 1)
            {                    
                pick1 = tileNum;                    
                panelArray[tileNum].isLocked = true;    
            }

            else
            {                    
                pick2 = tileNum;                  
                UpdateGameState();                   
            }
        }
    }

    private void UpdateGameState()
    {       
        Thread.Sleep(1500); 

        if (tiles[pick1].tag == tiles[pick2].tag)//compares tags to see if they match.
        {
            RemoveTiles();
        }
        else
        {
            ResetTiles();
        }

        pickNum = 0;
        guess += 1;
        guessDisplay.Text = Convert.ToString(guess);

        if (correct == 8)
        {
            CalculateScore();
        }            
    }
4

1 回答 1

0

尝试这个:

(sender as Panel).BackgroundImage = tiles[tileNum].tileImage;

您必须确保您的 tile_Click 方法与两个面板相关联...

于 2012-08-26T11:24:00.410 回答