创建倒计时时钟

问题描述 投票:0回答:1

我正在创造一个类似太空入侵者的游戏,我正在寻找它有一个时间限制,你可以在其中得分,一个得到游戏添加得分等但不知道你如何做倒计时钟,我喜欢它约3分钟,然后在最后只是为了停止游戏并显示一个消息框,以显示收到的分数

public partial class Form1 : Form
{

    private List<Invader> invaders = new List<Invader>();
    private List<Laser> lasers = new List<Laser>();

    int invaderNumber = 0;
    int score = 0;



    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode.Equals(Keys.W))
        {
            if (SpaceFighter.Top > 0)
            {
                SpaceFighter.Top = SpaceFighter.Top - 30;

            }
        }
        if (e.KeyCode.Equals(Keys.A))
        {
            if (SpaceFighter.Left > 0)
            {
                SpaceFighter.Left = SpaceFighter.Left - 10;

            }
        }
        if (e.KeyCode.Equals(Keys.D))
        {
            if (SpaceFighter.Right < this.Width)
            {
                SpaceFighter.Left = SpaceFighter.Left + 10;

            }
        }
        if (e.KeyCode.Equals(Keys.S))
        {
            if (SpaceFighter.Bottom < this.Height - 10)
            {
                SpaceFighter.Top = SpaceFighter.Top + 10;

            }
        }
        if (e.KeyCode.Equals(Keys.Space))
        {
            this.lasers.Add(new Laser(this, SpaceFighter));
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

            if (invaderNumber > 4)
            {
                timer1.Enabled = false;
                timer2.Enabled = true;
            }
            else
            {
                invaders.Add(new Invader(this));
                invaderNumber++;
            }



    }

    private void timer2_Tick(object sender, EventArgs e)
    {

        invaders.RemoveAll(ship => ship.isDisposed);
        foreach(Invader ship in invaders)
        {
            ship.MoveInvader(this);
            if (SpaceFighter.Bounds.IntersectsWith(ship.ship.Bounds))
            {
                timer2.Enabled = false;
                MessageBox.Show("You Lose!");
                return;

            }
        }

        lasers.RemoveAll(laser => laser.isDisposed);
        foreach (Laser laser in lasers)
        {
            laser.MoveLaser(this);
            foreach (Invader ship in invaders)
            {
                if (laser.laser.Bounds.IntersectsWith(ship.ship.Bounds))
                {
                    laser.isDisposed = true;
                    laser.laser.Dispose();
                    ship.isDisposed = true;
                    ship.ship.Dispose();
                    score = score + 2;
                    lblScore.Text = score.ToString();
                }
            }

        }
c# winforms
1个回答
0
投票

创建倒计时。请参考示例代码

Sample Output

private int m_ntimeLeft;
private void btnStart_Click(object sender, EventArgs e)
{
    //Rather than getting from a text box. You can
    //hardcode as 00:03:00 since your requirement is 3 minutes
    string[] totalSeconds = txtTotalTime.Text.Split(':');

   //You can Avoid Hours if not required.
   int nHours = Convert.ToInt32(totalSeconds[0]);
   int nMinutes = Convert.ToInt32(totalSeconds[1]);
   int nSeconds = Convert.ToInt32(totalSeconds[2]);
   m_ntimeLeft = (nHours * 60 * 60) + (nMinutes * 60) + nSeconds;

   timerCountDown.Enabled = true;
   timerCountDown.Start();

   btnStart.Enabled = false;
   btnStop.Enabled = true;
}

private void timerCountDown_Tick(object sender, EventArgs e)
{
    if (m_ntimeLeft > 0)
    {
       m_ntimeLeft = m_ntimeLeft - 1;
       var timespan = TimeSpan.FromSeconds(m_ntimeLeft);
       lblCounter.Text = timespan.ToString(@"hh\:mm\:ss");                
     }
     else
     {
        timerCountDown.Stop();
        btnStart.Enabled = true;
        btnStop.Enabled = false;
        txtTotalTime.Text = "00:00:00";

        //Call Stop Game Function
        MessageBox.Show("Time's up..!", "Notification", MessageBoxButtons.OK);    
      }
 }

 private void btnStop_Click(object sender, EventArgs e)
 {
    timerCountDown.Enabled = false;
    timerCountDown.Stop();
    btnStart.Enabled = true;
    lblCounter.Text = "00:00:00";
    btnStop.Enabled = false;
 }

注意:由于您将其称为游戏,因此您可能应该在单独的后台线程中执行指定的解决方案。如有必要,请确保相同。

© www.soinside.com 2019 - 2024. All rights reserved.