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

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;
                }
            }
        }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值