c#实现雷霆战机游戏案例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace 飞机大战
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }
        /// <summary>
        /// 设置飞机图片名称的索引
        /// </summary>
        int planeIndex = 1;

        /// <summary>
        /// 代表地图图片名称的索引
        /// </summary>
        int mapIndex = 1;

        /// <summary>
        /// 控制子弹的生成
        /// </summary>
        DispatcherTimer controlPlayer = new DispatcherTimer();
        DispatcherTimer controlBullet = new DispatcherTimer();
        /// <summary>
        /// 控制敌机的生成
        /// </summary>
        DispatcherTimer enemyCreate = new DispatcherTimer();
        //DispatcherTimer fly1move = new DispatcherTimer();
        //DispatcherTimer fly2move = new DispatcherTimer();
        DispatcherTimer fly3move = new DispatcherTimer();


        //集合:代表同一类型的一组对象
        //使用这组集合装图片路径
        List<string> imgs = new List<string>();


        //所有的wpf事件中,都会自带两个变量sender   、e   
        //e:指的是和当前的这个事件有关的第三方对象
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            PlaneName.Foreground = new LinearGradientBrush(Colors.Cyan, Colors.Red, 45);
            MapName.Foreground = new LinearGradientBrush(Colors.Cyan, Colors.Red, 45);

            GameMain.MouseMove += GameMain_MouseMove;


            //controlPlayer.Interval = new TimeSpan(0,0,0,0,10);
            controlPlayer.Interval = TimeSpan.FromMilliseconds(10);
            controlPlayer.Tick += ControlPlayer_Tick;

            controlBullet.Interval = TimeSpan.FromMilliseconds(10);
            controlBullet.Tick += ControlBullet_Tick;

            GameMain.MouseLeftButtonDown += GameMain_MouseLeftButtonDown;

            MainBG.Tag = "BG";
            Player.Tag = "player";

            enemyCreate.Interval = TimeSpan.FromMilliseconds(10);
            enemyCreate.Tick += EnemyCreate_Tick;

            //fly1move.Interval = TimeSpan.FromMilliseconds(10);
            //fly1move.Tick += Fly1move_Tick;

            //fly2move.Interval = TimeSpan.FromMilliseconds(10);
            //fly2move.Tick += Fly2move_Tick;


            fly3move.Interval = TimeSpan.FromMilliseconds(10);
            fly3move.Tick += Fly3move_Tick;

        }



        #region 游戏开始界面
        private void BtnStart_MouseEnter(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Hand;
            BtnStart.Source = new BitmapImage(new Uri("Img/Start/BG/UI_start_on.png", UriKind.Relative));
        }


        private void BtnStart_MouseLeave(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Arrow;
            BtnStart.Source = new BitmapImage(new Uri("Img/Start/BG/UI_start.png", UriKind.Relative));
        }

        /// <summary>
        /// 当点下开始游戏按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnStart_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //隐藏StartUI画布,显示过渡动画画布
            StartUI.Visibility = Visibility.Hidden;
            StartAnim.Visibility = Visibility.Visible;
        }
        #endregion

        #region  游戏过渡动画界面
        private void CG1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            CG1.Visibility = Visibility.Hidden;
        }

        private void CG2_MouseDown(object sender, MouseButtonEventArgs e)
        {
            CG2.Visibility = Visibility.Hidden;
        }

        private void CG3_MouseDown(object sender, MouseButtonEventArgs e)
        {
            StartAnim.Visibility = Visibility.Hidden;
            UserSet.Visibility = Visibility.Visible;
        }
        #endregion

        #region 游戏设置界面
        private void SelectLeftPlane_MouseEnter(object sender, MouseEventArgs e)
        {
            SelectLeftPlane.Source = new BitmapImage(new Uri("Img/Start/User/BtnLeft1.png", UriKind.Relative));
        }

        private void SelectLeftPlane_MouseLeave(object sender, MouseEventArgs e)
        {
            SelectLeftPlane.Source = new BitmapImage(new Uri("Img/Start/User/BtnLeft0.png", UriKind.Relative));
        }

        private void SelectLeftPlane_MouseDown(object sender, MouseButtonEventArgs e)
        {
            planeIndex--;
            if (planeIndex == 0)
            {
                planeIndex = 3;
            }

            SetPlaneName();
        }

        private void SelectRightPlane_MouseEnter(object sender, MouseEventArgs e)
        {
            SelectRightPlane.Source = new BitmapImage(new Uri("Img/Start/User/BtnRight1.png", UriKind.Relative));
        }

        private void SelectRightPlane_MouseLeave(object sender, MouseEventArgs e)
        {
            SelectRightPlane.Source = new BitmapImage(new Uri("Img/Start/User/BtnRight0.png", UriKind.Relative));
        }

        private void SelectRightPlane_MouseDown(object sender, MouseButtonEventArgs e)
        {
            planeIndex++;
            if (planeIndex >= 4)
            {
                planeIndex = 1;
            }

            SetPlaneName();

        }

        /// <summary>
        /// 设置选择的战机的姓名
        /// </summary>
        private void SetPlaneName()
        {
            PlaneBox.Source = new BitmapImage(new Uri("Img/Start/User/plane" + planeIndex + ".png", UriKind.Relative));
            switch (planeIndex)
            {
                case 1:
                    PlaneName.Content = "张碎虎";
                    break;
                case 2:
                    PlaneName.Content = "张老虎";
                    break;
                case 3:
                    PlaneName.Content = "张大虎";
                    break;
                default:
                    break;
            }
        }

        void SetMapName()
        {
            MapBox.Source = new BitmapImage(new Uri("Img/Start/User/Map" + mapIndex + ".png", UriKind.Relative));
            switch (mapIndex)
            {
                case 1:
                    MapName.Content = "张碎虎";
                    break;
                case 2:
                    MapName.Content = "张老虎";
                    break;
                case 3:
                    MapName.Content = "张大虎";
                    break;
                default:
                    break;
            }
        }

        private void SelectLeftMap_MouseDown(object sender, MouseButtonEventArgs e)
        {
            mapIndex--;
            if (mapIndex <= 0)
            {
                mapIndex = 3;
            }
            SetMapName();
        }

        private void SelectRightMap_MouseDown(object sender, MouseButtonEventArgs e)
        {
            mapIndex++;
            if (mapIndex >= 4)
            {
                mapIndex = 1;
            }
            SetMapName();
        }

        private void SelectLeftMap_MouseEnter(object sender, MouseEventArgs e)
        {
            SelectLeftMap.Source = new BitmapImage(new Uri("Img/Start/User/BtnLeft1.png", UriKind.Relative));
        }

        private void SelectLeftMap_MouseLeave(object sender, MouseEventArgs e)
        {
            SelectLeftMap.Source = new BitmapImage(new Uri("Img/Start/User/BtnLeft0.png", UriKind.Relative));
        }

        private void SelectRightMap_MouseEnter(object sender, MouseEventArgs e)
        {
            SelectRightMap.Source = new BitmapImage(new Uri("Img/Start/User/BtnRight1.png", UriKind.Relative));
        }

        private void SelectRightMap_MouseLeave(object sender, MouseEventArgs e)
        {
            SelectRightMap.Source = new BitmapImage(new Uri("Img/Start/User/BtnRight0.png", UriKind.Relative));
        }

        private void BtnBegin_MouseEnter(object sender, MouseEventArgs e)
        {
            BtnBegin.Width = 150;
            BtnBegin.Height = 60;
            Canvas.SetLeft(BtnBegin, Canvas.GetLeft(BtnBegin) - 4);
            Canvas.SetTop(BtnBegin, Canvas.GetTop(BtnBegin) - 3.5);
        }

        private void BtnBegin_MouseLeave(object sender, MouseEventArgs e)
        {
            BtnBegin.Width = 142;
            BtnBegin.Height = 53;
            Canvas.SetLeft(BtnBegin, Canvas.GetLeft(BtnBegin) + 4);
            Canvas.SetTop(BtnBegin, Canvas.GetTop(BtnBegin) + 3.5);
        }

        private void BtnBegin_MouseDown(object sender, MouseButtonEventArgs e)
        {
            UserSet.Visibility = Visibility.Hidden;
            GameMain.Visibility = Visibility.Visible;
            SetGame();
            //controlPlayer.Start();
            controlBullet.Start();
            enemyCreate.Start();

            //fly1move.Start();
            fly3move.Start();
          
        }
        void SetGame()
        {
            switch (planeIndex)
            {
                case 1:
                    Player.Source = new BitmapImage(new Uri("Img/Game/Plane/plane1.png", UriKind.Relative));
                    break;
                case 2:
                    Player.Source = new BitmapImage(new Uri("Img/Game/Plane/plane2.png", UriKind.Relative));
                    break;
                case 3:
                    Player.Source = new BitmapImage(new Uri("Img/Game/Plane/plane3.png", UriKind.Relative));
                    break;
                default:
                    break;
            }
            switch (mapIndex)
            {
                case 1:
                    MainBG.Source = new BitmapImage(new Uri("Img/Start/User/Map1.png", UriKind.Relative));
                    break;
                case 2:
                    MainBG.Source = new BitmapImage(new Uri("Img/Start/User/Map2.png", UriKind.Relative));
                    break;
                case 3:
                    MainBG.Source = new BitmapImage(new Uri("Img/Start/User/Map3.png", UriKind.Relative));
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region  玩家的操作
        private void GameMain_MouseMove(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.None;
            //控制飞机图片跟随鼠标移动
            //方法中填写,计算鼠标位置的坐标系对象
            Point mouseposition = e.GetPosition(GameMain);
            double planex = mouseposition.X - Player.Width / 2;
            double planey = mouseposition.Y - Player.Height / 2;
            Canvas.SetLeft(Player, planex);
            Canvas.SetTop(Player, planey);
        }

        /// <summary>
        /// 计算子弹发射的频率
        /// </summary>
        int rate = 50;
        private void ControlPlayer_Tick(object sender, EventArgs e)
        {
            rate++;
            //首先确定鼠标左键处于按下状态
            //MouseButtonState state = Mouse.LeftButton;
            //if (state== MouseButtonState.Pressed)
            //{
            //    if (rate>=50)
            //    {
            //        rate = 0;
            //    }
            //}

            //else
            //{
            //    rate = 50;
            //}
        }

        private void GameMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //当每隔500毫秒飞机装弹完成
            //if (rate >= 50)
            //{
            //刷新子弹冷却时间
            rate = 0;
            //制造子弹
            Image bullet = new Image();
            //给这个子弹Image添加一个标签为"zd"
            bullet.Tag = "zd";
            bullet.Width = 10;
            bullet.Height = 20;
            bullet.Source = new BitmapImage(new Uri("Img/Game/Bullet/PlayerBullet.png", UriKind.Relative));
            Canvas.SetTop(bullet, Canvas.GetTop(Player) - bullet.Height);
            Canvas.SetLeft(bullet, (Canvas.GetLeft(Player) + Player.Width / 2 - bullet.Width / 2));
            GameMain.Children.Add(bullet);
            //}
        }

        /// <summary>
        /// 控制子弹的移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ControlBullet_Tick(object sender, EventArgs e)
        {
            //遍历:在一个集合里面找对象
            foreach (UIElement element in GameMain.Children)
            {
                Image bulletImg = element as Image;
                //判断找到的这个对象是子弹
                if (bulletImg.Tag.ToString() == "zd")
                {
                    Canvas.SetTop(bulletImg, Canvas.GetTop(bulletImg) - 10);
                    //判断子弹飞出游戏区域
                    if (Canvas.GetTop(bulletImg)<=-bulletImg.Height)
                    {
                        GameMain.Children.Remove(bulletImg);
                        break;
                    }
                    //判断子弹击中敌机
                    //先遍历出每一辆敌机
                    foreach (UIElement enyelement in GameMain.Children)
                    {
                        Image enemyimg = enyelement as Image;
                        if (enemyimg.Tag.ToString()== "nmy3")
                        {
                            //判断子弹的高度和每一辆敌机的高度达到击中
                            if (Canvas.GetTop(bulletImg)<=Canvas.GetTop(enemyimg)+enemyimg.Height/2)
                            {
                                //证明水平位置上击中了敌机
                                if (Canvas.GetLeft(bulletImg)>=Canvas.GetLeft(enemyimg)-bulletImg.Width/2&&Canvas.GetLeft(bulletImg)<=Canvas.GetLeft(enemyimg)+enemyimg.Width-bulletImg.Width/2)
                                {
                                    //实现爆炸动画播放
                                    Image bombImg = new Image();
                                    bombImg.Width = 50;
                                    bombImg.Height=50;
                                    bombImg.Tag = "aa";
                                    bombImg.Source = new BitmapImage(new Uri("Img/Game/Bomb/0.png",UriKind.Relative));
                                    Canvas.SetLeft(bombImg,(Canvas.GetLeft(enemyimg)+enemyimg.Width/2-bombImg.Width/2));
                                    Canvas.SetTop(bombImg, (Canvas.GetTop(enemyimg) + enemyimg.Height / 2 - bombImg.Height / 2));
                                    GameMain.Children.Add(bombImg);

                                    DispatcherTimer bombTimer = new DispatcherTimer();
                                    bombTimer.Interval = TimeSpan.FromMilliseconds(20);
                                    bombTimer.Tag = bombImg;
                                    bombTimer.Tick += BombTimer_Tick;
                                    bombTimer.Start();


                                    GameMain.Children.Remove(bulletImg);
                                    GameMain.Children.Remove(enemyimg);
                                    //跳出这个事件
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }

        int index = 0;
        private void BombTimer_Tick(object sender, EventArgs e)
        {
            //通过sender获取到产生这个事件的对象timer
            DispatcherTimer timer = sender as DispatcherTimer;
            //既然能够拿到timer也就意味着能够通过timer的Tag属性获取到和timer同时生成的Image
            Image img = timer.Tag as Image;
            index++;
            img.Source= new BitmapImage(new Uri("Img/Game/Bomb/"+index+".png", UriKind.Relative));
            if (index==31)
            {
                index = 0;
                GameMain.Children.Remove(img);
                timer.Stop();
            }
        }
        #endregion

        #region 敌机智能AI
        /// <summary>
        /// 用来计算时间
        /// </summary>
        int enemyRate = 0;
        /// <summary>
        /// 计算敌机生成的频率
        /// </summary>
        int createRate = 100;

        Random r = new Random();
        private void EnemyCreate_Tick(object sender, EventArgs e)
        {
            enemyRate++;
            //判断时间达到一秒钟,可以生成一架敌机
            if (enemyRate >= createRate)
            {
                enemyRate = 0;
                //生成一架敌机
                CreateEnemy();
            }
        }


        /// <summary>
        /// 生成敌机的方法
        /// </summary>
        void CreateEnemy()
        {
            Image normalEnemy = new Image();
            normalEnemy.Width = 50;
            normalEnemy.Height = 35;
            normalEnemy.Tag = "nmy3";
            int enemyIndex = r.Next(3);
            normalEnemy.Stretch = Stretch.Fill;
            switch (enemyIndex)
            {
                case 0:
                    normalEnemy.Source = new BitmapImage(new Uri("Img/Game/Enemy/Enemy1.png", UriKind.Relative));
                    
                    break;
                case 1:
                    normalEnemy.Source = new BitmapImage(new Uri("Img/Game/Enemy/Enemy2.png", UriKind.Relative));
                    break;
                case 2:
                    normalEnemy.Source = new BitmapImage(new Uri("Img/Game/Enemy/Enemy3.png", UriKind.Relative));
                  
                    break;
                default:
                    break;
            }
            //计算生成的随机位置
            Point enemyPos = new Point(r.Next(Convert.ToInt32(GameMain.Width - normalEnemy.Width)), -normalEnemy.Height);
            Canvas.SetLeft(normalEnemy, enemyPos.X);
            Canvas.SetTop(normalEnemy, enemyPos.Y);
            GameMain.Children.Add(normalEnemy);
        }

        /// <summary>
        /// 控制第三种类型敌机刚生成向下方移动
        /// </summary>
        double enmy3y = 1;
        private void Fly3move_Tick(object sender, EventArgs e)
        {
            foreach (UIElement element in GameMain.Children)
            {
                Image enemyImg = element as Image;
                if (enemyImg.Tag.ToString() == "nmy3")
                {
                    Canvas.SetTop(enemyImg, Canvas.GetTop(enemyImg) + enmy3y);
                    if (Canvas.GetTop(enemyImg)>=GameMain.Height)
                    {
                        GameMain.Children.Remove(enemyImg);
                        //跳出当前本次循环
                        break;
                        //return;continue
                    }
                }
            }
        }
        #endregion
    }
}

猜你喜欢

转载自blog.csdn.net/qq_43552118/article/details/87107676