欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#像素鸟(独自一鸟闯天下)

程序员文章站 2022-07-07 14:18:28
...
PictureBox BG = new PictureBox();//游戏背景
        PictureBox FN = new PictureBox();//生成鸟
        PictureBox TD = new PictureBox();//土地
        Timer fn = new Timer();//移动鸟
        Timer PZ = new Timer();//判断碰撞
        Timer KD = new Timer();//生成水管
        Timer GG = new Timer();//移动水管
        private void Form1_Load(object sender, EventArgs e)
        {
            //窗体最大化
            this.WindowState = FormWindowState.Maximized;
            BG.Size = new Size(800,800);
            BG.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width/2-BG.Width/2,0);
            BG.BackgroundImage = Image.FromFile(@"../../img/01.png");
            BG.MouseClick += BG_MouseClick;
            //土地
            TD.Size = new Size(800,100);
            TD.BackgroundImage = Image.FromFile(@"../../img/07.png");
            TD.Top = 700;
            BG.Controls.Add(TD);
            this.Controls.Add(BG);
            //生成鸟
            FN.Size = new Size(50, 50);
            FN.Name = "FN";
            FN.BackgroundImage = Image.FromFile(@"../../img/04.png");
            FN.SizeMode = PictureBoxSizeMode.StretchImage;
            FN.BackColor = Color.Transparent;
            FN.Location = new Point(50,BG.Height/2-TD.Height-FN.Height/2);
            //下落鸟
            fn.Interval = 120;
            fn.Tick += Fn_Tick;
            fn.Start();
            BG.Controls.Add(FN);
            //生成水管
            
            KD.Start();
            KD.Interval = 1000;
            KD.Tick += KD_Tick;
            //移动水管
            GG.Start();
            GG.Interval = 100;
            GG.Tick += GG_Tick;
        }
        private void GG_Tick(object sender, EventArgs e)
        {
            foreach ( Control item in BG.Controls) 
            {
                if (item.Name=="UG"|| item.Name == "DG")
                {
                    item.Left -= 30;

                    foreach (Control item1 in BG.Controls)
                    {
                        if (item1.Name=="FN")
                        {
                            if (FN.Left + FN.Width >= item.Left && FN.Top+FN.Height >= item.Top&&FN.Left<=item.Left+item.Width&&FN.Top<=item.Height+item.Top)
                            {
                                fn.Stop();
                                KD.Stop();
                                GG.Stop();
                                PZ.Stop();
                                MessageBox.Show("请重新开始游戏!");
                            }

                        }
                    }

                }
            }
        }
        Random r = new Random();//随机
        int Z = 200;//定义一个中间数
        private void KD_Tick(object sender, EventArgs e)
        {
            int hei = r.Next(100,300); 
            //上管道
            PictureBox UG = new PictureBox();
            UG.Image = Image.FromFile(@"../../img/02.png");
            UG.SizeMode = PictureBoxSizeMode.StretchImage;
            UG.Size = new Size(55, hei);
            UG.Name = "UG";
            UG.Tag = "ug";
            UG.Location = new Point(600, 0);
            BG.Controls.Add(UG);
            //下管道
            PictureBox DG = new PictureBox();
            DG.Image = Image.FromFile(@"../../img/03.png");
            DG.SizeMode = PictureBoxSizeMode.StretchImage;
            DG.Size = new Size(55,BG.Height - DG.Height- hei-Z);
            DG.Name = "DG";
            DG.Tag = "dg";
            DG.Location = new Point(600, BG.Height - TD.Height - DG.Height);
            BG.Controls.Add(DG);
        }
        //点击上升
        private void BG_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button==MouseButtons.Left)
            {
                FN.Top -= 50;
                FN.Left -= 5;
            }
        }
        //鸟下落
        private void Fn_Tick(object sender, EventArgs e)
        {
            foreach (Control item in BG.Controls)
            {
                if (item.Name=="FN")
                {
                    item.Top += 10;
                    item.Left += 2;
                    if (item.Top >= this.Height - TD.Height - item.Height - item.Height/2)
                    {
                        fn.Stop();
                        GG.Stop();
                        KD.Stop();
                        MessageBox.Show("请重新开始游戏!");
                    }
                }
                if (item.Name=="UG")
                {
                    item.Left -= 5;
                }
                if (item.Name=="DG")
                {
                    item.Left -= 5;
                }
            }
        }

C#像素鸟(独自一鸟闯天下)

相关标签: 像素鸟的日常