【问题标题】:messagebox and timer in visual studioVisual Studio 中的消息框和计时器
【发布时间】:2023-03-03 03:16:01
【问题描述】:

需要帮助。 所以我和我的大学朋友使用 Visual Studio 2017 制作了一个游戏,这个程序有些奇怪。 我们是这样设置的即使我们添加了“timergame.enable = false;”,游戏和游戏的计时器仍在运行。 问题出在哪里?

private void timerGame_Tick(object sender, EventArgs e)
    {
        for (int i = 0; i < listOfSardine.Count; i++)
        {
            listOfSardine[i].Top += (int)listOfSardine[i].Tag;
            if (listOfSardine[i].Bounds.IntersectsWith(pictureBoxGrass.Bounds))
            {
                userLives--;
                labelLives.Text = "Lives: " + userLives;
                listOfSardine[i].Dispose();
                listOfSardine.RemoveAt(i);
                if (userLives == 0)
                {
                    highScore = userScore;
                    timerBonusSpeed.Enabled = false;
                    timerGame.Enabled = false;
                    timerHealth.Enabled = false;
                    timerMatatabi.Enabled = false;
                    timerSardine.Enabled = false;

                    DialogResult dialogResultLose = MessageBox.Show
                        ("Sorry.... you have lost, continue?", "Continue??", MessageBoxButtons.YesNo);
                    if (dialogResultLose == DialogResult.Yes)
                    {
                        for (int j = 0; j < listOfHealth.Count; j++)
                        {
                            listOfHealth[j].Dispose();
                        }
                        listOfHealth.Clear();
                        for (int q = 0; q < listOfSardine.Count; q++)
                        {
                            listOfSardine[q].Dispose();
                        }
                        listOfSardine.Clear();
                        for (int k = 0; k < listOfMatatabi.Count; k++)
                        {
                            listOfMatatabi[k].Dispose();
                        }
                        listOfMatatabi.Clear();

                        userLives = USER_LIVES;
                        userScore = USER_SCORE;

                        timerBonusSpeed.Enabled = true;
                        timerGame.Enabled = true;
                        timerHealth.Enabled = true;
                        timerMatatabi.Enabled = true;
                        timerSardine.Enabled = true;

                    }
                    else
                    {
                        this.Visible = false;
                        FormMainMenu formMainMenu = new FormMainMenu();
                        formMainMenu.Owner = this;
                        formMainMenu.ShowDialog();
                    }
                }
            }
            else if (listOfSardine[i].Bounds.IntersectsWith(pictureBoxMainCharacter.Bounds))
            {
                listOfSardine[i].Dispose();
                listOfSardine.RemoveAt(i);
                userScore += 1;
                labelScore.Text = "Score: " + userScore;
                if (userScore % 100 == 0)
                {
                    listOfSardine[i].Top += (int)listOfSardine[i].Tag * 4;
                }
                if(userScore == 1000)
                {
                    timerBonusSpeed.Enabled = false;
                    timerGame.Enabled = false;
                    timerHealth.Enabled = false;
                    timerMatatabi.Enabled = false;
                    timerSardine.Enabled = false;
                    highScore = userScore;
                }
            }
            else
            {
                listOfSardine[i].Refresh();
            }
        }

        for (int i = 0; i < listOfHealth.Count; i++)
        {
            listOfHealth[i].Top += (int)listOfHealth[i].Tag;
            if (listOfHealth[i].Bounds.IntersectsWith(pictureBoxGrass.Bounds))
            {
                listOfHealth[i].Dispose();
                listOfHealth.RemoveAt(i);
            }
            else if (listOfHealth[i].Bounds.IntersectsWith(pictureBoxMainCharacter.Bounds))
            {
                listOfHealth[i].Dispose();
                listOfHealth.RemoveAt(i);
                userLives++;
                labelLives.Text = "Lives: " + userLives;
            }
            else
            {
                listOfHealth[i].Refresh();
            }

【问题讨论】:

    标签: timer messagebox


    【解决方案1】:

    不够清楚你做了什么。但是,我认为您有一个生命计数器,并且根据您的游戏事件而减少。在计时器事件中,只需检查生命计数器是否等于或小于零。如果是这样,只需停止计时器,让用户确认重试。如果用户想重试,则重置生命计数器并启动计时器。

    【讨论】:

    • 我不太确定。但是根据您的代码,您首先检查生命(生命计数)。并且在获得零之后,您没有使用 return 关键字,因此其余代码执行,并且还增加了生命和继续计时器。在生命周期结束时使用 return 关键字(在 formMainMenu.ShowDialog(); 之后)以防止执行其余代码,或在最后检查生命周期。
    【解决方案2】:

    啊,已经找到答案了 我们的问题是关于表单的所有权,我们只是错误地插入了每个表单的所有权 感谢之前回答我问题的人

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多