c#面板图像仅更新一半时间

时间:2012-08-26 10:12:13

标签: c# winforms

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

我遇到的问题是只显示第一个面板中的图像,即使我使用相同的代码行显示两个面板中的图像,并使用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();
        }            
    }

1 个答案:

答案 0 :(得分:0)

试试这个:

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

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