C# 在C中重新启动while循环#

C# 在C中重新启动while循环#,c#,while-loop,restart,C#,While Loop,Restart,我是C#的初学者,正在创建一个小游戏,在这个游戏中,用户可以以一定的重力、速度和随机风投球。球应该击中一个目标,给用户分数(当它击中时),然后回到起始位置让用户再次投掷。除了把球放回起始位置的问题,我已经把一切都安排好了。任何帮助都将不胜感激 以下是我的代码的一些部分: private void goButton_Click(object sender, EventArgs e) { if (running == true) {

我是C#的初学者,正在创建一个小游戏,在这个游戏中,用户可以以一定的重力、速度和随机风投球。球应该击中一个目标,给用户分数(当它击中时),然后回到起始位置让用户再次投掷。除了把球放回起始位置的问题,我已经把一切都安排好了。任何帮助都将不胜感激

以下是我的代码的一些部分:

   private void goButton_Click(object sender, EventArgs e) 
    {
        if (running == true) 
        {
            b1.Location = new Point(0, 300); 
            b1.speedX = (double)upDownX.Value; 
            b1.speedY = (double)upDownY.Value; 
            running = false; 
            b1.Start();
            return;
        }
        running = true;

        worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(RunMe);             worker.RunWorkerAsync(); 
    }

public void RunMe(object sender, DoWorkEventArgs e)
    {
        while (running)
        {

            if (b1.speedY > 0 && b1.Location.Y > panel.Size.Height - b1.Size.Height)
            {
                b1.posY = panel.Size.Height - b1.Size.Height;

                addPoints();

                running = false;

                BeginInvoke((MethodInvoker)delegate
                {
                    b1.Location = new Point(0, 300);
                });

            }
            if (b1.speedX > 0 && b1.Location.X > panel.Size.Width - b1.Size.Width)
            {

                running = false;
                if (b1.Location.Y < 290 && b1.Location.Y > 150 && b1.Location.X > 430)
                {
                    if (diffValue.Text == "Normal")
                    {
                        Ball.score += 10;
                    }

                    if (diffValue.Text == "Easy")
                    {
                        Ball.score += 7;
                    }

                }

                else if (b1.Location.Y < 390 && b1.Location.Y > 60 && b1.Location.X > 430)
                {
                    if (diffValue.Text == "Normal")
                    {
                        Ball.score += 5;
                    }

                    if (diffValue.Text == "Easy")
                    {
                        Ball.score += 3;
                    }
                }

                addPoints();

                b1.BounceX();
                goto restart;
            }
            if (b1.speedX < 0 && b1.Location.X < 0)
            {
                b1.BounceX();
            }


            this.Invoke(new MoveBallCallback(MoveBall), b1);
            Thread.Sleep(10);
        }

    }



public void addPoints()
    {

        if (Ball.tries >= 1)
        {

            Ball.tries -= 1;
            string triesLeft = Ball.tries.ToString();
            this.Invoke(new Action(() => this.shotsLeft.Text = triesLeft));
            string score = Ball.score.ToString();
            this.Invoke(new Action(() => this.scores.Text = score));

            Normal();

            BeginInvoke((MethodInvoker)delegate
            {
                b1.Location = new Point(0, 300);
            });


        }

        else 
        {
            MessageBox.Show("No shots left!");
            string currentHighscore = System.IO.File.ReadAllText(@"highscore.txt");
            this.highscoreValue.Text = currentHighscore;
            int Highscore = Convert.ToInt32(currentHighscore);
            string score = Ball.score.ToString();
            this.Invoke(new Action(() => this.scores.Text = score));
            int Score = Convert.ToInt32(Ball.score);
            if (Score > Highscore)
            {
                MessageBox.Show("New highscore!");
                string highScore = score;
                System.IO.File.WriteAllText(@"highscore.txt", highScore);
                string highscore = Highscore.ToString();
                this.Invoke(new Action(() => this.highscoreValue.Text = highscore));
            }

            this.Invoke(new Action(() => goButton.Enabled = false));
        }

    }
private void goButton_单击(对象发送方,事件参数e)
{
if(running==true)
{
b1.位置=新点(0,300);
b1.speedX=(双)upDownX.Value;
b1.speedY=(双)upDownY.Value;
运行=错误;
b1.开始();
返回;
}
运行=真;
worker=新的BackgroundWorker();
worker.DoWork+=新的doworkerEventHandler(RunMe);worker.RunWorkerAsync();
}
公共无效运行库(对象发送方,DoWorkEventArgs e)
{
(跑步时)
{
如果(b1.0>0&&b1.Location.Y>panel.Size.Height-b1.Size.Height)
{
b1.posY=面板尺寸高度-b1.Size.Height;
addPoints();
运行=错误;
BeginInvoke((MethodInvoker)委托
{
b1.位置=新点(0,300);
});
}
如果(b1.speedX>0&&b1.Location.X>panel.Size.Width-b1.Size.Width)
{
运行=错误;
如果(b1.Location.Y<290和b1.Location.Y>150和b1.Location.X>430)
{
如果(diffValue.Text==“正常”)
{
得分+=10分;
}
if(diffValue.Text==“轻松”)
{
得分+=7分;
}
}
否则如果(b1.Location.Y<390和b1.Location.Y>60和b1.Location.X>430)
{
如果(diffValue.Text==“正常”)
{
得分+=5分;
}
if(diffValue.Text==“轻松”)
{
得分+=3分;
}
}
addPoints();
b1.BounceX();
转到重新启动;
}
if(b1.speedX<0&&b1.Location.X<0)
{
b1.BounceX();
}
调用(新的MoveBallCallback(MoveBall),b1);
睡眠(10);
}
}
公共void addPoints()
{
如果(Ball.trys>=1)
{
Ball.trys-=1;
string triesLeft=Ball.tries.ToString();
this.Invoke(新操作(()=>this.shotsLeft.Text=triesLeft));
字符串分数=Ball.score.ToString();
this.Invoke(新操作(()=>this.scores.Text=score));
正常();
BeginInvoke((MethodInvoker)委托
{
b1.位置=新点(0,300);
});
}
其他的
{
MessageBox.Show(“没有镜头了!”);
字符串currentHighscore=System.IO.File.ReadAllText(@“highscore.txt”);
this.highscoreValue.Text=currentHighscore;
int Highscore=转换为32(当前Highscore);
字符串分数=Ball.score.ToString();
this.Invoke(新操作(()=>this.scores.Text=score));
int分数=转换为32(球分数);
如果(分数>高分)
{
MessageBox.Show(“新高分!”);
字符串highScore=分数;
System.IO.File.writealText(@“highscore.txt”,highscore);
字符串highscore=highscore.ToString();
this.Invoke(新操作(()=>this.highscoreValue.Text=highscore));
}
this.Invoke(新操作(()=>goButton.Enabled=false));
}
}
您可以使用:

continue;

跳到下一个循环的重复(在这里你写
goto restart;
)。

当循环时“重新启动”是什么意思?什么构成“让球回到起始位置”?你在哪里设置过球的“起始位置”?你不能再次执行同样的逻辑吗?你应该创建一个
Reset
方法,并重新初始化该方法中所有必要的东西。游戏结束后就调用它。请查看有关在帖子中提供代码的指导。为什么要传递RunMe参数,然后忽略该参数?您正在显示来自该后台线程的MessageBox,这不太好。由于您是一名初学者,而且这是WinForms,因此最好使用计时器。