"Head First C #" alien invasion WPF to write source code

@ (Alien invasion (written in WPF))

introduction

Self-learning C #, read several textbooks telling the program code, but also to find online Screen, but are relatively old. We will play some code I do not know why this feels very awkward to play, at a friend's recommendation to choose this "Head First C #" before the next general understanding of C #.
The first chapter of this book I have encountered a problem, choose to install IDE is VS2012 for win8, it is entirely unworthy of my system and I was using VS2017 + win10, thought should be the same, but did not find the windows store, I guess I'm just wondering installed for .NET is not installed windows common platform that environment, if selected, then install the 12G space environment, too lazy loading. The book describes the use of WPF can play most of the code, then just live with it! A reference to the book PDF download URL but with a ladder Rom, played prefix discovery into their company's main station. URL changed the estimate, did not find the PDF resources on CSDN. Come blind yourself wondering, this blog is a record, I hope useful to you.

preliminary work

  1. Open VS, create WPF form;
    Creation interface
  2. According to build step by step book interface, substantially the same
    Here Insert Picture Description
  3. Well, we have to build to write the code, the code which I'll explain a little bit different of

    Coding

Precautions:

  1. Local references: Here you will find no using windows.UI.Xaml; references and the like. This is you did not install his environment and therefore do not have these dynamic libraries.
    Solution: by using System.Windows *, on the line, you will find that in fact remove UI, Xaml other fields are the same.
  2. About the event there is no PointerPressed and PointerMoved and so it can be published to the platform but also with the tablet is estimated to optimize them to the event, but do not panic, can read English can not analogy is a mouse-down event and mouse movements events do? Some man in our event on the line.
    Workaround: Use MouseMoved instead of MouseDown and other events the same.
  3. PlayArea_MouseMove function which has a Point statements, is not being given a knock and in accordance with the book! I do not know why this may still be a problem for the environment it! .NET, WPF does not have a few functions. However, under the careful reading can understand PlayArea is to get inside the mouse position, the position of judge of a mouse. Online search the next several ways to get the mouse position WPF it is more appropriate to use this discovery with simple, not to carry out the conversion as the mouse like a book, with e.GetPosition (playArea); directly using the opposite playArea mouse position.
    Solution: Use e.GetPosition ()

The other is no longer a problem, the problem message, if I can help you.

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.Media.Animation;
using System.Windows.Threading;

