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