《Head First C#》外星人入侵WPF编写源码

2022-10-13,,,,

目录

@(外星人入侵(wpf编写))

引言

自学的c#,看了几本教材讲的都是程序代码,网上找的也有视屏,但都比较老了。只会打些代码为不晓得为什么要这样打感觉很别扭,在朋友的推荐下先选择了这本《head first c#》先大致的了解下c#。
这本书的第一章我就遇见了问题,要装ide选的是vs2012 for win8的,这完全和我的系统不配我是用的vs2017+win10,想着应该是一样的,但是没有找到windows store,我寻思着估计我只安装了for .net没有安装windows平台通用的那个环境,要是选择在安装那个环境的话12g空间,懒得装。书上说明了使用wpf可以打大部分代码,那就将就着用吧!书上给了参考pdf的下载网址可是用梯子也连不上,打了前缀发现进了他们公司的主站。估计网址换了把,csdn上也没找到那个pdf资源。自己瞎琢磨着来吧,这个博客是个记录,希望对你有用。

前期工作

  1. 打开vs,创建wpf窗体;
  2. 照书上一步一步来搭建界面,基本一样
  3. 搭建好了我们就来写代码,代码里面有点不一样我会说明的

    代码编写

注意事项:

  1. 引用的地方:这里你会发现没有using windows.ui.xaml;之类的引用。这是你没安装他的环境因而没有这些动态库。
    解决方法:用using system.windows.*,就行,你会发现其实去掉ui,xaml其他字段都一样。
  2. 关于事件里面没有pointerpressed和pointermoved等等这个它可以发布到平台上平板也要用估计是他们把事件给优化了,不过不用慌,看得懂英文的就可以类推不就是个鼠标按压事件和鼠标移动事件吗?照着我们有的事件来就行。
    解决方法:使用mousedown和mousemoved代替其他事件一样。
  3. playarea_mousemove函数里面有个point的语句,是不是按照书上敲又报错了!这个我也不晓得为啥给可能还是环境的问题吧!.net的wpf确实没有那几个函数。不过仔细读下就可以理解是获取playarea里面的鼠标位置,进行鼠标位置判断的。网上查了下wpf获取鼠标位置的几种方法这发现使用这种更合适跟简单,还不用像书上那样进行鼠标的转换,用e.getposition(playarea);直接就使用的是相对与playarea的鼠标位置。
    解决办法:使用e.getposition()

其他的倒没有什么问题,有问题留言,如果我可以帮助你的话。

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

            }
        }

        
    }
}

只要努力没有什么困难可以难倒你,加油骚年!

————————————————————————————————boting_isme

《《Head First C#》外星人入侵WPF编写源码.doc》

下载本文的Word格式文档,以方便收藏与打印。