创建倒数时钟

时间:2019-04-09 09:21:46

标签: c# winforms

我正在创建一款类似太空侵略者的游戏,我希望它有一个可以在其中得分的时间限制,一个让游戏增加得分的时间,但不确定如何做倒计时,我希望它运行大约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();
                }
            }

        }

1 个答案:

答案 0 :(得分: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;
 }

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