必威体育Betway必威体育官网
当前位置:首页 > IT技术

雷霆战机

时间:2019-10-19 01:12:11来源:IT技术作者:seo实验室小编阅读:54次「手机版」
 

雷霆战机

1,#region #endregion:设置代码缩进

2,FromFile:从指定的文件创建image

3,sizemode:图片平铺:拉伸 stretchImage

4,Panel:相当于一个容器,可以装除过本身的对象

5,XAML:可扩展应用程序标记语言

6,XML:可扩展标记语言

7,HerizontalcontentAlignment:文本水平居中

​ verticalAlignment:文本垂直居中

8,RadialGradientBrush:径向渐变

​ LinearGradientBrush:线性渐变

9,随机

​ Random r=new Random();

​ int index=r.Next(3);3的意思是索引最大取到2

10,添加图片

​ Image img=new Image();

​ 图片路径: img.Source=new BitmapImage(new Uri(“路径”),Urikind.Relative);

11,事件中的参数:

​ sender:哪个对象的事件,sender就是谁

​ e:代表和当前事件有关的第三方对象

12,未将对象引用设置到对象的实例

​ 解决方法:给对象添加一个标签

雷霆战机案例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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();
        }
        //设置飞机索引
        int planeindex = 1;
        //设置场景的索引
        int spotindex = 1;
        //控制子弹的生成
        DispatcherTimer controlPlayer = new DispatcherTimer();
        DispatcherTimer controlBullet = new DispatcherTimer();
        //控制敌机的生成
        DispatcherTimer EnemyCreate = new DispatcherTimer();
        //控制敌机移动
        DispatcherTimer fly3move = new DispatcherTimer();
        //控制boss生成
        DispatcherTimer BossEnemy = new DispatcherTimer();
        //控制boss移动
        DispatcherTimer bossmove = new DispatcherTimer();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //飞机名称的颜色
            PlaneName.Foreground = new LinearGradientBrush(colors.Cyan, Colors.Plum, 45);
            //场景名称的颜色
            SpotName.Foreground = new LinearGradientBrush(Colors.Cyan, Colors.Plum, 45);
            GameMain.MouseMove += GameMain_MouseMove;
            GameMain.MouseLeftButtonDown += GameMain_MouseLeftButtonDown;
            //第一种写法
            //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;
            //敌机
            EnemyCreate.Interval = TimeSpan.FromMilliseconds(10);
            EnemyCreate.Tick += EnemyCreate_Tick;
            //敌机移动
            fly3move.Interval = TimeSpan.FromMilliseconds(10);
            fly3move.Tick += Fly3move_Tick;
            //boss移动
            bossmove.Interval = TimeSpan.FromMilliseconds(10);
            bossmove.Tick += Bossmove_Tick;
            //给对象添加一个标签 
            MainBG.Tag = "BG";
            Player.Tag = "player";
        }



        #region 子弹发射
        /// <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;
                    //制造子弹
                    Image bullet = new Image();
                    //给这个子弹image添加一个标签为"zd"
                    bullet.Tag = "zd";
                    bullet.Width = 10;
                    bullet.Height = 20;
                    bullet.Source = new BitmapImage(new Uri("img/Game/Bullet/Pbullet.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);
                }
                else
                {
                    rate = 50;
                }
            }
        }
        #endregion

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

        private void btnStart_MouseDown(object sender, MouseButtonEventArgs e)
        {
            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();
        }
        //飞机名称
        private void SetPlaneName()
        {
            PlaneBox.Source = new BitmapImage(new Uri("img/start/User/plane" + planeindex + ".png", UriKind.Relative));
            switch (planeindex)
            {
                case 1:
                    PlaneName.Content = "Lay战机";
                    break;
                case 2:
                    PlaneName.Content = "Cai战机";
                    break;
                case 3:
                    PlaneName.Content = "JJ战机";
                    break;
                default:
                    break;
            }
        }
        //左右键以及点击左右键选择的地点
        private void SpotLeft_MouseEnter(object sender, MouseEventArgs e)
        {
            SpotLeft.Source = new BitmapImage(new Uri("img/start/User/BtnLeft1.png", UriKind.Relative));
        }

        private void SpotLeft_MouseLeave(object sender, MouseEventArgs e)
        {
            SpotLeft.Source = new BitmapImage(new Uri("img/start/User/BtnLeft0.png", UriKind.Relative));
        }

        private void SpotLeft_MouseDown(object sender, MouseButtonEventArgs e)
        {
            spotindex--;
            if (spotindex == 0)
            {
                spotindex = 3;
            }
            SetSpotName();
        }

        private void SpotRight_MouseEnter(object sender, MouseEventArgs e)
        {
            SpotRight.Source = new BitmapImage(new Uri("img/start/User/BtnRight1.png", UriKind.Relative));
        }

        private void SpotRight_MouseLeave(object sender, MouseEventArgs e)
        {
            SpotRight.Source = new BitmapImage(new Uri("img/start/User/BtnRight0.png", UriKind.Relative));
        }

        private void SpotRight_MouseDown(object sender, MouseButtonEventArgs e)
        {
            spotindex++;
            if (spotindex >= 4)
            {
                spotindex = 1;
            }
            SetSpotName();
        }
        //地点区域
        private void SetSpotName()
        {
            SpotBox.Source = new BitmapImage(new Uri("img/start/User/spot" + spotindex + ".png", UriKind.Relative));
            switch (spotindex)
            {
                case 1:
                    SpotName.Content = "黑色世界";
                    break;
                case 2:
                    SpotName.Content = "蓝色世界";
                    break;
                case 3:
                    SpotName.Content = "红色世界";
                    break;
                default:
                    break;
            }
        }
        //确定按钮
        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;
            controlBullet.Start();
            controlPlayer.Start();
            EnemyCreate.Start();
            fly3move.Start();
            bossmove.Start();
            SetGame();
        }
        //GameMain确定选择的飞机、场景
        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 (spotindex)
            {
                case 1:
                    MainBG.Source = new BitmapImage(new Uri("img/start/User/spot1.png", UriKind.Relative));
                    break;
                case 2:
                    MainBG.Source = new BitmapImage(new Uri("img/start/User/spot2.png", UriKind.Relative));
                    break;
                case 3:
                    MainBG.Source = new BitmapImage(new Uri("img/start/User/spot3.png", UriKind.Relative));
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 玩家操作界面
        private void GameMain_MouseMove(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.None;
            //控制飞机图片跟随鼠标移到
            //方法中填写,计算鼠标位置的坐标系对象
            Point position = e.GetPosition(GameMain);
            double planeX = position.X - Player.Width / 2;
            double planeY = position.Y - Player.Height / 2;
            Canvas.SetLeft(Player, planeX);
            Canvas.SetTop(Player, planeY);
            //判断飞机碰到左边缘
            if (position.X <= Player.Width / 2)
            {
                Canvas.SetLeft(Player, 0);
            }
            if (position.X >= GameMain.Width - Player.Width / 2)
            {
                Canvas.SetLeft(Player, GameMain.Width - Player.Width);
            }
            if (position.Y <= Player.Height / 2)
            {
                Canvas.SetTop(Player, 0);
            }
            if (position.Y >= GameMain.Height - Player.Height / 2)
            {
                Canvas.SetTop(Player, GameMain.Height - Player.Height);
            }
        }
        private void GameMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ////制造子弹
            //Image bullet = new Image();
            ////给这个子弹image添加一个标签为"zd"
            //bullet.Tag = "zd";
            //bullet.Width = 10;
            //bullet.Height = 20;
            //bullet.Source = new BitmapImage(new Uri("img/Game/Bullet/Pbullet.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);
        }
        private void ControlBullet_Tick(object sender, EventArgs e)
        {
            //遍历:在一个集合里面找对象

            foreach (UIElement element in GameMain.Children)
            {
                //强制转换:element as Image
                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 enemying = enyelement as Image;
                        if (enemying.Tag.ToString() == "nmy3")
                        {
                            //判断子弹的高度和每一辆敌机的高度达到一致
                            if (Canvas.GetTop(bulletImg) <= Canvas.GetTop(enemying) + enemying.Height / 2)
                            {
                                //证明水平位置击中了敌机
                                if (Canvas.GetLeft(bulletImg) >= Canvas.GetLeft(enemying) - bulletImg.Width / 2 &&
                                    Canvas.GetLeft(bulletImg) <= Canvas.GetLeft(enemying) + enemying.Width - bulletImg.Width / 2)
                                {
                                    //实现爆炸的动画播放
                                    Image bombImg = new Image();
                                    bombImg.Width = 50;
                                    bombImg.Height = 50;
                                    bombImg.Tag = "fly";
                                    bombImg.Source = new BitmapImage(new Uri("img/Game/Bomb/0.png", UriKind.Relative));
                                    Canvas.SetLeft(bombImg, (Canvas.GetLeft(enemying) + enemying.Width / 2 - bombImg.Width / 2));
                                    Canvas.SetTop(bombImg, (Canvas.GetTop(enemying) + enemying.Height / 2 - bombImg.Height / 2));
                                    GameMain.Children.Add(bombImg);

                                    DispatcherTimer bombTimer = new DispatcherTimer();
                                    bombTimer.Interval = TimeSpan.FromMilliseconds(10);
                                    bombTimer.Tag = bombImg;
                                    bombTimer.Tick += BombTimer_Tick;
                                    bombTimer.Start();
                                    GameMain.Children.Remove(bulletImg);
                                    GameMain.Children.Remove(enemying);
                                    //跳出这个事件
                                    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 敌机的生成
        //用来计算时间
        int enemyRate = 0;
        //计算敌机生成的频率
        int createRate = 100;
        //boss生成
        int BossRate = 0;
        int Bosscreate = 1000;
        //随机产生敌机
        Random r = new Random();
        private void EnemyCreate_Tick(object sender, EventArgs e)
        {
            enemyRate++;
            //判断时间达到一秒钟,可以生成一架敌机
            if (enemyRate >= createRate)
            {
                enemyRate = 0;
                //生成一架飞机
                CreateEnemy();
            }
            BossRate++;
            //判断时间达到一秒钟,可以生成一架boss机
            if (BossRate >= Bosscreate)
            {
                BossRate = 0;
                //生成一架boss
                BossPlayer();
            }
        }
        /// <summary>
        /// 生成敌机方法
        /// </summary>
        void CreateEnemy()
        {
            Image normalEnemy = new Image();
            normalEnemy.Tag = "nmy3";
            normalEnemy.Width = 50;
            normalEnemy.Height = 35;
            normalEnemy.Stretch = Stretch.Fill;
            int enemyindex = r.Next(3);
            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 enemy3y = 1;
        //定义一个变量 记录小兵的个数
        int smallplayer = 0;
        /// <summary>
        /// 生成boss机的方法
        /// </summary>
        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) + enemy3y);
                    if (Canvas.GetTop(enemyImg) >= GameMain.Height)
                    {
                        //判断敌机落到最下面自己的飞机消失
                        smallplayer++;
                        if (smallplayer >= 5)
                        {
                            GameMain.Children.Remove(Player);
                            messageBox.Show("Game Over");
                            fly3move.Stop();
                        }
                        GameMain.Children.Remove(enemyImg);
                        break;
                    }
                }

            }
        }

        void BossPlayer()
        {
            Image BossEnemy = new Image();
            BossEnemy.Tag = "nmy2";
            BossEnemy.Width = 70;
            BossEnemy.Height = 50;
            BossEnemy.Stretch = Stretch.Fill;
            int Bossindex = r.Next(3);
            switch (Bossindex)
            {
                case 0:
                    BossEnemy.Source = new BitmapImage(new Uri("img/Game/Enemy/EnemyB.png", UriKind.Relative));
                    break;
                case 1:
                    BossEnemy.Source = new BitmapImage(new Uri("img/Game/Enemy/EnemyT.png", UriKind.Relative));
                    break;
                case 2:
                    BossEnemy.Source = new BitmapImage(new Uri("img/Game/Enemy/EnemyBlood.png", UriKind.Relative));
                    break;
                default:
                    break;
            }
            //计算生成的随机位置
            Point enemyPos = new Point(r.Next(Convert.ToInt32(GameMain.Width - BossEnemy.Width)), 0);
            Canvas.SetLeft(BossEnemy, enemyPos.X);
            Canvas.SetTop(BossEnemy, enemyPos.Y);
            GameMain.Children.Add(BossEnemy);
        }

        double flyL = 1;
        private void Bossmove_Tick(object sender, EventArgs e)
        {
            foreach (UIElement element in GameMain.Children)
            {
                Image BossEnemy = element as Image;
                if (BossEnemy.Tag.ToString() == "nmy2")
                {
                    Canvas.SetLeft(BossEnemy, Canvas.GetLeft(BossEnemy) + flyL);
                    if (Canvas.GetLeft(BossEnemy)>=GameMain.Width-BossEnemy.Width)
                    {
                        flyL = -1;
                    }
                    if (Canvas.GetLeft(BossEnemy)<=0)
                    {
                        flyL = 1;
                    }
                }
            }
        }
        #endregion
    }
}

相关阅读

“未来雷霆舱”叫板VR体验:你还不够爽!

随着科技的发展和人们需求的不断提升,游戏体验不再局限于屏幕,而是搬到了现实空间,真人亲临其中、亲身体会,刺激感倍增,这也使得VR产业

雷霆i智能众筹1200万创全球第一,究竟是什么人在买

73%关注雷霆i智能女性震动棒的用户竟然是男性?这一数据让人太过震惊了,在淘宝平台关键词&ldquo;雷霆i智能&rdquo;淘宝指数关注性别

分享到:

栏目导航

推荐阅读

热门阅读