-1

I am a beginner in C# and creating a small game in where the user gets to throw a ball with a certain gravity, speed and random wind. The ball is supposed to hit a goal, give the user points (when it hits) and then go back to start position for user to throw it again. I have got everything working except the matter of getting the ball back into start position. Any help would be greatly appreciated!

Here is some parts of my code:

   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));
        }

    }
Ellinor
  • 323
  • 3
  • 14
  • What do you mean "restart" while loop? – AustinWBryan Jan 05 '16 at 18:00
  • 1
    What constitutes "getting the ball back into start position"? Where do you ever set the ball's "start position"? Can't you just perform that same logic again? – David Jan 05 '16 at 18:01
  • 1
    You should create a `Reset` method and re initialize every necessary thing in that method. Just call it when your game ends. – Shaharyar Jan 05 '16 at 18:01
  • Please check out [MCVE] for guidance on providing code in the posts. – Alexei Levenkov Jan 05 '16 at 18:05
  • I have tried to set the balls position (b1.Location = new Point(0, 300);) put what it does is simply go back to where it bounced rather than stay in the start position. Thats when I thought I needed to do a restart of the whole loop instead. – Ellinor Jan 05 '16 at 18:07
  • 1
    Why do you pass a RunMe argument and then ignore the parameter? You are showing a MessageBox from that background thread, not good. Since you are a beginner and this is WinForms, you are probably better off using a timer instead. – LarsTech Jan 05 '16 at 18:07

1 Answers1

2

You can use:

continue;

to skip to next repetition of a loop (where you write goto restart;).

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181