[英]How to make my main form pause execution for 1 second then continue in c#?
我正在創建紙牌游戲,並且需要在整個屏幕上制作紙牌動畫時暫停執行方法。 目前,我擁有此功能,它可以對卡片進行動畫處理,但是會在對卡片進行動畫處理時繼續玩游戲。 不知道如何去做。 我已經嘗試過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);
}
}
啟用計時器會立即返回,並且計時器事件會分別發生,這就是為什么您看到游戲繼續的原因。
在動畫完成后,將游戲的其余部分移至計時器事件。 像這樣(偽代碼):
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();
}
我猜想您的示例中沒有某種游戲循環,您可能還需要暫停一下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.