Skip to content

Car Timer

Darren Duffy

Asked Jan 12, 2015, 1:39 PM EST3 answers
Share
LinkedInX
Trying to find away to get my car image to slow down from an initial fast speed before stopping using this timer. Can someone offer some help please I don't no how to link my image to these calculations 


private int speedCounter = 100; //Speed for this many intervals
  private void timer1_Tick(object sender, EventArgs e)
  {
 

           if (speedCounter > 0)
                {
                  --speedCounter;
           if (timer1.Interval < 600)
                {
                  timer1.Interval++;
                }
         else
               timer1.Enabled = false;
                }
   }

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hello,


    If your desire is to move an image using the PictureBox then all you need to do is:


    //At the end of the if statement add

    pictureBox1.Location = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y);

    //This will increase the  X  position of the pictureBox by 1 each time the timer tick.
    //It won't look so natural as you might desire but it's what I believe you ask for
    0
  • 0
    Thanks for the reply. I 'll show what I'm working with with maybe that will make it a bit clearer. I think it may be my delaycount at the start which is causing the problem, what do you think?


    C#
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace Move
    {
        public partial class Move : Form
        {
            public Move()
            {
                InitializeComponent();
            }
            static void Main()
            {
                Application.Run(new Move());
            }
    
            double carBrake;
            double roadCon;
            double combWeight;
            double thinkDis;
            double thinkTime;
            double vehSpeed;
            double brakDis;
            double brakeTime;
            double stopTime;
            double stopDist;
            int position;
            double vehSpeedkmh;
    
            private void delayloop(int delayCount) // simple nested loop to delay processing
            //method called with paramater passed
            {
                for (int i = 0; i < delayCount; i++) // outer loop controlled by delayCount
                {
                    for (int j = 0; j < 600; j++) ; // empty loop to act as delay

                } //loop i

     

            }//delay

     

    C#
            private void ThinkingTime_SelectedIndexChanged(object sender, EventArgs e)
            {
                switch (ThinkingTime.SelectedIndex)
                {
                    case 0:
                        thinkTime = (float) 0.25;
                        break;
    
                    case 1:
                        thinkTime = (float) 0.7;
                        break;
    
                    case 2:
                        thinkTime = (float) 1.25;
                        break;

                }// switch (ThinkingTime.SelectedIndex)

            }// ThinkingTime_SelectedIndexChanged

     

     

    C#
            private void cbImage_SelectedIndexChanged_1(object sender, EventArgs e)
            {
                switch (cbImage.SelectedIndex)
                {
                    case 0:
    
                        pbCar.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Racing.jpg"));
                        carBrake = (float)2;
                        break;
                    case 1:
                        pbCar.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Rally.jpg"));
                        carBrake = (float)1.6;
                        break;
                    case 2:
                        pbCar.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Performance.jpg"));
                        carBrake = (float)1.3;
                        break;
                    case 3:
                        pbCar.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Normal.jpg"));
                        carBrake = (float)1;
                        break;
                    case 4:
                        pbCar.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Worn Brakes.jpg"));
                        carBrake = (float)0.7;
                        break;
                    case 5:
                        pbCar.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Poor tyres and brakes.jpg"));
                        carBrake = (float)0.4;
                        break;

     

                } //switch (cbImage.SelectedIndex)

            } // cbImage_SelectedIndexChanged_1

     

    C#
            private void cbRoadCondition_SelectedIndexChanged(object sender, EventArgs e)
            {
                switch (cbRoadCondition.SelectedIndex)
                {
    
                    case 0:
                        pbWeather.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Dry Road.jpg"));
                        roadCon = (float) 0.75;
                        break;
    
                    case 1:
                        pbWeather.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Dry Road.jpg"));
                        roadCon = (float) 0.6;
                        break;
    
                    case 2:
                        pbWeather.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Damp Road.jpg"));
                        roadCon = (float) 0.6;
                        break;
    
                    case 3:
                        pbWeather.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Damp Road.jpg"));
                        roadCon = (float) 0.45;
                        break;
    
                    case 4:
                        pbWeather.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Wet Road.jpg"));
                        roadCon = (float) 0.4;
                        break;
    
                    case 5:
                        pbWeather.Image = Image.FromFile(Path.Combine(Application.StartupPath, "Wet Road.jpg"));
                        roadCon = (float) 0.26;
                        break;

     

                }// switch (cbRoadCondition.SelectedIndex)

            }// cbRoadCondition_SelectedIndexChanged

          

    TypeScript
            private void btnReset_Click_1(object sender, EventArgs e)
            {
                pbCar.Left = 0; // Reset Image back to start
            }
    
            private void sbSpeed_Scroll(object sender, ScrollEventArgs e)
            {
                lblSpeed.Text = "Speed in Km/h = " + sbSpeed.Value.ToString();

            } // sbSpeed_Scroll

            

    C#
            private void btnMove_Click_1(object sender, EventArgs e)
            {
                // set delay from slider then move picture with delay time
                int delay = 4 * sbSpeed.Value; // Proportional delay
                for (position = 0; position < 600; position += 2)
                {
    
                    delayloop(delay);
                    pbCar.Left = position;

                }// For Move

     

    Code
                // Calculations
                combWeight = carBrake * roadCon;
                thinkDis = vehSpeedkmh * thinkTime;
                vehSpeed = vehSpeedkmh * 1000 / 3600;
                brakDis = (vehSpeedkmh * vehSpeedkmh) / (2 * combWeight * 9.81);
                brakeTime = 2 * brakDis / vehSpeedkmh;
                stopTime = thinkTime + brakeTime;
                stopDist = brakDis + thinkDis;
                delay = sbSpeed.Maximum - sbSpeed.Value;
    
                // Output Results
                Results.Text = "Speed (Km/h) = " + vehSpeedkmh.ToString() + "\r\n" +

                "Thinking Time (s) = " + thinkTime.ToString("f2") + "\r\n" +

                "Road Condition = " + roadCon.ToString("f2") + "\r\n" +

                "Vehicle Speed (m/s) = " + vehSpeed.ToString("f2") + "\r\n" +

                "Braking Distance (m) = " + brakDis.ToString("f2") + "\r\n" +

                "Braking Time (s) = " + brakeTime.ToString("f2") + "\r\n" +

                "Stopping Time (s) = " + stopTime.ToString("f2") + "\r\n" +

    C#
                "Stopping Distance (m) = " + stopDist.ToString("f2");
    
            }
    
            private void sbSpeed_Scroll_1(object sender, ScrollEventArgs e)
            {
                vehSpeedkmh = sbSpeed.Value;
            }
    
            private int speedCounter = 100; //Speed for this many intervals
            private void timer1_Tick(object sender, EventArgs e)
            {
    
                if (speedCounter > 0)
                {
                    --speedCounter;
                    if (timer1.Interval < 200)
                    {
                        timer1.Interval++;
                        pbCar.Location = new Point(pbCar.Location.X + 1, pbCar.Location.Y);
                        // Increase the  X  position of the pictureBox by 1 each time the timer tick.
                    }
                    else
                        timer1.Enabled = false;
                }
            }
            private void Move_Load(object sender, EventArgs e)
            {
                cbImage.SelectedIndex = 1;
                ThinkingTime.SelectedIndex = 1;
                cbRoadCondition.SelectedIndex = 1;
                sbSpeed.Value = 40;
            }
        }
    }
    0
  • 0
    It would be better if you uploaded the source as a file.
    -As then i would be able to test and see the problem myself
    0