基本信息
源码名称:C# 控制小车 移动 示例源码
源码大小:0.08M
文件格式:.rar
开发语言:C#
更新时间:2018-05-16
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 3 元 
   源码介绍

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;


namespace MoveCar
{
    public partial class Form1 : Form
    {
        Graphics gra;
        Car car = new Car(0,25);
        Pen bluePen;
        Point p11 = new Point(898,85);
        Point p12 = new Point(969,85);
        Boolean run = false;
        int flag = 0;
        public Form1()
        {
            InitializeComponent();
            label3.Text = "红点为自动驾驶感应点,红线为自动驾驶小车终点";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            car.SetSpeed(1);
            gra = pictureBox1.CreateGraphics();
            bluePen = new Pen(Color.Red, 3);        
        }

       

         void paintCar(int x,int y,string s)
        {          
            Image img = new Bitmap(s);          
            gra.DrawImage(img, x, y);
            gra.DrawRectangle(bluePen,405,38,3,3);
            gra.DrawRectangle(bluePen, 405,435, 3, 3);
            gra.DrawRectangle(bluePen, 734, 435, 3, 3);
            gra.DrawRectangle(bluePen, 734, 38, 3, 3);
            gra.DrawLine(bluePen, p11, p12);
        }

        
        private void button5_Click(object sender, EventArgs e)
        {
            if (button5.Text == "显示小车")
            {
                paintCar(car.X,car.Y,car.ImageUr1);
                label1.Text = "";      
                button5.Text = "开始移动";
                run = false;
            }
            else
            {
                button5.Text = "显示小车";
                timer1.Start();
                label1.Text = "";
                run = false;
               // pictureBox1.Refresh();
            }
            
        }

        private void button1_Click(object sender, EventArgs e)//加速
        {
            car.SpeedUp();
        }

        private void button3_Click(object sender, EventArgs e)//暂停
        {
            if (button3.Text == "暂停")
            {
                button3.Text = "继续";
                timer1.Stop();
                timer2.Stop();
                run = false;
            }
            else
            {
                button3.Text = "暂停";
                timer1.Start();
                timer2.Start();
                run = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            gra.Clear(Color.White);
            if(run)
            {
                flag  ;
                if (flag > 65)
                {
                    flag = 0;
                    timer2.Start();
                }
            }
            if (car.MoveDirection == (int)Car.move.WTE)
            {
                car.X  = car.Speed;
            }
            else if (car.MoveDirection == (int)Car.move.STN)
            {
                car.Y -= car.Speed;
            }

            else if (car.MoveDirection == (int)Car.move.ETW)
            {
                car.X -= car.Speed;
            }
            else if(car.MoveDirection == (int)Car.move.NTS)
            {
                car.Y  = car.Speed;
            }
            paintCar(car.X, car.Y, car.ImageUr1);


            if ((car.X > pictureBox1.Width - 65) || (car.X < 0) || (car.Y < 0) || (car.Y > pictureBox1.Height - 65))
            {
                timer1.Stop();
                timer2.Stop();
            }
            
        }

        private void button2_Click(object sender, EventArgs e)//减速
        {
            car.SpeedDown();
        }

        private void button4_Click(object sender, EventArgs e)//归位
        {
            gra.Clear(Color.White );
            car.init();
            paintCar(car.X,car.Y,car.ImageUr1);
            button3.Text = "暂停";
            button9.Text = "自动驾驶";
            run = false;
            timer1.Stop();
            timer2.Stop();
            
        }

        private void button6_Click(object sender, EventArgs e)//左转
        {
            timer1.Start();
            if (run) timer2.Start(); 
            car.turnLeft();
            if (car.X < 0) car.X = 0;
            if (car.Y < 0) car.Y = 0;
            if (car.X > (pictureBox1.Width - 65)) car.X = pictureBox1.Width - 65;
            if (car.Y > (pictureBox1.Height - 65)) car.Y = pictureBox1.Height - 65;
        }

        private void button7_Click(object sender, EventArgs e)//右转
        {
            timer1.Start();
            if (run) timer2.Start(); 
            car.turnRight();
            if (car.X < 0) car.X = 0;
            if (car.Y < 0)  car.Y = 0;
            if (car.X > (pictureBox1.Width - 65)) car.X = pictureBox1.Width - 65;
            if (car.Y > (pictureBox1.Height - 65)) car.Y = pictureBox1.Height - 65;
        }

        private void button8_Click(object sender, EventArgs e) //掉头
        {
            timer1.Start();
            if (run) timer2.Start();           
            car.turnBack();
            if (car.X < 0) car.X = 0;
            if (car.Y < 0) car.Y = 0;
            if (car.X > (pictureBox1.Width - 65)) car.X = pictureBox1.Width - 65;
            if (car.Y > (pictureBox1.Height - 65)) car.Y = pictureBox1.Height - 65;
        }

        private void button8_MouseMove(object sender, MouseEventArgs e)
        {
           
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label1.Text = e.X.ToString()   "    "   e.Y.ToString();
        }

        private void button9_Click(object sender, EventArgs e)//无人驾驶
        {
            timer2.Start();
           
            if (button9.Text == "自动驾驶")
            {
                button9.Text = "手动驾驶";
                timer2.Start();
                run = true;
            }
            else
            {
                button9.Text = "自动驾驶";
                timer2.Stop();
                timer2.Stop();
                run = false;
                run = false;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            
            if (car.Speed >= 3)
            {
                car.Speed = 3;
                label1.Text = "自动驾驶,安全第一!(限速)";
            }
            if (Math.Abs(car.X - 405) < 30 && Math.Abs(car.Y - 38) < 30)//1
            {
                if (car.MoveDirection == (int)Car.move.WTE)
                {
                    car.turnRight();
                }
                 else if (car.MoveDirection == (int)Car.move.ETW)
                {
                    car.turnLeft();
                }
                else if (car.MoveDirection == (int)Car.move.STN)
                {
                    car.turnBack();
                }
                timer2.Stop();
            }

            else if (Math.Abs(car.X - 405) < 30 && Math.Abs(car.Y - 435) <30)//2
            {
                if (car.MoveDirection == (int)Car.move.NTS)
                {
                    car.turnLeft();
                }
                 else if (car.MoveDirection == (int)Car.move.ETW)
                {
                    car.turnBack();
                }
                else if (car.MoveDirection == (int)Car.move.WTE)
                {
                   
                }
                timer2.Stop();
            }

            else if (Math.Abs(car.X - 734) < 30 && Math.Abs(car.Y -435) < 30)//3
            {
                if (car.MoveDirection == (int)Car.move.WTE)
                {
                    car.turnLeft();
                }
                 else if (car.MoveDirection == (int)Car.move.ETW)
                {
                    car.turnRight();
                }
                else if (car.MoveDirection == (int)Car.move.NTS)
                {
                    car.turnBack();
                }
                timer2.Stop();
            }

            else if (Math.Abs(car.X - 734) < 28 && Math.Abs(car.Y - 38) < 28)//4
            {
                if (car.MoveDirection == (int)Car.move.WTE)
                {
                   
                }
                else if (car.MoveDirection == (int)Car.move.ETW)
                {
                    car.turnBack();
                }
                else if (car.MoveDirection == (int)Car.move.STN)
                {
                    car.turnRight();
                }
                timer2.Stop();
            }
          
        }
    }
}