如何使我的主窗体暂停执行1秒钟然后继续在C#中执行?

时间:2019-01-15 14:35:48

标签: c# winforms

我正在创建一个纸牌游戏,我需要一种方法执行来暂停在整个屏幕上制作纸牌动画时的播放。目前,我拥有此功能,它可以对卡片进行动画处理,但是会在对卡片进行动画处理时继续玩游戏。不知道如何去做。我已经尝试过Thread.Sleep(),但它仍然继续所有可能的执行,然后暂停。

private void btnPlay_Click(object sender, EventArgs e)
        {
            try
            {
                string input = Microsoft.VisualBasic.Interaction.InputBox("Please enter your betting amount (£3.00 minimum bet)", "Play", "3.00", -1, -1);
                bet = double.Parse(input);
                if (Globals.Balance > bet)
                {
                    btnHit.Enabled = true;
                    btnStick.Enabled = true;
                    Globals.Balance -= bet;
                    lblBalance.Text = Globals.Balance.ToString();
                    Play();
                }
                else
                {
                    throw new Exception("You don't have enough money!");
                }

            }
            catch (FormatException)
            {
                MessageBox.Show("Incorrect format for betting amount");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

private void Play()
        {
            ClearDetails();
            DealPlayerCard();
            Classes.Deck.NextCard(Deck);
            DealPlayerCard();
            UpdatePlayerTotal();
        }

    private void DealPlayerCard()
            {
                //does stuff here

                switch (cardNum)
                {
                    case 3:
                        pb.Location = new Point(120, 0);
                        timerCard.Enabled = true;
                        timerCard_Tick(null, null);
                        break;
                    case 4:
                        pb.Location = new Point(180, 0);
                        timerCard.Enabled = true;
                        timerCard_Tick(null, null);
                        break;
                    case 5:
                        pb.Location = new Point(240, 0);
                        timerCard.Enabled = true;
                        timerCard_Tick(null, null);
                        break;
                }
                AddPlayerCard(pb);
                AddToHand("Player");
            }

    private void timerCard_Tick(object sender, EventArgs e)
            {
                this.SuspendLayout();
                //sets x and y in a switch statement here

                if ((CardBack.Location.X == x) && (CardBack.Location.Y == y))
                {
                    timerCard.Enabled = false;
                    CardBack.Visible = false;
                    CardBack.Location = new Point(775, 247);
                    this.ResumeLayout();
                }
                else if ((CardBack.Location.X > 417) && (CardBack.Location.Y < 434))
                {
                    CardBack.Location = new Point(CardBack.Location.X - 1, CardBack.Location.Y + 1);
                    timerCard_Tick(null, null);
                }
                else if ((CardBack.Location.X > 417) && (CardBack.Location.Y == 434))
                {
                    CardBack.Location = new Point(CardBack.Location.X - 1, CardBack.Location.Y);
                    timerCard_Tick(null, null);
                }
                else if ((CardBack.Location.X == 417) && (CardBack.Location.Y < 434))
                {
                    CardBack.Location = new Point(CardBack.Location.X, CardBack.Location.Y + 1);
                    timerCard_Tick(null, null);
                }
            }

1 个答案:

答案 0 :(得分:1)

启用计时器会立即返回,并且计时器事件会单独发生,这就是为什么您看到游戏继续的原因。

在动画完成后,将游戏的其余部分移至计时器事件。像这样(伪代码):

private void DealPlayerCard()
{
    //does stuff here
    timer.Enabled = true;
    // no more code
}

private void timerCard_Tick(object sender, EventArgs e)
{
     switch (location)
         case first:
             DoSomeStuff();
         case next:
             DoSomeOtherStuff();
         case finished:
             timer.enabled = false;
             continueGame();
 } 

我想您的示例中没有某种游戏循环,您可能还需要暂停。