资源经验分享C#写的打飞机

C#写的打飞机

2019-11-20 | |  108 |   0

原标题:C#写的打飞机

原文来自:CSDN      原文链接:https://blog.csdn.net/JGD313/article/details/103116780


20191118180009718.png


分析:
1.飞机的出现,
2.飞机的移动通过鼠标,
3.子弹的出现
4.敌机的随机出现
5.子弹碰撞敌机 飞机碰撞敌机 碰撞的原理

namespace 飞战
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // 游戏背景
        Panel bg = new Panel();
        //飞机
        PictureBox fly = new PictureBox();
        // 随机敌机位置
        Random r = new Random();

        //敌机的创建
        Timer diji = new Timer();
        // 敌机的下落
        Timer di = new Timer();
        int coun = 0;
        int count = 0;
        // 飞机下面的火焰
        PictureBox f1 = new PictureBox();
        PictureBox f2 = new PictureBox();
        int index = 0;
        int index1 = 0;
        Label lb1 = new Label();
        Timer zdt = new Timer();
        Timer crezd = new Timer();
        Label lb3 = new Label();
        SoundPlayer bs = new SoundPlayer();
        private void Form1_Load(object sender, EventArgs e)
        {
            //form的样式
            this.Size = new Size(1300,800);
            this.Location = new Point(20,20);
            //创建背景
            bg.Size = new Size(1100, 700);
            bg.Location = new Point(10, 20);
            bg.BackColor = Color.Gray;
            bg.MouseMove += Bg_MouseMove;
            this.Controls.Add(bg);
            //创建飞机
            fly.Size = new Size(100,100);
            fly.Tag = "fj";
            fly.Image = Image.FromFile(@"../../img/fei1.png");
            fly.Location = new Point(bg.Width/2-fly.Width/2,bg.Height-fly.Height);
            bg.Controls.Add(fly);


            lb1.Size = new Size(150, 500);
            lb1.Location = new Point(this.Width - lb1.Width, 30);
            lb1.BackColor = Color.Aqua;
            lb1.Text = "得分:" + count + "分";
            lb1.Font = new Font("", 15);
            this.Controls.Add(lb1);

            lb3.Size = new Size(100, 30);
            lb3.Location = new Point(5, 60);
            lb3.ForeColor = Color.DeepPink;
            lb3.Text = "开始游戏";
            lb3.Click += Lb3_Click;
            lb1.Controls.Add(lb3);
            //子弹的计时器

            zdt.Tick += Zdt_Tick;
            zdt.Interval = 80;
            
         
            // 子弹创建的计时器
            crezd.Tick += Crezd_Tick;
            crezd.Interval =400;
           

            diji.Tick += Diji_Tick;
            diji.Interval = 1000;
            
            di.Tick += Di_Tick;
            di.Interval = 60;
          


            f2.Size = new Size(15, 30);
            f2.Tag = "f2";
            f2.Location = new Point(fly.Left + fly.Width / 2 - f2.Width - 20, fly.Top + fly.Height);
            f2.Image = imageList3.Images[0];
            f2.SizeMode = PictureBoxSizeMode.StretchImage;
            bg.Controls.Add(f2);

           
            f1.Size = new Size(15, 30);
            f1.Tag = "f1";
            f1.Location = new Point(fly.Left + fly.Width / 2 - f1.Width / 2, fly.Top + fly.Height);
            f1.Image = imageList2.Images[0];
            f1.SizeMode = PictureBoxSizeMode.StretchImage;
            bg.Controls.Add(f1);

            // 火焰的计时器
            Timer t1 = new Timer();
            Timer t2 = new Timer();
            t1.Tick += T1_Tick;
            t1.Interval = 10;
            t1.Start();
            t2.Tick += T2_Tick;
            t2.Interval = 10;
            t2.Start();




        }
        bool po = true;
        private void Lb3_Click(object sender, EventArgs e)
        {

            if (po==true)
             {
                diji.Start();
                di.Start();
                crezd.Start();
                zdt.Start();
                lb3.Text = "暂停游戏";
                 po = false;
             }
             else
            {
                diji.Stop();
                di.Stop();
                crezd.Stop();
                zdt.Stop();

                lb3.Text = "开始游戏";
                 po = true;
             }
        }

        private void T2_Tick(object sender, EventArgs e)
        {
           
            f2.Image = imageList3.Images[index1];
            index1++;
            if (index1 >1)
            {
                index1 = 0;
            }
        }
        // 火焰的动画
        private void T1_Tick(object sender, EventArgs e)
        {
            f1.Image = imageList2.Images[index];
            index++;
            if (index >1)
            {
                index = 0;
            }
        }


        //敌机的移动
        private void Di_Tick(object sender, EventArgs e)
        {
            foreach (Control dj in bg.Controls)
            {
                if (dj.Tag.ToString()=="diji")
                {
                    dj.Top += 5;
                }
            }
        }
        //敌机的创建
        private void Diji_Tick(object sender, EventArgs e)
        {
            PictureBox diji = new PictureBox();
            diji.Image = Image.FromFile(@"../../img/Enemy1.png");
            diji.Tag = "diji";
            diji.Size = new Size(60,60);
            diji.SizeMode = PictureBoxSizeMode.StretchImage;
            diji.Location = new Point( r.Next(bg.Width-diji.Width),0);
            coun++;
           // diji.Image = Image.FromFile(@"../../img/Enemy"+r.Next(1,3)+".png");
            if (coun % 2 == 0)
            {
                diji.Image = Image.FromFile(@"../../img/Enemy2.png");
            }else if(coun%3==0)
            {
                diji.Image = Image.FromFile(@"../../img/Enemy3_1.png");
            }
            bg.Controls.Add(diji);
            
        }
        ListpictureBoxes = new List(); 
        // 子弹的创建
        private void Crezd_Tick(object sender, EventArgs e)
        {
            PictureBox zd = new PictureBox();
         
            zd.Image = Image.FromFile(@"../../img/zd1.png");
            zd.Size = new Size(20, 20);
            zd.Tag = "zd";
            zd.Location = new Point(fly.Left + fly.Width / 2 - zd.Width + 2, fly.Top - fly.Height+zd.Height+60);
            // zd.SizeMode = PictureBoxSizeMode.StretchImage;
            bg.Controls.Add(zd);
            pictureBoxes.Add(zd);
        }
        // 子弹的移动
        private void Zdt_Tick(object sender, EventArgs e)
        {
            foreach(Control item  in bg.Controls)
            {

                if (item.Tag.ToString() == "zd")
                {
                    item.Top -= 10;
                    foreach (Control dj in bg.Controls)
                    {
                        if (dj.Tag.ToString() == "diji")
                        {
                            if (item.Left >= dj.Left && item.Left = dj.Top && item.Top  31)
            {
                pic.Dispose();
                tim.Dispose();
            }
        }

        // 鼠标控制飞机移动

        private void qing()
        {
            foreach (PictureBox zd in pictureBoxes)
            {
                zd.Dispose();
            }
        }



        private void Bg_MouseMove(object sender, MouseEventArgs e)
        {
            Cursor.Hide();
            fly.Left = MousePosition.X - this.Left - bg.Left - fly.Width ;
            fly.Top = MousePosition.Y - this.Top - bg.Top - fly.Height;
            f1.Location = new Point(fly.Left + fly.Width / 2 - f1.Width / 2, fly.Top + fly.Height);
            f2.Location = new Point(fly.Left + fly.Width / 2 - f1.Width / 2-20, fly.Top + fly.Height);

            foreach (Control item in bg.Controls)
            {  
                if (item.Tag.ToString()=="diji")
                {
                     if ((item.Left >= fly.Left &&item.Left = fly.Top &&item.Top <= fly.Left + fly.Height))
                     {
                         
                        item.Dispose();
                        fly.Dispose();
                        qing();
                        zdt.Stop();
                        crezd.Stop();
                        f1.Dispose();
                        f2.Dispose();
                    }
                }
            }
        }
    }
}

免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。

合作及投稿邮箱:E-mail:editor@tusaishared.com

上一篇:python自动化实现登录获取图片验证码

下一篇:姿态估计0-08:DenseFusion(6D姿态估计)-源码解析(4)-PoseNet网络loss详解(重点篇)

用户评价
全部评价

热门资源

  • Python 爬虫(二)...

    所谓爬虫就是模拟客户端发送网络请求,获取网络响...

  • TensorFlow从1到2...

    原文第四篇中,我们介绍了官方的入门案例MNIST,功...

  • TensorFlow从1到2...

    “回归”这个词,既是Regression算法的名称,也代表...

  • 机器学习中的熵、...

    熵 (entropy) 这一词最初来源于热力学。1948年,克...

  • TensorFlow2.0(10...

    前面的博客中我们说过,在加载数据和预处理数据时...