namespace SaveHuman
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Random random = new Random();
        DispatcherTimer enemyTimer = new DispatcherTimer();
        DispatcherTimer targetTimer = new DispatcherTimer();
        bool humanCaptured = false;
        public MainWindow()
        {
            InitializeComponent();

            enemyTimer.Tick += EnemyTimer_Tick;//2019.10.30 22点21分
            enemyTimer.Interval = TimeSpan.FromSeconds(2);//2秒增加一个敌人

            targetTimer.Tick += TargetTimer_Tick;
            targetTimer.Interval = TimeSpan.FromSeconds(.1);//0.1秒执行一次
        }
        /// <summary>
        /// 进度条计时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TargetTimer_Tick(object sender, EventArgs e)
        {
            progressBar.Value += 1;
            if (progressBar.Value >= progressBar.Maximum)
            {
                EndTheGame();
            }
        }

        /// <summary>
        /// 游戏结束
        /// </summary>
        #region 游戏结束
        private void EndTheGame()
        {
            if (!playArea.Children.Contains(gameoverText))
            {
                enemyTimer.Stop();
                targetTimer.Stop();
                humanCaptured = false;
                starbutton.Visibility = Visibility.Visible;
                playArea.Children.Add(gameoverText);
            }
        }
        #endregion
        /// <summary>
        /// 添加敌人的计时器
        /// </summary>

        private void EnemyTimer_Tick(object sender, EventArgs e)
        {
            AddEnemy();
        }
        /// <summary>
        /// 点击Star开始游戏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Starbutton_Click(object sender, RoutedEventArgs e)
        {
            StarGame();

        }
        /// <summary>
        /// 开始游戏初始化
        /// </summary>
        private void StarGame()
        {
            human.IsHitTestVisible = true;
            humanCaptured = false;
            progressBar.Value = 0;
            starbutton.Visibility = Visibility.Collapsed;
            playArea.Children.Clear();
            playArea.Children.Add(target);
            playArea.Children.Add(human);
            enemyTimer.Start();
            targetTimer.Start();
        }
        /// <summary>
        /// 添加敌人
        /// </summary>
        private void AddEnemy()
        {
            ContentControl enemy = new ContentControl();
            enemy.Template = Resources["EnemyTemplate"] as ControlTemplate;
            AnimateEnemy(enemy, 0, playArea.ActualWidth - 100, "(Canvas.Left)");
            AnimateEnemy(enemy, random.Next((int)playArea.ActualHeight - 100), random.Next((int)playArea.ActualHeight - 100), "(Canvas.Top)");
            playArea.Children.Add(enemy);

            enemy.MouseEnter += Enemy_MouseEnter;
        }
        /// <summary>
        /// 鼠标进入敌人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Enemy_MouseEnter(object sender, MouseEventArgs e)
        {
            if (humanCaptured)
            {
                EndTheGame();
            }
        }
        /// <summary>
        /// 动画函数
        /// </summary>
        /// <param name="enemy"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="propertyToAnimate"></param>
        private void AnimateEnemy(ContentControl enemy, double from, double to, string propertyToAnimate)
        {
            Storyboard storyboard = new Storyboard() { AutoReverse = true, RepeatBehavior = RepeatBehavior.Forever };
            DoubleAnimation animation = new DoubleAnimation()
            {
                From = from,
                To = to,
                Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)))
            };
            Storyboard.SetTarget(animation, enemy);
            Storyboard.SetTargetProperty(animation, new PropertyPath(propertyToAnimate));
            storyboard.Children.Add(animation);
            storyboard.Begin();
        }

        
        /// <summary>
        /// 人类是否到达目的地的判断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Target_MouseEnter(object sender, MouseEventArgs e)
        {
            if(targetTimer.IsEnabled && humanCaptured)
            {
                progressBar.Value = 0;
                Canvas.SetLeft(target, random.Next(100, (int)playArea.ActualWidth - 100));
                Canvas.SetTop(target, random.Next(100, (int)playArea.ActualHeight - 100));
                Canvas.SetLeft(human, random.Next(100, (int)playArea.ActualWidth - 100));
                Canvas.SetTop(human, random.Next(100, (int)playArea.ActualHeight - 100));
                humanCaptured = false;
                human.IsHitTestVisible = true;

            }
        }
        /// <summary>
        /// 鼠标在游戏区域移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PlayArea_MouseMove(object sender, MouseEventArgs e)
        {
            if (humanCaptured)
            {
                //获取鼠标相对于palyArea的位置,无需书上的转换这是wpf与windows Store的不同
                //对于这个不理解可以把playArea改为null试试看看区别就晓得了
                Point pointerProsition = e.GetPosition(playArea);

                
                if((Math.Abs(pointerProsition.X-Canvas.GetLeft(human))>human.ActualWidth*3) || (Math.Abs(pointerProsition.Y - Canvas.GetTop(human)) > human.ActualHeight * 3))
                {
                    humanCaptured = false;
                    human.IsHitTestVisible = true;

                }
                else
                {
                    Canvas.SetLeft(human, pointerProsition.X - human.ActualWidth / 2);
                    Canvas.SetTop(human, pointerProsition.Y - human.ActualHeight / 2);

                }
            }
        }
        /// <summary>
        /// 鼠标拖着人类离开游戏区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PlayArea_MouseLeave(object sender, MouseEventArgs e)
        {
            if (humanCaptured)
            {
                EndTheGame();
            }
        }

        /// <summary>
        /// 鼠标左键点击人类的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Human_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (enemyTimer.IsEnabled)
            {
                humanCaptured = true;
                human.IsHitTestVisible = false;
                //Console.WriteLine("鼠标按下选中状态:" + humanCaptured);

            }
        }

        
    }
}

If they work hard nothing can be stumped you, come on in show!

-------------------------------- Bhotidag_ismen
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/botinghub/p/11801484.html