void resetBall() { shootsTaken++; shotsTaken.guiText.text = ("" + (5 - shootsTaken)); PlayerPrefs.SetInt("shots", shootsTaken); score1.guiText.text = (score + "/10"); basketBall = null; gameObject.transform.parent = Camera.main.transform; gameObject.transform.position = Camera.main.transform.TransformPoint(Vector3.forward * 2); }
void Update() { if (Input.GetKeyDown(KeyCode.Space)) { isFiring = true; shootingDirection = GameObject.Find("Main Camera").transform.forward; initialVelocity = shootingDirection * (power); //sets strengths basketBall = new DragProjectile(gameObject.transform.position.x, gameObject.transform.position.z, gameObject.transform.position.y, initialVelocity.x, initialVelocity.z, initialVelocity.y, 0.0, mass, area, density, cd); //creates projectile gameObject.transform.position = new Vector3((float)basketBall.GetX(), (float)basketBall.GetZ(), (float)basketBall.GetY()); } if (isFiring) { time += Time.deltaTime / 100; //slows down time basketBall.UpdateLocationAndVelocity(time); //updates position and speed if (basketBall.GetZ() >= -1) { gameObject.transform.position = new Vector3((float)basketBall.GetX(), (float)basketBall.GetZ(), (float)basketBall.GetY()); } else { if (distance(ball.transform.position, target.transform.position) < 1) //hits { audio.PlayOneShot(cheer); //sound score += 2; //increases } isFiring = false; //false time = 0; //sets resetBall(); //reset ball } } if (shootsTaken == 5) //load next level { previousLevel++; PlayerPrefs.SetInt("currentScore", currentScore + score); PlayerPrefs.SetInt("previousLevel", previousLevel); PlayerPrefs.Save(); Application.LoadLevel("briefing"); } }
// Event handling method for the "Fire" button public void FireButtonClicked(object source, EventArgs e) { // Set the shot made flag to false. shotMade = false; // Extract input values from textfields double velocity = Convert.ToDouble(velocityTextBox.Text); double angle = Convert.ToDouble(angleTextBox.Text); // Convert the angle to radians. angle = angle * Math.PI / 180.0; // Compute initial velocities double vx0 = velocity * Math.Cos(angle); double vy0 = 0.0; double vz0 = velocity * Math.Sin(angle); // The ball starts at a spot 18 meters from and directly // in front of the goal. double x0 = 1.0; double y0 = 0.0; double z0 = 2.25; // Define some basketball variables. The Cd value will be // assumed to be constant. double ballMass = 0.62; double radius = 0.12; double density = 1.2; double area = Math.PI * radius * radius; double cd = 0.5; // Create a DragProjectile object representing the basketball. basketball = new DragProjectile(x0, y0, z0, vx0, vy0, vz0, 0.0, ballMass, area, density, cd); // Update the display UpdateDisplay(); // Fire the golf ball using a Timer object // to slow down the action. gameTimer.Start(); }
/* * Initialize the starting variables again after setting with UI inputs; */ void initialize() { if (init) { trailSwitch = true; vx0 = tempVx0; vy0 = tempVy0; vz0 = tempVz0; mass = 20.0f; area = .2f; cd = .4f; density = 1.2f; golfBall = new DragProjectile(gameObject.transform.position.x, gameObject.transform.position.z, gameObject.transform.position.y, vx0, vy0, vz0, 0.0, mass, area, density, cd, windVx, windVy); gameObject.transform.position = new Vector3((float)golfBall.GetX(), (float)golfBall.GetZ(), (float)golfBall.GetY()); init = false; } }
void Start() { init = true; trailSwitch = false; trajectoryPoints = new List <GameObject>(); vx0 = 0f; vy0 = 0; vz0 = 0f; mass = 0; area = .2f; cd = .4f; density = 1.2f; windVx = 0; windVy = 0; golfBall = new DragProjectile(gameObject.transform.position.x, gameObject.transform.position.z, gameObject.transform.position.y, vx0, vy0, vz0, 0.0, mass, area, density, cd, windVx, windVy); gameObject.transform.position = new Vector3((float)golfBall.GetX(), (float)golfBall.GetZ(), (float)golfBall.GetZ()); }
// Event handling method for the "Fire" button public void FireButtonClicked(object source, EventArgs e) { // Get the initial values from the textfield double vx0 = Convert.ToDouble(vxTextBox.Text); double vy0 = Convert.ToDouble(vyTextBox.Text); double vz0 = Convert.ToDouble(vzTextBox.Text); distanceToHole = Convert.ToDouble(distanceTextBox.Text); double mass = Convert.ToDouble(massTextBox.Text); double area = Convert.ToDouble(areaTextBox.Text); double cd = Convert.ToDouble(cdTextBox.Text); double density = Convert.ToDouble(densityTextBox.Text); // Create a DragProjectile object representing the golf ball. golfball = new DragProjectile(0.0, 0.0, 0.0, vx0, vy0, vz0, 0.0, mass, area, density, cd); // Update the display UpdateDisplay(); // Fire the golf ball using a Timer object // to slow down the action. gameTimer.Start(); }
public GolfGame2() { // Create a DragProjectile object with default values // so the display can be updated the first time. golfball = new DragProjectile(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0459, 0.001432, 0.5, 1.225); // Set up images golferIcon = Image.FromFile("Golfer.jpg"); flagIcon = Image.FromFile("Hole_Cartoon.jpg"); // Initialize the distanceToHole field. distanceToHole = 200.0; // Create a Timer object that will be used // to slow the action down. gameTimer = new Timer(); gameTimer.Interval = 50; // delay in milliseconds. gameTimer.Tick += new EventHandler(ActionPerformed); // Create some Labels vxLabel = new Label(); vxLabel.Text = "Initial x-velocity, m/s"; vxLabel.Font = new Font(vxLabel.Font, FontStyle.Bold); vxLabel.Top = 50; vxLabel.Left = 10; vxLabel.Width = 130; vyLabel = new Label(); vyLabel.Text = "Initial y-velocity, m/s"; vyLabel.Font = new Font(vyLabel.Font, FontStyle.Bold); vyLabel.Top = 80; vyLabel.Left = 10; vyLabel.Width = 130; vzLabel = new Label(); vzLabel.Text = "Initial z-velocity, m/s"; vzLabel.Font = new Font(vzLabel.Font, FontStyle.Bold); vzLabel.Top = 110; vzLabel.Left = 10; vzLabel.Width = 130; distanceLabel = new Label(); distanceLabel.Text = "Distance to hole, m"; distanceLabel.Font = new Font(distanceLabel.Font, FontStyle.Bold); distanceLabel.Top = 140; distanceLabel.Left = 10; distanceLabel.Width = 120; massLabel = new Label(); massLabel.Text = "mass, kg"; massLabel.Font = new Font(massLabel.Font, FontStyle.Bold); massLabel.Top = 50; massLabel.Left = 210; massLabel.Width = 110; areaLabel = new Label(); areaLabel.Text = "area, m^2"; areaLabel.Font = new Font(areaLabel.Font, FontStyle.Bold); areaLabel.Top = 80; areaLabel.Left = 210; areaLabel.Width = 110; cdLabel = new Label(); cdLabel.Text = "drag coefficient"; cdLabel.Font = new Font(cdLabel.Font, FontStyle.Bold); cdLabel.Top = 110; cdLabel.Left = 210; cdLabel.Width = 110; densityLabel = new Label(); densityLabel.Text = "density, kg/m^3"; densityLabel.Font = new Font(densityLabel.Font, FontStyle.Bold); densityLabel.Top = 140; densityLabel.Left = 210; densityLabel.Width = 110; axesLabel = new Label(); axesLabel.Text = "View axes"; axesLabel.Font = new Font(axesLabel.Font, FontStyle.Bold); axesLabel.Top = 170; axesLabel.Left = 10; axesLabel.Width = 70; // Create TextBox objects to display the inputs. vxTextBox = new TextBox(); vxTextBox.Width = 50; vxTextBox.Text = "31.0"; vxTextBox.AutoSize = true; vxTextBox.Top = vxLabel.Top; vxTextBox.Left = 150; vyTextBox = new TextBox(); vyTextBox.Width = 50; vyTextBox.Text = "0.0"; vyTextBox.AutoSize = true; vyTextBox.Top = vyLabel.Top; vyTextBox.Left = 150; vzTextBox = new TextBox(); vzTextBox.Width = 50; vzTextBox.Text = "35.0"; vzTextBox.AutoSize = true; vzTextBox.Top = vzLabel.Top; vzTextBox.Left = 150; distanceTextBox = new TextBox(); distanceTextBox.Width = 50; distanceTextBox.Text = "200.0"; distanceTextBox.AutoSize = true; distanceTextBox.Top = distanceLabel.Top; distanceTextBox.Left = 150; massTextBox = new TextBox(); massTextBox.Width = 60; massTextBox.Text = "0.0459"; massTextBox.AutoSize = true; massTextBox.Top = massLabel.Top; massTextBox.Left = 330; areaTextBox = new TextBox(); areaTextBox.Width = 60; areaTextBox.Text = "0.001432"; areaTextBox.AutoSize = true; areaTextBox.Top = areaLabel.Top; areaTextBox.Left = 330; cdTextBox = new TextBox(); cdTextBox.Width = 60; cdTextBox.Text = "0.25"; cdTextBox.AutoSize = true; cdTextBox.Top = cdLabel.Top; cdTextBox.Left = 330; densityTextBox = new TextBox(); densityTextBox.Width = 60; densityTextBox.Text = "1.225"; densityTextBox.AutoSize = true; densityTextBox.Top = densityLabel.Top; densityTextBox.Left = 330; // Create a ComboBox to select the view axes. axesComboBox = new ComboBox(); axesComboBox.Items.Add("XZ"); axesComboBox.Items.Add("XY"); axesComboBox.SelectedIndex = 0; axesComboBox.Left = 80; axesComboBox.Top = axesLabel.Top; // Create Button objects int buttonHeight = 30; int buttonWidth = 50; int buttonLeft = 20; fireButton = new Button(); fireButton.Text = "Fire"; fireButton.Height = buttonHeight; fireButton.Width = buttonWidth; fireButton.Top = 200; fireButton.Left = buttonLeft; fireButton.Click += new EventHandler(FireButtonClicked); resetButton = new Button(); resetButton.Text = "Reset"; resetButton.Height = buttonHeight; resetButton.Width = buttonWidth; resetButton.Top = 250; resetButton.Left = buttonLeft; resetButton.Click += new EventHandler(ResetButtonClicked); // Create a drawing panel. drawingPanel = new Panel(); drawingPanel.Width = 501; drawingPanel.Height = 201; drawingPanel.Left = 20; drawingPanel.Top = 300; drawingPanel.BorderStyle = BorderStyle.FixedSingle; // Add the GUI components to the Form this.Controls.Add(vxLabel); this.Controls.Add(vyLabel); this.Controls.Add(vzLabel); this.Controls.Add(distanceLabel); this.Controls.Add(axesLabel); this.Controls.Add(massLabel); this.Controls.Add(areaLabel); this.Controls.Add(cdLabel); this.Controls.Add(densityLabel); this.Controls.Add(vxTextBox); this.Controls.Add(vyTextBox); this.Controls.Add(vzTextBox); this.Controls.Add(distanceTextBox); this.Controls.Add(massTextBox); this.Controls.Add(areaTextBox); this.Controls.Add(cdTextBox); this.Controls.Add(densityTextBox); this.Controls.Add(axesComboBox); this.Controls.Add(fireButton); this.Controls.Add(resetButton); this.Controls.Add(drawingPanel); // Set the size and title of the form this.Width = 600; this.Height = 550; this.Text = "Golf Game version 2"; // Center the form on the screen and make // it visible. this.StartPosition = FormStartPosition.CenterScreen; this.Visible = true; // Update the GUI display UpdateDisplay(); }
public FreeThrow() { // Create a DragProjectile object to represent the soccer ball. basketball = new DragProjectile(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.62, 0.0452, 1.2, 0.5); basketball.SetQ(1.0, 1); // x0 basketball.SetQ(2.25, 5); // z0 // The shot is missed until it is made shotMade = false; // Set up images playerIcon = Image.FromFile("basketball_player.gif"); playerWidth = playerIcon.Width; playerHeight = playerIcon.Height; ballIcon = Image.FromFile("Basketball.jpg"); // Create a Timer object that will be used // to slow the action down. gameTimer = new Timer(); gameTimer.Interval = 20; // delay in milliseconds. gameTimer.Tick += new EventHandler(ActionPerformed); // Create some Labels velocityLabel = new Label(); velocityLabel.Text = "Initial velocity (m/s)"; velocityLabel.Font = new Font(velocityLabel.Font, FontStyle.Bold); velocityLabel.Top = 20; velocityLabel.Left = 10; velocityLabel.Width = 120; angleLabel = new Label(); angleLabel.Text = "Shot angle (deg)"; angleLabel.Font = new Font(angleLabel.Font, FontStyle.Bold); angleLabel.Top = 50; angleLabel.Left = 10; angleLabel.Width = 120; // Create TextBox objects to display the inputs. velocityTextBox = new TextBox(); velocityTextBox.Width = 50; velocityTextBox.Text = "7.5"; velocityTextBox.AutoSize = true; velocityTextBox.Top = velocityLabel.Top; velocityTextBox.Left = 140; angleTextBox = new TextBox(); angleTextBox.Width = 50; angleTextBox.Text = "40.0"; angleTextBox.AutoSize = true; angleTextBox.Top = angleLabel.Top; angleTextBox.Left = 140; // Create Button objects int buttonHeight = 30; int buttonWidth = 50; int buttonLeft = 20; fireButton = new Button(); fireButton.Text = "Fire"; fireButton.Height = buttonHeight; fireButton.Width = buttonWidth; fireButton.Top = 80; fireButton.Left = buttonLeft; fireButton.Click += new EventHandler(FireButtonClicked); resetButton = new Button(); resetButton.Text = "Reset"; resetButton.Height = buttonHeight; resetButton.Width = buttonWidth; resetButton.Top = 130; resetButton.Left = buttonLeft; resetButton.Click += new EventHandler(ResetButtonClicked); // Create a drawing panel. drawingPanel = new Panel(); drawingPanel.Width = 301; drawingPanel.Height = 251; drawingPanel.Left = 300; drawingPanel.Top = 20; drawingPanel.BorderStyle = BorderStyle.FixedSingle; // Add the GUI components to the Form this.Controls.Add(velocityLabel); this.Controls.Add(angleLabel); this.Controls.Add(velocityTextBox); this.Controls.Add(angleTextBox); this.Controls.Add(fireButton); this.Controls.Add(resetButton); this.Controls.Add(drawingPanel); // Set the size and title of the form this.Width = 650; this.Height = 400; this.Text = "Free Throw"; // Center the form on the screen and make // it visible. this.StartPosition = FormStartPosition.CenterScreen; this.Visible = true; // Update the GUI display UpdateDisplay(); }