Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace ProjectileGame
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         projectile p;
  16.  
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.             this.p = new projectile();
  21.         }
  22.  
  23.         public void launchTimer_Tick(object sender, EventArgs e)
  24.         {
  25.             p.projectilepathcalculate();
  26.             bool craterBool = p.checkForCrater();
  27.             if(craterBool == true)
  28.             {
  29.                 crater.Location = new Point(p.FinalX, 500);
  30.                 crater.Visible = true;
  31.             }
  32.             ball.Location = new Point(p.FinalX, p.FinalY);
  33.             timeSinceLaunchBox.Text = Convert.ToString(p.showTimeElapsed);
  34.             ballXBox.Text = ball.Location.X.ToString();
  35.             ballYBox.Text = ball.Location.Y.ToString();
  36.         }
  37.  
  38.         private void launch_Click(object sender, EventArgs e)
  39.         {
  40.             launchTimer.Start();
  41.         }
  42.  
  43.         public void inspeedchanged(object sender, EventArgs e)
  44.         {
  45.             try
  46.             {
  47.                 int tempInitialSpeed = Convert.ToInt32(speedTextBox.Text);
  48.                 if (tempInitialSpeed < 1)
  49.                 {
  50.                     MessageBox.Show("input a speed higher than 1");
  51.                 }
  52.                 else
  53.                 {
  54.                     p.initialSpeed = tempInitialSpeed;
  55.                     speedTextBox.Text = Convert.ToString(tempInitialSpeed);
  56.                 }
  57.             }
  58.             catch (Exception)
  59.             {
  60.                 if (speedTextBox.Text == "")
  61.                 {
  62.                     //do nothing if textbox is empty
  63.                 }
  64.                 else
  65.                 {
  66.                     MessageBox.Show("invalid input");
  67.                 }
  68.             }
  69.         }
  70.  
  71.         private void anglechanged(object sender, EventArgs e)
  72.         {
  73.             try
  74.             {
  75.                 string showAngle = angleTextBox.Text;
  76.                 int tempAngle = Convert.ToInt32(angleTextBox.Text);
  77.                 if (tempAngle > 1 & tempAngle < 90)
  78.                 {
  79.                     p.angle = tempAngle;
  80.                     angleTextBox.Text = showAngle;
  81.                 }
  82.                 else
  83.                 {
  84.                     MessageBox.Show("please input an angle between 1 and 90");
  85.                 }
  86.             }
  87.             catch (Exception)
  88.             {
  89.                 if (angleTextBox.Text == "")
  90.                 {
  91.                     //do nothing if textbox is empty
  92.                 }
  93.                 else
  94.                 {
  95.                     MessageBox.Show("invalid input");
  96.                 }
  97.             }
  98.         }
  99.  
  100.         private void masschanged(object sender, EventArgs e)
  101.         {
  102.             try
  103.             {
  104.                 string showMass = massTextBox.Text;
  105.                 int tempMass = Convert.ToInt32(massTextBox.Text);
  106.                 if (tempMass >= 1)
  107.                 {
  108.                     p.mass = tempMass;
  109.                     massTextBox.Text = showMass;
  110.                 }
  111.                 else
  112.                 {
  113.                     MessageBox.Show("please input a mass above 1");
  114.                 }
  115.             }
  116.             catch (Exception)
  117.             {
  118.                 if (massTextBox.Text == "")
  119.                 {
  120.                     //do nothing if textbox is empty
  121.                 }
  122.                 else
  123.                 {
  124.                     MessageBox.Show("invalid input");
  125.                 }
  126.             }
  127.         }
  128.  
  129.         private void materialChanged(object sender, EventArgs e)
  130.         {
  131.             p.material = comboBoxMaterial.Text;
  132.         }
  133.  
  134.         private void resetButtonPressed(object sender, EventArgs e)
  135.         {
  136.             p.startX = 68;
  137.             p.startY = 474;
  138.             p.FinalX = 68;
  139.             p.FinalY = 474;
  140.             p.timeElapsed = 0;
  141.             p.showTimeElapsed = 0;
  142.             p.initialSpeed = Convert.ToInt32(speedTextBox.Text);
  143.             crater.Visible = false;
  144.             timeSinceLaunchBox.Text = "0";
  145.             launchTimer.Enabled = false;
  146.             ball.Location = new Point(p.FinalX, p.FinalY);
  147.             ballYBox.Text = "474";
  148.             ballXBox.Text = "68";
  149.         }
  150.  
  151.         private void defaultValuePressed(object sender, EventArgs e)
  152.         {
  153.             p.initialSpeed = 80;
  154.             p.timeElapsed = 0;
  155.             p.mass = 10;
  156.             p.angle = 30;
  157.             p.material = "Rubber";
  158.             massTextBox.Text = "10";
  159.             angleTextBox.Text = "30";
  160.             speedTextBox.Text = "80";
  161.             massTextBox.Text = "10";
  162.         }
  163.  
  164.         private void Form1_Load(object sender, EventArgs e)
  165.         {
  166.  
  167.         }
  168.     }
  169.  
  170.     public class projectile
  171.     {
  172.         public int timeElapsed = 0;
  173.         public double g = -9.8;
  174.         public double initialSpeed = 80;
  175.         public double angle = 30;
  176.         public int startX = 68;
  177.         public int startY = 474;
  178.         public int FinalX = 68;
  179.         public int FinalY = 474;
  180.         public double mass = 10;
  181.         public string material = "Rubber";
  182.         public int showTimeElapsed = 0;
  183.  
  184.         public projectile()
  185.         {
  186.  
  187.         }
  188.  
  189.         public void projectilepathcalculate()
  190.         {
  191.             //only do if balls velocity is positive
  192.             if (initialSpeed >= 1)
  193.             {
  194.                 timeElapsed = timeElapsed + 1;
  195.                 showTimeElapsed++;
  196.                 if (FinalY <= 474)
  197.                 {
  198.                     FinalX = Convert.ToInt32(startX + initialSpeed * timeElapsed * Math.Cos(angle));
  199.                     FinalY = Convert.ToInt32(startY + initialSpeed * timeElapsed * Math.Sin(angle) - 0.5 * g * timeElapsed * timeElapsed);
  200.                     //store the location of the ball for use in the next bounce
  201.                 }
  202.                 else if (FinalY > 474 & material == "Rubber")
  203.                 {
  204.                     //reset timer to treat each bounce as a new launch
  205.                     timeElapsed = 1;
  206.                     //make the start position the old position for new launch
  207.                     startX = FinalX;
  208.                     FinalY = 474;
  209.                     //put the ball at y=474 so the bounce can start again
  210.                     //reduce speed of ball for next bounce
  211.                     initialSpeed = 0.9 * initialSpeed - (0.5 * mass);
  212.                 }
  213.  
  214.             }
  215.         }
  216.         public bool checkForCrater()
  217.         {
  218.             if (FinalY > 474 & material == "Metal")
  219.             {
  220.                 initialSpeed = 0;
  221.                 return true;
  222.             }
  223.             else
  224.             {
  225.                 return false;
  226.             }
  227.         }
  228.  
  229.     }
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement