C#打飞机游戏

来源:互联网 发布:ubuntu安装monaco 编辑:程序博客网 时间:2024/06/11 12:30

游戏简介:地面有一个导弹车,可以发射导弹。天空上飞着飞机,导弹打到飞机时,游戏结束。
设计思路:
1,俩个定时器。一个用于飞机的飞行,一个用于导弹发射。
2,一个矩形的判断,若飞机矩形和导弹矩形有重叠就视为发生碰撞,游戏结束。
主要代码:
//飞机飞行
        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Location = new Point(x, y);
            x = x + 6;
            if (x >= int.Parse(this.Size.Width.ToString()))
            {
                x = 0;
            }
//以下语句判断飞机与导弹两个矩形是否交叠
              Rectangle pictureBox1ScreenBounds = pictureBox1.Bounds;
             Rectangle pictureBox3ScreenBounds = pictureBox3.Bounds;
            bool intersected = pictureBox1ScreenBounds.IntersectsWith(pictureBox3ScreenBounds);
            if (intersected) //如果打中,飞机为pictureBox1,导弹为pictureBox3
            {
                //首先停止飞行
                timer1.Enabled = false;
                timer2.Enabled = false;
                pictureBox4.Location = pictureBox3.Location;  //复制位置'
                //置为游戏结束状态
                pictureBox1.Visible = false;
                pictureBox3.Visible = false;
                pictureBox2.Visible = false;
                btnPlay.Enabled = false;
                pictureBox4.Visible = true;
            }
//导弹发射

    //鼠标放在别的位置,导弹车移位
        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            mouse_offset = new Point(-e.X, -e.Y);//移动导弹车用的偏移量
            pictureBox3.Visible = true;  //显示图片
            pictureBox3.Location = new Point(pictureBox2.Location.X+pictureBox2.Width-pictureBox3.Width, pictureBox2.Location.Y); ;
            DaoDanloc = pictureBox3.Location;  //记录位置
            timer2.Enabled = true;  //开始计时器
        }
 //捕获导弹车的位置,模拟导弹发射。
        private void timer2_Tick(object sender, EventArgs e)
        {
            Point currentLoc = pictureBox3.Location;
            pictureBox3.Location = new Point(pictureBox3.Location.X,pictureBox3.Location.Y-6);
            if (pictureBox3.Location.Y<=0) //超出范围
            {
                pictureBox3.Location =DaoDanloc;
            }
        }


        }

原创粉丝点击