在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 控制小车 移动 示例源码

C# 控制小车 移动 示例源码

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.08M
  • 下载次数:18
  • 浏览次数:170
  • 发布时间:2018-05-16
  • 实例类别:C#语言基础
  • 发 布 人:konger
  • 所需积分: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();
            }
          
        }
    }
}

标签:

实例下载地址

C# 控制小车 移动 示例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
;
报警
var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_4847253'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s13.cnzz.com/stat.php%3Fid%3D4847253' type='text/javascript'%3E%3C/script%3E"));