2

I'm building a Flappy Bird game in C#. So far, the code works perfectly, however I added a GIF saying Game Over, and this image only shows once timer1 is stopped.

How can I achieve that?

Below is the code snippet for the GIF image (I tried both snippets of code, and they didn't work though there was no error):

private void pictureBox5_Click(object sender, EventArgs e)
{
    if (timer2.Enabled)
    {
        pictureBox5.Show();
    }
    else
    {
        pictureBox5.Hide();
    }
}

And here's the code snippet for timer2

private void timer2_Tick(object sender, EventArgs e)
{
    if (!timer1.Enabled)
    {
        timer2.Enabled = true;
        timer2.Start();
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I can see 2 possible issues with your code. These might not be correct and I'm chancing it a bit. The if statement in `timer2_Tick` should be checking if (!timer2.Enabled) OR Every cycle of the application timer2.Start() is being called causing recursion (the function repeating over and over) causing it to not work Edit: Adding the actual solutions – Callum S Jul 03 '22 at 20:24

1 Answers1

1

To disable Timer1, you must first enable Timer2, then disable Timer1.

    private void Timer1_Tick(object sender, EventArgs e)
    {
        //For example, the gamer has 20 seconds
        //So, we set the value of Label1.text to 20
        if (int.Parse(Label1.Text) > 0)
        {
            int Counter = int.Parse(Label1.Text);
            Counter--;
            Label1.Text = Counter.ToString();
        }
        else
        {
            Timer2.Enabled = true;
            Timer1.Enabled = false;
        }
    }
    private void pictureBox4_Click(object sender, EventArgs e)
    {
        if (Timer2.Enabled == true)
        {
            pictureBox5.Show();
        }
        else
        {
            pictureBox5.Hide();
        }
    }

Tested in:

Visual Studio 2017 .NET Framework 4.5.2 Windows Forms

Thanks

R_J
  • 52
  • 12