Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace ProjectileGame
- {
- public partial class Form1 : Form
- {
- projectile p;
- public Form1()
- {
- InitializeComponent();
- this.p = new projectile();
- }
- public void launchTimer_Tick(object sender, EventArgs e)
- {
- p.projectilepathcalculate();
- bool craterBool = p.checkForCrater();
- if(craterBool == true)
- {
- crater.Location = new Point(p.FinalX, 500);
- crater.Visible = true;
- }
- ball.Location = new Point(p.FinalX, p.FinalY);
- timeSinceLaunchBox.Text = Convert.ToString(p.showTimeElapsed);
- ballXBox.Text = ball.Location.X.ToString();
- ballYBox.Text = ball.Location.Y.ToString();
- }
- private void launch_Click(object sender, EventArgs e)
- {
- launchTimer.Start();
- }
- public void inspeedchanged(object sender, EventArgs e)
- {
- try
- {
- int tempInitialSpeed = Convert.ToInt32(speedTextBox.Text);
- if (tempInitialSpeed < 1)
- {
- MessageBox.Show("input a speed higher than 1");
- }
- else
- {
- p.initialSpeed = tempInitialSpeed;
- speedTextBox.Text = Convert.ToString(tempInitialSpeed);
- }
- }
- catch (Exception)
- {
- if (speedTextBox.Text == "")
- {
- //do nothing if textbox is empty
- }
- else
- {
- MessageBox.Show("invalid input");
- }
- }
- }
- private void anglechanged(object sender, EventArgs e)
- {
- try
- {
- string showAngle = angleTextBox.Text;
- int tempAngle = Convert.ToInt32(angleTextBox.Text);
- if (tempAngle > 1 & tempAngle < 90)
- {
- p.angle = tempAngle;
- angleTextBox.Text = showAngle;
- }
- else
- {
- MessageBox.Show("please input an angle between 1 and 90");
- }
- }
- catch (Exception)
- {
- if (angleTextBox.Text == "")
- {
- //do nothing if textbox is empty
- }
- else
- {
- MessageBox.Show("invalid input");
- }
- }
- }
- private void masschanged(object sender, EventArgs e)
- {
- try
- {
- string showMass = massTextBox.Text;
- int tempMass = Convert.ToInt32(massTextBox.Text);
- if (tempMass >= 1)
- {
- p.mass = tempMass;
- massTextBox.Text = showMass;
- }
- else
- {
- MessageBox.Show("please input a mass above 1");
- }
- }
- catch (Exception)
- {
- if (massTextBox.Text == "")
- {
- //do nothing if textbox is empty
- }
- else
- {
- MessageBox.Show("invalid input");
- }
- }
- }
- private void materialChanged(object sender, EventArgs e)
- {
- p.material = comboBoxMaterial.Text;
- }
- private void resetButtonPressed(object sender, EventArgs e)
- {
- p.startX = 68;
- p.startY = 474;
- p.FinalX = 68;
- p.FinalY = 474;
- p.timeElapsed = 0;
- p.showTimeElapsed = 0;
- p.initialSpeed = Convert.ToInt32(speedTextBox.Text);
- crater.Visible = false;
- timeSinceLaunchBox.Text = "0";
- launchTimer.Enabled = false;
- ball.Location = new Point(p.FinalX, p.FinalY);
- ballYBox.Text = "474";
- ballXBox.Text = "68";
- }
- private void defaultValuePressed(object sender, EventArgs e)
- {
- p.initialSpeed = 80;
- p.timeElapsed = 0;
- p.mass = 10;
- p.angle = 30;
- p.material = "Rubber";
- massTextBox.Text = "10";
- angleTextBox.Text = "30";
- speedTextBox.Text = "80";
- massTextBox.Text = "10";
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- public class projectile
- {
- public int timeElapsed = 0;
- public double g = -9.8;
- public double initialSpeed = 80;
- public double angle = 30;
- public int startX = 68;
- public int startY = 474;
- public int FinalX = 68;
- public int FinalY = 474;
- public double mass = 10;
- public string material = "Rubber";
- public int showTimeElapsed = 0;
- public projectile()
- {
- }
- public void projectilepathcalculate()
- {
- //only do if balls velocity is positive
- if (initialSpeed >= 1)
- {
- timeElapsed = timeElapsed + 1;
- showTimeElapsed++;
- if (FinalY <= 474)
- {
- FinalX = Convert.ToInt32(startX + initialSpeed * timeElapsed * Math.Cos(angle));
- FinalY = Convert.ToInt32(startY + initialSpeed * timeElapsed * Math.Sin(angle) - 0.5 * g * timeElapsed * timeElapsed);
- //store the location of the ball for use in the next bounce
- }
- else if (FinalY > 474 & material == "Rubber")
- {
- //reset timer to treat each bounce as a new launch
- timeElapsed = 1;
- //make the start position the old position for new launch
- startX = FinalX;
- FinalY = 474;
- //put the ball at y=474 so the bounce can start again
- //reduce speed of ball for next bounce
- initialSpeed = 0.9 * initialSpeed - (0.5 * mass);
- }
- }
- }
- public bool checkForCrater()
- {
- if (FinalY > 474 & material == "Metal")
- {
- initialSpeed = 0;
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement