【WPF学习】第五十七章 使用代码创建故事板
在“【wpf学习】第五十章 故事板”中讨论了如何使用代码创建简单动画,以及如何使用xaml标记构建更复杂的故事板——具有多个动画以及播放控制功能。但有时采用更复杂的故事板例程,并在代码中实现全部复杂功能是合理的。实际上,这种情况十分常见。当需要处理多个动画并且预先不知道将有多少个动画或不知道如何配置动画时,就会遇到这种情况。如果希望在不同的窗口中使用相同的动画,或者只是希望从标记中灵活地分离出所有与动画相关的细节以以方便重用,也会遇到这种情况。
通过编写代码创建、配置和启动故事板并不难。只需要创建动画和故事板对象,并将动画添加到故事板中,然后启动故事板即可。在动画结束后可响应storyboard.completed事件以执行所有清理工作。
在接下来的示例中,将看到如何创建下图中显示的游戏。在该例中,投下的一系列炸弹的速度始终不断增加。玩家必须单击每个炸弹以逐一拆除。当达到设置的极限时——默认情况下是落下5个炸弹——游戏i结束。
在这个示例中,投下的每颗炸弹都有自己的包含两个动画的故事板。第一个动画使炸弹下落(通过为canvas.top属性应用动画),而第二个动画稍微前后旋转炸弹,使其就有逼真的摆动效果。如果用户单击一颗下落的炸弹,这些动画就会停止,并且会发生另外两个动画,使炸弹倾斜,悄然间离开canvas面板的侧边。最后,每次结束一个动画,应用程序都会进行检查,一查看该动画是标示炸弹被拆除了还是落下了,并相应地更新计数。
一、创建主窗口
bombdropper示例中的主窗口十分简单。它包含一个具有两列的grid面板。在左侧是一个border元素,该border元素包含标示游戏表面的canvas面板:
<border borderbrush="steelblue" borderthickness="1" margin="5"> <grid> <canvas x:name="canvasbackground" sizechanged="canvasbackground_sizechanged" minwidth="50"> <canvas.background> <radialgradientbrush> <gradientstop color="aliceblue" offset="0"></gradientstop> <gradientstop color="white" offset="0.7"></gradientstop> </radialgradientbrush> </canvas.background> </canvas> </grid> </border>
当第一次为canvas面板设置尺寸或改变尺寸时(当用户改变窗口的尺寸时),会运行下面的代码变设置裁剪区域:
private void canvasbackground_sizechanged(object sender, sizechangedeventargs e) { rectanglegeometry rect = new rectanglegeometry(); rect.rect = new rect(0, 0, canvasbackground.actualwidth, canvasbackground.actualheight); canvasbackground.clip = rect; }
这是需要的,否则即使canvas面板的子元素位于显示区域外,canvas面板也会绘制其子元素。在投弹游戏中,这会导致炸弹飞出勾勒的canvas面板轮廓的方框。
在主窗口的右侧是一个面板,该面板显示游戏统计信息、当前拆除的炸弹数量和落下的炸弹数量,以及一个用于开始游戏的按钮:
<border grid.column="1" borderbrush="steelblue" borderthickness="1" margin="5"> <border.background> <radialgradientbrush gradientorigin="1,0.7" center="1,0.7" radiusx="1" radiusy="1"> <gradientstop color="orange" offset="0"></gradientstop> <gradientstop color="white" offset="1"></gradientstop> </radialgradientbrush> </border.background> <stackpanel margin="15" verticalalignment="center" horizontalalignment="center"> <textblock fontfamily="impact" fontsize="35" foreground="lightsteelblue">bomb dropper</textblock> <textblock x:name="lblrate" margin="0,30,0,0" textwrapping="wrap" fontfamily="georgia" fontsize="14"></textblock> <textblock x:name="lblspeed" margin="0,30" textwrapping="wrap" fontfamily="georgia" fontsize="14"></textblock> <textblock x:name="lblstatus" textwrapping="wrap" fontfamily="georgia" fontsize="20">no bombs have dropped.</textblock> <button x:name="cmdstart" padding="5" margin="0,30" width="80" content="start game" click="cmdstart_click"></button> </stackpanel> </border>
二、创建bomb用户控件
下一步创建炸弹的图形划线。尽管可使用静态图像(只要具有透明的背景就可以),但使用更灵活的wpf形状总是更好一些。通过使用形状,当改变炸弹尺寸时不会导致变形,并且可以修改图画中的单个部分或为其应用动画。这个动画中显示的炸弹是直接从miscrosoft word的在线剪贴画集合绘制的。炸弹剪贴画被转换为xaml,具体方法是首先将剪贴画插入到word文档中,然后将word文档保存为xps文件。
用于bomb类的xaml稍微做了简化(删除不必要的用于包含炸弹的额外canvas元素以及用于缩放炸弹的变换)。然后将xaml插入到新的名为bomb的用户控件中,通过这种方法,住页面可通过创建bomb用户控件并将其添加到布局容器(如canvas面板)中来显示炸弹。
在单独的用户控件中放置图形,可是的在用户界面中实例化该图形的多个副本更加容易。还可通过为用户控件添加代码来封装相关的功能。在投弹示例中,只需要为代码添加一个细节——跟踪炸弹当时是否正在下落的boolean属性:
public partial class bomb : usercontrol { public bomb() { initializecomponent(); } public bool isfalling { get; set; } }
用于炸弹的标记包含rotatetransform变换,动画代码使用该变换为下落中的炸弹应用摆动效果。尽管可通过编写代码创建并添加这个rotatetransform变换,但在炸弹的xaml文件中定义该变换更加合理:
<usercontrol x:class="bombdropper.bomb" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <usercontrol.rendertransform> <transformgroup> <rotatetransform angle="20" centerx="50" centery="50"></rotatetransform> <scaletransform scalex="0.5" scaley="0.5"></scaletransform> </transformgroup> </usercontrol.rendertransform> <canvas> <path data="m 11.989,50.026 l 24.381,37.08 l 19.097,53.862 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 0.46098,31.997 l 17.945,28.449 l 4.1114,39.19 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 9.9713,7.3517 l 22.075,20.49 l 5.7445,14.16 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 58.484,29.408 l 40.712,31.997 l 57.523,37.367 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 51.663,10.229 l 38.694,22.408 l 55.506,17.325 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 32.354,0.25535 l 31.682,18.092 l 40.039,2.7487 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 105.84,186.87 l 110.84,183.99 l 115.45,180.64 l 119.58,176.9 l 123.33,172.77 l 126.69,168.36 l 129.47,163.76 l 131.88,158.87 l 133.8,153.79 l 135.14,148.51 l 136.01,143.14 l 136.39,137.77 l 136.3,132.21 l 135.53,126.74 l 134.28,121.37 l 132.45,116 l 130.05,110.73 l 128.61,108.14 l 127.17,105.74 l 125.54,103.34 l 123.81,101.14 l 121.98,99.029 l 120.06,97.015 l 118.04,95.097 l 115.93,93.275 l 113.82,91.549 l 111.61,89.919 l 109.3,88.481 l 106.9,87.138 l 104.5,85.891 l 102,84.741 l 99.503,83.782 l 96.909,82.823 l 94.316,82.055 l 91.722,81.48 l 89.032,81.001 l 86.342,80.617 l 83.556,80.329 l 80.867,80.233 l 78.081,80.233 l 75.391,80.329 l 72.605,80.617 l 69.915,81.096 l 67.129,81.672 l 64.44,82.343 l 61.75,83.206 l 59.06,84.165 l 56.37,85.316 l 53.777,86.563 l 48.781,89.44 l 44.17,92.796 l 40.039,96.536 l 36.293,100.66 l 33.027,104.97 l 30.145,109.67 l 27.743,114.56 l 25.918,119.65 l 24.477,124.83 l 23.612,130.2 l 23.324,135.66 l 23.42,141.13 l 24.189,146.59 l 25.438,152.06 l 27.263,157.43 l 29.664,162.7 l 31.105,165.29 l 32.546,167.69 l 34.179,170.09 l 35.909,172.29 l 37.734,174.4 l 39.655,176.42 l 41.672,178.34 l 43.69,180.16 l 45.899,181.88 l 48.109,183.51 l 50.414,184.95 l 52.72,186.3 l 55.217,187.54 l 57.619,188.69 l 60.213,189.65 l 62.71,190.61 l 65.304,191.38 l 67.994,191.95 l 70.684,192.43 l 73.374,192.82 l 76.063,193.1 l 78.753,193.2 l 81.539,193.2 l 84.325,193.1 l 87.015,192.82 l 89.801,192.34 l 92.49,191.76 l 95.18,191.09 l 97.87,190.23 l 100.56,189.27 l 103.25,188.12 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="f 1 m 125.92,112.84 l 125.92,112.84 l 128.13,117.63 l 129.86,122.62 l 131.01,127.51 l 131.68,132.5 l 131.78,137.68 l 131.4,142.66 l 130.63,147.65 l 129.38,152.44 l 127.65,157.05 l 125.44,161.55 l 122.94,165.77 l 119.77,169.9 l 116.31,173.64 l 112.57,177.09 l 108.34,180.16 l 103.73,182.75 l 107.96,190.99 l 113.34,187.83 l 118.33,184.19 l 122.85,180.16 l 126.88,175.65 l 130.44,170.95 l 133.51,165.97 l 136.1,160.69 l 138.22,155.13 l 139.66,149.38 l 140.62,143.62 l 141,137.87 l 140.91,131.92 l 140.04,125.98 l 138.7,120.13 l 136.78,114.37 l 134.18,108.62 l 134.18,108.62 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="f 1 m 55.89,90.686 l 55.89,90.686 l 58.195,89.44 l 60.693,88.481 l 63.191,87.522 l 65.688,86.754 l 68.09,86.179 l 70.78,85.604 l 73.181,85.124 l 75.679,84.932 l 78.081,84.836 l 80.867,84.836 l 83.268,84.932 l 85.862,85.22 l 88.36,85.508 l 90.857,85.987 l 93.163,86.467 l 95.468,87.138 l 97.87,88.097 l 100.27,88.96 l 102.48,90.015 l 104.79,91.166 l 107,92.412 l 109.01,93.659 l 111.03,95.193 l 112.95,96.728 l 114.97,98.454 l 116.79,100.28 l 118.62,102.1 l 120.25,104.02 l 121.79,106.03 l 123.33,108.14 l 124.67,110.44 l 125.92,112.84 l 134.18,108.62 l 132.55,105.84 l 131.01,103.34 l 129.28,100.66 l 127.36,98.262 l 125.34,95.961 l 123.33,93.755 l 121.12,91.741 l 118.91,89.823 l 116.6,87.905 l 114.2,86.179 l 111.61,84.549 l 109.01,83.11 l 106.52,81.768 l 103.73,80.521 l 101.14,79.466 l 98.35,78.507 l 95.468,77.644 l 92.586,76.973 l 89.704,76.493 l 86.823,76.014 l 83.845,75.726 l 80.867,75.63 l 78.081,75.63 l 75.103,75.726 l 72.029,76.11 l 69.051,76.589 l 66.169,77.165 l 63.191,77.932 l 60.309,78.891 l 57.427,79.85 l 54.545,81.192 l 51.663,82.439 l 51.663,82.439 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="f 1 m 33.795,160.6 l 33.795,160.6 l 31.586,155.8 l 29.857,150.81 l 28.704,145.83 l 28.031,140.84 l 27.935,135.66 l 28.223,130.68 l 28.992,125.78 l 30.337,120.99 l 31.97,116.29 l 34.179,111.88 l 36.773,107.56 l 39.847,103.54 l 43.306,99.796 l 47.052,96.344 l 51.279,93.275 l 55.89,90.686 l 51.663,82.439 l 46.284,85.604 l 41.288,89.248 l 36.773,93.275 l 32.738,97.783 l 29.28,102.39 l 26.11,107.47 l 23.516,112.84 l 21.499,118.3 l 19.962,123.87 l 19.001,129.72 l 18.713,135.66 l 18.809,141.42 l 19.674,147.36 l 21.019,153.31 l 22.94,159.06 l 25.534,164.81 l 25.534,164.81 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="f 1 m 103.73,182.75 l 103.73,182.75 l 101.42,183.99 l 98.927,184.95 l 96.429,185.91 l 93.931,186.68 l 91.53,187.25 l 88.936,187.83 l 86.438,188.31 l 84.037,188.5 l 81.539,188.6 l 78.753,188.6 l 76.352,188.5 l 73.854,188.21 l 71.356,187.93 l 68.859,187.45 l 66.361,186.97 l 64.151,186.3 l 61.846,185.34 l 59.348,184.47 l 57.235,183.42 l 54.833,182.27 l 52.816,181.02 l 50.702,179.77 l 48.685,178.24 l 46.668,176.71 l 44.746,174.98 l 42.921,173.16 l 41.096,171.34 l 39.463,169.42 l 37.926,167.4 l 36.389,165.29 l 35.044,162.99 l 33.795,160.6 l 25.534,164.81 l 27.167,167.6 l 28.704,170.09 l 30.433,172.77 l 32.354,175.17 l 34.372,177.47 l 36.389,179.68 l 38.598,181.69 l 40.712,183.61 l 43.113,185.53 l 45.515,187.25 l 48.013,188.88 l 50.606,190.32 l 53.2,191.67 l 55.89,192.91 l 58.58,193.97 l 61.27,194.93 l 64.248,195.79 l 67.129,196.46 l 70.011,196.94 l 72.893,197.42 l 75.775,197.71 l 78.753,197.8 l 81.539,197.8 l 84.613,197.71 l 87.591,197.32 l 90.665,196.84 l 93.451,196.27 l 96.429,195.5 l 99.311,194.54 l 102.19,193.58 l 105.07,192.24 l 107.96,190.99 l 107.96,190.99 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 105.84,186.87 l 110.84,183.99 l 115.45,180.64 l 119.58,176.9 l 123.33,172.77 l 126.69,168.36 l 129.47,163.76 l 131.88,158.87 l 133.8,153.79 l 135.14,148.51 l 136.01,143.14 l 136.39,137.77 l 136.3,132.21 l 135.53,126.74 l 134.28,121.37 l 132.45,116 l 130.05,110.73 l 128.61,108.14 l 127.17,105.74 l 125.54,103.34 l 123.81,101.14 l 121.98,99.029 l 120.06,97.015 l 118.04,95.097 l 115.93,93.275 l 113.82,91.549 l 111.61,89.919 l 109.3,88.481 l 106.9,87.138 l 104.5,85.891 l 102,84.741 l 99.503,83.782 l 96.909,82.823 l 94.316,82.055 l 91.722,81.48 l 89.032,81.001 l 86.342,80.617 l 83.556,80.329 l 80.867,80.233 l 78.081,80.233 l 75.391,80.329 l 72.605,80.617 l 69.915,81.096 l 67.129,81.672 l 64.44,82.343 l 61.75,83.206 l 59.06,84.165 l 56.37,85.316 l 53.777,86.563 l 48.781,89.44 l 44.17,92.796 l 40.039,96.536 l 36.293,100.66 l 33.027,104.97 l 30.145,109.67 l 27.743,114.56 l 25.918,119.65 l 24.477,124.83 l 23.612,130.2 l 23.324,135.66 l 23.42,141.13 l 24.189,146.59 l 25.438,152.06 l 27.263,157.43 l 29.664,162.7 l 31.105,165.29 l 32.546,167.69 l 34.179,170.09 l 35.909,172.29 l 37.734,174.4 l 39.655,176.42 l 41.672,178.34 l 43.69,180.16 l 45.899,181.88 l 48.109,183.51 l 50.414,184.95 l 52.72,186.3 l 55.217,187.54 l 57.619,188.69 l 60.213,189.65 l 62.71,190.61 l 65.304,191.38 l 67.994,191.95 l 70.684,192.43 l 73.374,192.82 l 76.063,193.1 l 78.753,193.2 l 81.539,193.2 l 84.325,193.1 l 87.015,192.82 l 89.801,192.34 l 92.49,191.76 l 95.18,191.09 l 97.87,190.23 l 100.56,189.27 l 103.25,188.12 z "> <path.fill> <solidcolorbrush color="#ff000000"/> </path.fill> </path> <path data="m 58.195,51.081 l 20.538,70.548 l 18.809,71.507 l 19.674,73.233 l 37.734,107.85 l 38.022,108.52 l 38.791,108.71 l 38.983,108.81 l 39.367,108.91 l 40.039,109.1 l 40.808,109.19 l 41.769,109.39 l 42.921,109.48 l 44.266,109.58 l 45.707,109.67 l 47.34,109.58 l 49.069,109.48 l 50.991,109.19 l 52.912,108.81 l 55.025,108.24 l 57.139,107.56 l 59.444,106.7 l 61.75,105.55 l 64.632,103.92 l 67.129,102.39 l 69.339,100.76 l 71.26,99.221 l 72.989,97.687 l 74.334,96.248 l 75.487,94.906 l 76.448,93.563 l 77.216,92.316 l 77.889,91.262 l 78.273,90.207 l 78.657,89.44 l 78.849,88.672 l 78.945,88.193 l 79.041,87.809 l 79.041,87.617 l 79.041,87.042 l 78.849,86.467 l 60.789,51.848 l 59.925,50.218 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 60.021,102.1 l 58.292,102.96 l 56.562,103.63 l 54.929,104.21 l 53.392,104.69 l 51.855,105.07 l 50.414,105.36 l 49.069,105.55 l 47.724,105.65 l 46.476,105.74 l 45.419,105.74 l 44.362,105.74 l 43.402,105.65 l 42.537,105.55 l 41.865,105.45 l 41.192,105.36 l 40.712,105.26 l 23.997,73.137 l 58.292,55.396 l 75.103,87.713 l 74.815,88.576 l 74.238,89.823 l 73.278,91.357 l 71.933,93.18 l 70.011,95.193 l 67.418,97.399 l 64.151,99.7 z "> <path.fill> <solidcolorbrush color="#ff000000"/> </path.fill> </path> <path data="m 50.222,155.32 l 47.917,150.33 l 46.284,145.16 l 45.323,140.07 l 44.939,134.89 l 45.131,129.81 l 45.995,124.92 l 47.34,120.22 l 49.262,115.72 l 47.532,116 l 45.995,116.19 l 44.458,116.19 l 43.017,116.19 l 41.672,116.1 l 40.424,115.81 l 39.271,115.52 l 38.214,115.24 l 36.101,119.74 l 34.564,124.44 l 33.699,129.43 l 33.315,134.51 l 33.603,139.79 l 34.564,144.96 l 36.197,150.14 l 38.406,155.22 l 40.135,158.2 l 41.961,160.98 l 44.074,163.66 l 46.284,166.06 l 48.589,168.27 l 51.087,170.28 l 53.68,172.1 l 56.37,173.73 l 59.156,175.17 l 61.942,176.32 l 64.92,177.28 l 67.898,178.05 l 70.876,178.53 l 73.854,178.72 l 76.928,178.72 l 79.906,178.53 l 77.696,177.95 l 75.391,177.28 l 73.181,176.51 l 71.068,175.55 l 68.955,174.5 l 66.937,173.35 l 64.92,172.01 l 62.903,170.57 l 61.077,169.03 l 59.252,167.4 l 57.523,165.68 l 55.89,163.76 l 54.353,161.84 l 52.816,159.73 l 51.471,157.62 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 102.87,110.63 l 98.35,116.29 l 98.734,116.87 l 99.215,117.54 l 99.599,118.11 l 100.08,118.78 l 100.46,119.36 l 100.85,120.03 l 101.14,120.61 l 101.52,121.28 l 101.81,121.76 l 102,122.24 l 102.29,122.72 l 102.48,123.2 l 104.02,122.72 l 108.15,120.7 l 107.67,119.26 l 107.09,117.92 l 106.42,116.58 l 105.84,115.33 l 105.07,114.09 l 104.4,112.84 l 103.63,111.69 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 116.12,113.8 l 115.35,112.45 l 114.59,111.02 l 113.72,109.77 l 112.95,108.43 l 111.99,107.18 l 111.03,105.93 l 110.07,104.69 l 109.11,103.54 l 105.27,108.24 l 106.13,109.39 l 107,110.63 l 107.86,111.98 l 108.63,113.32 l 109.4,114.66 l 110.07,116.19 l 110.74,117.63 l 111.32,119.26 l 117.37,116.48 l 117.08,115.81 l 116.79,115.14 l 116.51,114.47 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 101.71,104.21 l 105.36,99.7 l 104.02,98.742 l 102.58,97.783 l 101.14,96.919 l 99.599,96.056 l 98.062,95.289 l 96.525,94.618 l 94.988,93.947 l 93.355,93.275 l 90.473,98.837 l 91.722,99.221 l 93.067,99.796 l 94.508,100.28 l 95.853,100.95 l 97.294,101.62 l 98.734,102.39 l 100.27,103.25 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 88.744,102.29 l 85.574,108.43 l 86.823,108.81 l 88.167,109.29 l 89.416,109.77 l 90.569,110.35 l 91.818,110.92 l 92.971,111.59 l 94.123,112.26 l 95.18,113.03 l 99.695,107.37 l 98.254,106.41 l 96.813,105.65 l 95.372,104.88 l 94.027,104.21 l 92.586,103.63 l 91.338,103.15 l 89.993,102.67 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 112.95,97.974 l 114.39,99.413 l 115.83,100.95 l 117.18,102.48 l 118.43,104.11 l 119.68,105.84 l 120.93,107.66 l 121.98,109.48 l 123.04,111.4 l 125.15,116.1 l 126.79,120.89 l 127.94,125.69 l 128.61,130.58 l 128.71,135.47 l 128.42,140.36 l 127.55,145.06 l 126.4,149.76 l 124.67,154.27 l 122.56,158.68 l 119.96,162.8 l 117.08,166.73 l 113.72,170.38 l 109.97,173.73 l 105.94,176.71 l 101.42,179.29 l 97.966,180.93 l 94.508,182.27 l 90.953,183.32 l 87.399,184.09 l 83.845,184.67 l 80.29,184.95 l 76.64,184.95 l 73.085,184.67 l 69.627,184.19 l 66.169,183.51 l 62.71,182.56 l 59.348,181.31 l 56.082,179.87 l 53.008,178.24 l 49.934,176.32 l 47.052,174.21 l 50.03,176.8 l 53.104,179.1 l 56.37,181.12 l 59.732,182.94 l 63.287,184.47 l 66.841,185.72 l 70.588,186.68 l 74.334,187.45 l 78.177,187.83 l 82.019,188.02 l 85.958,187.83 l 89.801,187.35 l 93.643,186.58 l 97.486,185.53 l 101.23,184.09 l 104.98,182.36 l 109.49,179.77 l 113.53,176.8 l 117.27,173.45 l 120.64,169.8 l 123.52,165.87 l 126.11,161.75 l 128.23,157.33 l 129.86,152.83 l 131.11,148.13 l 131.88,143.33 l 132.26,138.44 l 132.07,133.55 l 131.49,128.66 l 130.34,123.87 l 128.71,119.07 l 126.59,114.37 l 125.25,111.98 l 123.81,109.67 l 122.17,107.47 l 120.54,105.36 l 118.72,103.34 l 116.89,101.43 l 114.97,99.605 z "> <path.fill> <solidcolorbrush color="#ff383838"/> </path.fill> </path> <path data="m 52.624,63.739 l 69.243,95.865 l 69.723,95.385 l 70.203,95.002 l 70.588,94.522 l 71.068,94.138 l 71.452,93.659 l 71.74,93.275 l 72.125,92.892 l 72.413,92.508 l 56.466,61.725 z "> <path.fill> <solidcolorbrush color="#ff383838"/> </path.fill> </path> <path data="m 47.628,66.328 l 64.728,99.317 l 65.4,98.837 l 66.073,98.358 l 66.649,97.974 l 67.225,97.495 l 50.318,64.89 z "> <path.fill> <solidcolorbrush color="#ff383838"/> </path.fill> </path> <path data="m 44.554,70.26 l 46.187,69.397 l 47.724,68.438 l 49.262,67.575 l 50.702,66.616 l 52.143,65.657 l 53.392,64.698 l 54.641,63.739 l 55.794,62.78 l 56.851,61.821 l 57.811,60.862 l 58.676,59.903 l 59.444,58.944 l 60.117,58.081 l 60.693,57.218 l 61.173,56.355 l 61.462,55.492 l 61.75,54.245 l 61.75,53.19 l 61.558,52.327 l 61.27,51.656 l 61.077,51.368 l 60.885,51.081 l 60.597,50.697 l 60.309,50.409 l 59.925,50.122 l 59.444,49.834 l 58.868,49.546 l 58.292,49.355 l 57.427,49.163 l 56.466,48.971 l 55.41,48.875 l 54.257,48.971 l 53.008,48.971 l 51.759,49.163 l 50.414,49.45 l 49.069,49.738 l 47.532,50.122 l 46.091,50.601 l 44.554,51.081 l 42.921,51.656 l 41.384,52.327 l 39.751,52.999 l 38.022,53.766 l 36.389,54.629 l 34.564,55.588 l 32.642,56.739 l 30.913,57.794 l 29.184,58.944 l 27.551,60.191 l 26.014,61.342 l 24.573,62.588 l 23.228,63.835 l 22.075,65.082 l 21.115,66.328 l 20.346,67.575 l 19.674,68.822 l 19.29,69.972 l 19.193,71.123 l 19.29,72.178 l 19.674,73.233 l 19.866,73.521 l 20.058,73.808 l 20.346,74.096 l 20.634,74.384 l 21.115,74.767 l 21.595,75.055 l 22.075,75.343 l 22.748,75.534 l 23.612,75.726 l 24.573,75.918 l 25.63,75.918 l 26.686,75.918 l 27.935,75.822 l 29.184,75.63 l 30.529,75.439 l 31.97,75.151 l 33.411,74.767 l 34.852,74.288 l 36.389,73.808 l 38.022,73.233 l 39.559,72.562 l 41.192,71.89 l 42.921,71.123 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 23.132,71.411 l 23.132,71.219 l 23.132,71.027 l 23.132,70.836 l 23.228,70.548 l 23.805,69.397 l 24.765,68.055 l 26.11,66.616 l 27.839,64.986 l 29.857,63.26 l 32.354,61.534 l 35.14,59.807 l 38.214,58.081 l 39.847,57.314 l 41.384,56.547 l 42.921,55.876 l 44.458,55.3 l 45.803,54.725 l 47.244,54.245 l 48.589,53.862 l 49.838,53.478 l 50.991,53.286 l 52.143,52.999 l 53.2,52.903 l 54.161,52.807 l 55.025,52.807 l 55.794,52.807 l 56.466,52.903 l 57.043,52.999 l 57.331,53.095 l 57.523,53.19 l 57.715,53.382 l 57.811,53.478 l 57.907,53.766 l 57.811,54.149 l 57.619,54.629 l 57.331,55.204 l 56.947,55.971 l 56.37,56.643 l 55.602,57.506 l 54.833,58.369 l 53.873,59.328 l 52.72,60.287 l 51.471,61.342 l 50.03,62.397 l 48.397,63.451 l 46.668,64.602 l 44.746,65.657 l 42.729,66.808 l 41.096,67.575 l 39.559,68.342 l 38.022,69.013 l 36.485,69.589 l 35.14,70.164 l 33.699,70.644 l 32.354,71.027 l 31.105,71.315 l 29.953,71.603 l 28.8,71.794 l 27.743,71.986 l 26.783,72.082 l 25.918,72.082 l 25.149,72.082 l 24.477,71.986 l 23.901,71.794 l 23.612,71.699 l 23.42,71.603 l 23.228,71.507 z "> <path.fill> <solidcolorbrush color="#ff000000"/> </path.fill> </path> <path data="m 41.769,58.752 l 40.52,57.698 l 39.079,56.451 l 37.542,54.917 l 36.197,53.286 l 35.044,51.56 l 34.179,49.93 l 33.891,48.396 l 34.083,46.957 l 35.332,43.697 l 36.101,41.012 l 36.389,38.614 l 36.197,36.504 l 35.62,34.586 l 34.66,32.86 l 33.507,31.038 l 32.066,29.12 l 31.586,28.545 l 31.105,27.874 l 30.625,27.298 l 30.145,26.627 l 30.145,26.627 l 29.568,26.148 l 28.896,25.86 l 28.127,25.86 l 27.455,26.243 l 26.975,26.819 l 26.686,27.49 l 26.686,28.257 l 26.975,28.929 l 26.975,28.929 l 27.551,29.6 l 28.031,30.271 l 28.512,30.942 l 28.992,31.518 l 30.241,33.052 l 31.201,34.491 l 31.97,35.833 l 32.354,37.272 l 32.45,38.806 l 32.162,40.628 l 31.586,42.834 l 30.529,45.423 l 29.953,48.204 l 30.433,51.081 l 31.778,53.766 l 33.603,56.259 l 35.524,58.369 l 37.446,60.095 l 38.791,61.342 l 39.463,61.821 l 39.463,61.821 l 40.135,62.205 l 40.904,62.205 l 41.576,61.917 l 42.153,61.438 l 42.537,60.766 l 42.537,59.999 l 42.249,59.328 l 41.769,58.752 z "> <path.fill> <solidcolorbrush color="#ffff1900"/> </path.fill> </path> <path data="m 59.925,139.59 l 60.597,130.2 l 27.167,127.61 l 26.398,137 z "> <path.fill> <solidcolorbrush color="#ff000000"/> </path.fill> </path> </canvas> </usercontrol>
通过使用上述代码,可使用<bomb:bomb>元素将炸弹插入到窗口中,就像为主窗口插入title用户控件一样。然而,在这个示例中以编程方式创建炸弹更加合理。
三、投弹
为了投弹,应用程序使用dispatchertimer,这是一种能很好地用于wpf用户界面的计时器,因为它在用户界面线程触发事件。选择事件间隔,此后dispatchertime会在该时间间隔内引发周期性的tick事件。
// fires events on the user interface thread. private dispatchertimer bombtimer = new dispatchertimer(); public mainwindow() { initializecomponent(); bombtimer.tick += bombtimer_tick; }
在bombdropper游戏中,计时器最初被设置为每隔1.3秒引发一次。当用户单击按钮开始游戏时,计时器随之启动:
// keep track of how many are dropped and stopped. private int droppedcount = 0; private int savedcount = 0; // initially, bombs fall every 1.3 seconds, and hit the ground after 3.5 seconds. private double initialsecondsbetweenbombs = 1.3; private double initialsecondstofall = 3.5; private double secondsbetweenbombs; private double secondstofall; // start the game. private void cmdstart_click(object sender, routedeventargs e) { cmdstart.isenabled = false; // reset the game. droppedcount = 0; savedcount = 0; secondsbetweenbombs = initialsecondsbetweenbombs; secondstofall = initialsecondstofall; // start bomb dropping events. bombtimer.interval = timespan.fromseconds(secondsbetweenbombs); bombtimer.start(); }
每次引发计时器事件时,代码创建一个新的bomb对象并设置其再canvas面板上的位置。炸弹放在canvas面板的顶部边缘,使其可以无缝地落入试图。炸弹的水平位置是随机的,位于canvas面板的左侧和右侧之间:
// drop a bomb. private void bombtimer_tick(object sender, eventargs e) { // create the bomb. bomb bomb = new bomb(); bomb.isfalling = true; // position the bomb. random random = new random(); bomb.setvalue(canvas.leftproperty, (double)(random.next(0, (int)(canvasbackground.actualwidth - 50)))); bomb.setvalue(canvas.topproperty, -100.0); ... }
然后代码动态创建故事板为炸弹应用动画。这里使用例两个动画:一个动画通过修改canvas.top附加属性使炸弹下落,另一个动画通过修改旋转变换的角度使炸弹摆动。因为storyboard.targetelement和storyboard.targetproperty是附加属性,所以必须使用storyboard.settargetelement和storyboard.settargetproperty()方法设置它们:
// attach mouse click event (for defusing the bomb). bomb.mouseleftbuttondown += bomb_mouseleftbuttondown; // create the animation for the falling bomb. storyboard storyboard = new storyboard(); doubleanimation fallanimation = new doubleanimation(); fallanimation.to = canvasbackground.actualheight; fallanimation.duration = timespan.fromseconds(secondstofall); storyboard.settarget(fallanimation, bomb); storyboard.settargetproperty(fallanimation, new propertypath("(canvas.top)")); storyboard.children.add(fallanimation); // create the animation for the bomb "wiggle." doubleanimation wiggleanimation = new doubleanimation(); wiggleanimation.to = 30; wiggleanimation.duration = timespan.fromseconds(0.2); wiggleanimation.repeatbehavior = repeatbehavior.forever; wiggleanimation.autoreverse = true; storyboard.settarget(wiggleanimation, ((transformgroup)bomb.rendertransform).children[0]); storyboard.settargetproperty(wiggleanimation, new propertypath("angle")); storyboard.children.add(wiggleanimation);
这两个动画均可使用动画缓动以得到更逼真的行为,但这个示例使用基本的线性动画以使代码保持简单。
新创建的故事板存储在字典集合中,从而可以在其他事件处理程序中很容易地检索故事板。字典集合存储为主窗口类的一个字段:
// make it possible to look up a storyboard based on a bomb. private dictionary<bomb, storyboard> storyboards = new dictionary<bomb, storyboard>();
下面的代码将故事板添加到跟踪集合中:
... // add the storyboard to the tracking collection. storyboards.add(bomb, storyboard); ...
接下来,关联当故事板结束fallanimation动画时进行响应的事件处理程序,当炸弹落地时fallanimation动画结束。最后,启动股市板并执行动画:
// configure and start the storyboard. storyboard.duration = fallanimation.duration; storyboard.completed += storyboard_completed; storyboard.begin();
用于投弹的代码还需要最后一个细节。随着游戏的进行,游戏难度加大。更频繁地引发计时器事件,从而炸弹之间的距离越来越近,并且介绍了下落时间。为实现这些变化,每经过一定的时间间隔就调整一次计时器代码。默认情况下,bombdropper每隔15秒调整一次。下面是控制调整的字段:
// "adjustments" happen periodically, increasing the speed of bomb // falling and shortening the time between bombs. private datetime lastadjustmenttime = datetime.minvalue; // perform an adjustment every 15 seconds. private double secondsbetweenadjustments = 15; // after every adjustment, shave 0.1 seconds off both. private double secondsbetweenbombsreduction = 0.1; private double secondstofallreduction = 0.1;
下面的代码位于dispatchertime.tick事件处理程序的末尾处,这些代码检查是否需要对计时器进行一次调整,并执行适当的修改:
// perform an "adjustment" when needed. if ((datetime.now.subtract(lastadjustmenttime).totalseconds > secondsbetweenadjustments)) { lastadjustmenttime = datetime.now; secondsbetweenbombs -= secondsbetweenbombsreduction; secondstofall -= secondstofallreduction; // (technically, you should check for 0 or negative values. // however, in practice these won't occur because the game will // always end first.) // set the timer to drop the next bomb at the appropriate time. bombtimer.interval = timespan.fromseconds(secondsbetweenbombs); // update the status message. lblrate.text = string.format("a bomb is released every {0} seconds.", secondsbetweenbombs); lblspeed.text = string.format("each bomb takes {0} seconds to fall.", secondstofall); }
通过上面的代码,这款游戏已经具有以不断增加的速率投弹的功能。不过,游戏仍缺少响应炸弹落下以及被拆除的代码。
四、拦截炸弹
用户通过在炸弹达到canvas面板底部之前单击炸弹来进行拆除。因为每个炸弹都是单独的bomb用户控件实例,所以拦截鼠标单击很容易——需要做的全部工作就是处理mouseleftbuttondown事件,当单击炸弹的任意部分时会引发该事件(但如果单击背景上的某个地方,例如炸弹圈边缘的周围,不会引发该事件)。
当单击炸弹时,第一步是获取适当的炸弹对象,并设置其isfalling属性以指示不再下降(在处理动画完成的事件处理程序中会使用isfalling属性)。
private void bomb_mouseleftbuttondown(object sender, mousebuttoneventargs e) { // get the bomb. bomb bomb = (bomb)sender; bomb.isfalling = false; // get the bomb's current position. storyboard storyboard = storyboards[bomb]; double currenttop = canvas.gettop(bomb); ... }
接下来查找控制炸弹的动画的故事板,从而可以停止动画。为查找故事板,需要在游戏的跟踪集合中查找。当前,wpf没有提供任何查找影响给定元素的动画的标准方式。
// get the bomb's current position. storyboard storyboard = storyboards[bomb]; // stop the bomb from falling. storyboard.stop();
单击炸弹后,使用另一个动画集将炸弹移除屏幕,将炸弹抛向上方、抛向左侧或右侧(屈居于距离哪一侧最近)。尽管可创建全新的故事板以实现该效果,但bombdropper游戏清空用于炸弹的当前故事板并为其添加新动画。处理完毕后,启动新的故事板:
// reuse the existing storyboard, but with new animations. // send the bomb on a new trajectory by animating canvas.top // and canvas.left. storyboard.children.clear(); doubleanimation riseanimation = new doubleanimation(); riseanimation.from = currenttop; riseanimation.to = 0; riseanimation.duration = timespan.fromseconds(2); storyboard.settarget(riseanimation, bomb); storyboard.settargetproperty(riseanimation, new propertypath("(canvas.top)")); storyboard.children.add(riseanimation); doubleanimation slideanimation = new doubleanimation(); double currentleft = canvas.getleft(bomb); // throw the bomb off the closest side. if (currentleft < canvasbackground.actualwidth / 2) { slideanimation.to = -100; } else { slideanimation.to = canvasbackground.actualwidth + 100; } slideanimation.duration = timespan.fromseconds(1); storyboard.settarget(slideanimation, bomb); storyboard.settargetproperty(slideanimation, new propertypath("(canvas.left)")); storyboard.children.add(slideanimation); // start the new animation. storyboard.duration = slideanimation.duration; storyboard.begin();
现在,游戏已经具有足够的代码用于投下炸弹并当用户拆除它们弹出屏幕。然而,为跟踪哪些炸弹被拆了以及那些炸弹落下了,需要响应在动画结束时引发的storyboard.completed事件。
五、统计炸弹和清理工作
正如在前面看到的,bombdropper采用两种方式使用故事板:为下落的炸弹应用动画以及为拆除的炸弹应用动画。可使用不同的事件处理程序处理这些故事板的结束事件,但为使代码保持简单,bombdropper只使用一个事件处理程序。通过检查bomb.isfalling属性来区分爆炸的炸弹和拆除的炸弹。
// end the game at maxdropped. private int maxdropped = 5; private void storyboard_completed(object sender, eventargs e) { clockgroup clockgroup = (clockgroup)sender; // get the first animation in the storyboard, and use it to find the // bomb that's being animated. doubleanimation completedanimation = (doubleanimation)clockgroup.children[0].timeline; bomb completedbomb = (bomb)storyboard.gettarget(completedanimation); // determine if a bomb fell or flew off the canvas after being clicked. if (completedbomb.isfalling) { droppedcount++; } else { savedcount++; } ...
无论采用哪种方式,代码都会接着更新显示测试,指示已经落下的拆除的炸弹数量:
// update the display. lblstatus.text = string.format("you have dropped {0} bombs and saved {1}.", droppedcount, savedcount);
接下来,代码进行检查以查看落下炸弹是否达到了最大值。如果达到了最大值,游戏结束,停止计时器并移除所有炸弹和故事板:
// check if it's game over. if (droppedcount >= maxdropped) { bombtimer.stop(); lblstatus.text += "\r\n\r\ngame over."; // find all the storyboards that are underway. foreach (keyvaluepair<bomb, storyboard> item in storyboards) { storyboard storyboard = item.value; bomb bomb = item.key; storyboard.stop(); canvasbackground.children.remove(bomb); } // empty the tracking collection. storyboards.clear(); // allow the user to start a new game. cmdstart.isenabled = true; } else { storyboard storyboard = (storyboard)clockgroup.timeline; storyboard.stop(); storyboards.remove(completedbomb); canvasbackground.children.remove(completedbomb); }
本例完整xaml标记:
<window x:class="bombdropper.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="500" width="600"> <grid x:name="layoutroot"> <grid.columndefinitions> <columndefinition></columndefinition> <columndefinition width="280"></columndefinition> </grid.columndefinitions> <border borderbrush="steelblue" borderthickness="1" margin="5"> <grid> <canvas x:name="canvasbackground" sizechanged="canvasbackground_sizechanged" minwidth="50"> <canvas.background> <radialgradientbrush> <gradientstop color="aliceblue" offset="0"></gradientstop> <gradientstop color="white" offset="0.7"></gradientstop> </radialgradientbrush> </canvas.background> </canvas> </grid> </border> <border grid.column="1" borderbrush="steelblue" borderthickness="1" margin="5"> <border.background> <radialgradientbrush gradientorigin="1,0.7" center="1,0.7" radiusx="1" radiusy="1"> <gradientstop color="orange" offset="0"></gradientstop> <gradientstop color="white" offset="1"></gradientstop> </radialgradientbrush> </border.background> <stackpanel margin="15" verticalalignment="center" horizontalalignment="center"> <textblock fontfamily="impact" fontsize="35" foreground="lightsteelblue">bomb dropper</textblock> <textblock x:name="lblrate" margin="0,30,0,0" textwrapping="wrap" fontfamily="georgia" fontsize="14"></textblock> <textblock x:name="lblspeed" margin="0,30" textwrapping="wrap" fontfamily="georgia" fontsize="14"></textblock> <textblock x:name="lblstatus" textwrapping="wrap" fontfamily="georgia" fontsize="20">no bombs have dropped.</textblock> <button x:name="cmdstart" padding="5" margin="0,30" width="80" content="start game" click="cmdstart_click"></button> </stackpanel> </border> </grid> </window>
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.animation; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; using system.windows.threading; namespace bombdropper { /// <summary> /// mainwindow.xaml 的交互逻辑 /// </summary> public partial class mainwindow : window { // fires events on the user interface thread. private dispatchertimer bombtimer = new dispatchertimer(); public mainwindow() { initializecomponent(); bombtimer.tick += bombtimer_tick; } private void canvasbackground_sizechanged(object sender, sizechangedeventargs e) { rectanglegeometry rect = new rectanglegeometry(); rect.rect = new rect(0, 0, canvasbackground.actualwidth, canvasbackground.actualheight); canvasbackground.clip = rect; } // "adjustments" happen periodically, increasing the speed of bomb // falling and shortening the time between bombs. private datetime lastadjustmenttime = datetime.minvalue; // perform an adjustment every 15 seconds. private double secondsbetweenadjustments = 15; // initially, bombs fall every 1.3 seconds, and hit the ground after 3.5 seconds. private double initialsecondsbetweenbombs = 1.3; private double initialsecondstofall = 3.5; private double secondsbetweenbombs; private double secondstofall; // after every adjustment, shave 0.1 seconds off both. private double secondsbetweenbombsreduction = 0.1; private double secondstofallreduction = 0.1; // make it possible to look up a storyboard based on a bomb. private dictionary<bomb, storyboard> storyboards = new dictionary<bomb, storyboard>(); // start the game. private void cmdstart_click(object sender, routedeventargs e) { cmdstart.isenabled = false; // reset the game. droppedcount = 0; savedcount = 0; secondsbetweenbombs = initialsecondsbetweenbombs; secondstofall = initialsecondstofall; // start bomb dropping events. bombtimer.interval = timespan.fromseconds(secondsbetweenbombs); bombtimer.start(); } // drop a bomb. private void bombtimer_tick(object sender, eventargs e) { // perform an "adjustment" when needed. if ((datetime.now.subtract(lastadjustmenttime).totalseconds > secondsbetweenadjustments)) { lastadjustmenttime = datetime.now; secondsbetweenbombs -= secondsbetweenbombsreduction; secondstofall -= secondstofallreduction; // (technically, you should check for 0 or negative values. // however, in practice these won't occur because the game will // always end first.) // set the timer to drop the next bomb at the appropriate time. bombtimer.interval = timespan.fromseconds(secondsbetweenbombs); // update the status message. lblrate.text = string.format("a bomb is released every {0} seconds.", secondsbetweenbombs); lblspeed.text = string.format("each bomb takes {0} seconds to fall.", secondstofall); } // create the bomb. bomb bomb = new bomb(); bomb.isfalling = true; // position the bomb. random random = new random(); bomb.setvalue(canvas.leftproperty, (double)(random.next(0, (int)(canvasbackground.actualwidth - 50)))); bomb.setvalue(canvas.topproperty, -100.0); // attach mouse click event (for defusing the bomb). bomb.mouseleftbuttondown += bomb_mouseleftbuttondown; // create the animation for the falling bomb. storyboard storyboard = new storyboard(); doubleanimation fallanimation = new doubleanimation(); fallanimation.to = canvasbackground.actualheight; fallanimation.duration = timespan.fromseconds(secondstofall); storyboard.settarget(fallanimation, bomb); storyboard.settargetproperty(fallanimation, new propertypath("(canvas.top)")); storyboard.children.add(fallanimation); // create the animation for the bomb "wiggle." doubleanimation wiggleanimation = new doubleanimation(); wiggleanimation.to = 30; wiggleanimation.duration = timespan.fromseconds(0.2); wiggleanimation.repeatbehavior = repeatbehavior.forever; wiggleanimation.autoreverse = true; storyboard.settarget(wiggleanimation, ((transformgroup)bomb.rendertransform).children[0]); storyboard.settargetproperty(wiggleanimation, new propertypath("angle")); storyboard.children.add(wiggleanimation); // add the bomb to the canvas. canvasbackground.children.add(bomb); // add the storyboard to the tracking collection. storyboards.add(bomb, storyboard); // configure and start the storyboard. storyboard.duration = fallanimation.duration; storyboard.completed += storyboard_completed; storyboard.begin(); } private void bomb_mouseleftbuttondown(object sender, mousebuttoneventargs e) { // get the bomb. bomb bomb = (bomb)sender; bomb.isfalling = false; // get the bomb's current position. storyboard storyboard = storyboards[bomb]; double currenttop = canvas.gettop(bomb); // stop the bomb from falling. storyboard.stop(); // reuse the existing storyboard, but with new animations. // send the bomb on a new trajectory by animating canvas.top // and canvas.left. storyboard.children.clear(); doubleanimation riseanimation = new doubleanimation(); riseanimation.from = currenttop; riseanimation.to = 0; riseanimation.duration = timespan.fromseconds(2); storyboard.settarget(riseanimation, bomb); storyboard.settargetproperty(riseanimation, new propertypath("(canvas.top)")); storyboard.children.add(riseanimation); doubleanimation slideanimation = new doubleanimation(); double currentleft = canvas.getleft(bomb); // throw the bomb off the closest side. if (currentleft < canvasbackground.actualwidth / 2) { slideanimation.to = -100; } else { slideanimation.to = canvasbackground.actualwidth + 100; } slideanimation.duration = timespan.fromseconds(1); storyboard.settarget(slideanimation, bomb); storyboard.settargetproperty(slideanimation, new propertypath("(canvas.left)")); storyboard.children.add(slideanimation); // start the new animation. storyboard.duration = slideanimation.duration; storyboard.begin(); } // keep track of how many are dropped and stopped. private int droppedcount = 0; private int savedcount = 0; // end the game at maxdropped. private int maxdropped = 5; private void storyboard_completed(object sender, eventargs e) { clockgroup clockgroup = (clockgroup)sender; // get the first animation in the storyboard, and use it to find the // bomb that's being animated. doubleanimation completedanimation = (doubleanimation)clockgroup.children[0].timeline; bomb completedbomb = (bomb)storyboard.gettarget(completedanimation); // determine if a bomb fell or flew off the canvas after being clicked. if (completedbomb.isfalling) { droppedcount++; } else { savedcount++; } // update the display. lblstatus.text = string.format("you have dropped {0} bombs and saved {1}.", droppedcount, savedcount); // check if it's game over. if (droppedcount >= maxdropped) { bombtimer.stop(); lblstatus.text += "\r\n\r\ngame over."; // find all the storyboards that are underway. foreach (keyvaluepair<bomb, storyboard> item in storyboards) { storyboard storyboard = item.value; bomb bomb = item.key; storyboard.stop(); canvasbackground.children.remove(bomb); } // empty the tracking collection. storyboards.clear(); // allow the user to start a new game. cmdstart.isenabled = true; } else { storyboard storyboard = (storyboard)clockgroup.timeline; storyboard.stop(); storyboards.remove(completedbomb); canvasbackground.children.remove(completedbomb); } } } }
<usercontrol x:class="bombdropper.bomb" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <usercontrol.rendertransform> <transformgroup> <rotatetransform angle="20" centerx="50" centery="50"></rotatetransform> <scaletransform scalex="0.5" scaley="0.5"></scaletransform> </transformgroup> </usercontrol.rendertransform> <canvas> <path data="m 11.989,50.026 l 24.381,37.08 l 19.097,53.862 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 0.46098,31.997 l 17.945,28.449 l 4.1114,39.19 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 9.9713,7.3517 l 22.075,20.49 l 5.7445,14.16 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 58.484,29.408 l 40.712,31.997 l 57.523,37.367 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 51.663,10.229 l 38.694,22.408 l 55.506,17.325 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 32.354,0.25535 l 31.682,18.092 l 40.039,2.7487 z "> <path.fill> <solidcolorbrush color="#fff2cc0c"/> </path.fill> </path> <path data="m 105.84,186.87 l 110.84,183.99 l 115.45,180.64 l 119.58,176.9 l 123.33,172.77 l 126.69,168.36 l 129.47,163.76 l 131.88,158.87 l 133.8,153.79 l 135.14,148.51 l 136.01,143.14 l 136.39,137.77 l 136.3,132.21 l 135.53,126.74 l 134.28,121.37 l 132.45,116 l 130.05,110.73 l 128.61,108.14 l 127.17,105.74 l 125.54,103.34 l 123.81,101.14 l 121.98,99.029 l 120.06,97.015 l 118.04,95.097 l 115.93,93.275 l 113.82,91.549 l 111.61,89.919 l 109.3,88.481 l 106.9,87.138 l 104.5,85.891 l 102,84.741 l 99.503,83.782 l 96.909,82.823 l 94.316,82.055 l 91.722,81.48 l 89.032,81.001 l 86.342,80.617 l 83.556,80.329 l 80.867,80.233 l 78.081,80.233 l 75.391,80.329 l 72.605,80.617 l 69.915,81.096 l 67.129,81.672 l 64.44,82.343 l 61.75,83.206 l 59.06,84.165 l 56.37,85.316 l 53.777,86.563 l 48.781,89.44 l 44.17,92.796 l 40.039,96.536 l 36.293,100.66 l 33.027,104.97 l 30.145,109.67 l 27.743,114.56 l 25.918,119.65 l 24.477,124.83 l 23.612,130.2 l 23.324,135.66 l 23.42,141.13 l 24.189,146.59 l 25.438,152.06 l 27.263,157.43 l 29.664,162.7 l 31.105,165.29 l 32.546,167.69 l 34.179,170.09 l 35.909,172.29 l 37.734,174.4 l 39.655,176.42 l 41.672,178.34 l 43.69,180.16 l 45.899,181.88 l 48.109,183.51 l 50.414,184.95 l 52.72,186.3 l 55.217,187.54 l 57.619,188.69 l 60.213,189.65 l 62.71,190.61 l 65.304,191.38 l 67.994,191.95 l 70.684,192.43 l 73.374,192.82 l 76.063,193.1 l 78.753,193.2 l 81.539,193.2 l 84.325,193.1 l 87.015,192.82 l 89.801,192.34 l 92.49,191.76 l 95.18,191.09 l 97.87,190.23 l 100.56,189.27 l 103.25,188.12 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="f 1 m 125.92,112.84 l 125.92,112.84 l 128.13,117.63 l 129.86,122.62 l 131.01,127.51 l 131.68,132.5 l 131.78,137.68 l 131.4,142.66 l 130.63,147.65 l 129.38,152.44 l 127.65,157.05 l 125.44,161.55 l 122.94,165.77 l 119.77,169.9 l 116.31,173.64 l 112.57,177.09 l 108.34,180.16 l 103.73,182.75 l 107.96,190.99 l 113.34,187.83 l 118.33,184.19 l 122.85,180.16 l 126.88,175.65 l 130.44,170.95 l 133.51,165.97 l 136.1,160.69 l 138.22,155.13 l 139.66,149.38 l 140.62,143.62 l 141,137.87 l 140.91,131.92 l 140.04,125.98 l 138.7,120.13 l 136.78,114.37 l 134.18,108.62 l 134.18,108.62 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="f 1 m 55.89,90.686 l 55.89,90.686 l 58.195,89.44 l 60.693,88.481 l 63.191,87.522 l 65.688,86.754 l 68.09,86.179 l 70.78,85.604 l 73.181,85.124 l 75.679,84.932 l 78.081,84.836 l 80.867,84.836 l 83.268,84.932 l 85.862,85.22 l 88.36,85.508 l 90.857,85.987 l 93.163,86.467 l 95.468,87.138 l 97.87,88.097 l 100.27,88.96 l 102.48,90.015 l 104.79,91.166 l 107,92.412 l 109.01,93.659 l 111.03,95.193 l 112.95,96.728 l 114.97,98.454 l 116.79,100.28 l 118.62,102.1 l 120.25,104.02 l 121.79,106.03 l 123.33,108.14 l 124.67,110.44 l 125.92,112.84 l 134.18,108.62 l 132.55,105.84 l 131.01,103.34 l 129.28,100.66 l 127.36,98.262 l 125.34,95.961 l 123.33,93.755 l 121.12,91.741 l 118.91,89.823 l 116.6,87.905 l 114.2,86.179 l 111.61,84.549 l 109.01,83.11 l 106.52,81.768 l 103.73,80.521 l 101.14,79.466 l 98.35,78.507 l 95.468,77.644 l 92.586,76.973 l 89.704,76.493 l 86.823,76.014 l 83.845,75.726 l 80.867,75.63 l 78.081,75.63 l 75.103,75.726 l 72.029,76.11 l 69.051,76.589 l 66.169,77.165 l 63.191,77.932 l 60.309,78.891 l 57.427,79.85 l 54.545,81.192 l 51.663,82.439 l 51.663,82.439 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="f 1 m 33.795,160.6 l 33.795,160.6 l 31.586,155.8 l 29.857,150.81 l 28.704,145.83 l 28.031,140.84 l 27.935,135.66 l 28.223,130.68 l 28.992,125.78 l 30.337,120.99 l 31.97,116.29 l 34.179,111.88 l 36.773,107.56 l 39.847,103.54 l 43.306,99.796 l 47.052,96.344 l 51.279,93.275 l 55.89,90.686 l 51.663,82.439 l 46.284,85.604 l 41.288,89.248 l 36.773,93.275 l 32.738,97.783 l 29.28,102.39 l 26.11,107.47 l 23.516,112.84 l 21.499,118.3 l 19.962,123.87 l 19.001,129.72 l 18.713,135.66 l 18.809,141.42 l 19.674,147.36 l 21.019,153.31 l 22.94,159.06 l 25.534,164.81 l 25.534,164.81 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="f 1 m 103.73,182.75 l 103.73,182.75 l 101.42,183.99 l 98.927,184.95 l 96.429,185.91 l 93.931,186.68 l 91.53,187.25 l 88.936,187.83 l 86.438,188.31 l 84.037,188.5 l 81.539,188.6 l 78.753,188.6 l 76.352,188.5 l 73.854,188.21 l 71.356,187.93 l 68.859,187.45 l 66.361,186.97 l 64.151,186.3 l 61.846,185.34 l 59.348,184.47 l 57.235,183.42 l 54.833,182.27 l 52.816,181.02 l 50.702,179.77 l 48.685,178.24 l 46.668,176.71 l 44.746,174.98 l 42.921,173.16 l 41.096,171.34 l 39.463,169.42 l 37.926,167.4 l 36.389,165.29 l 35.044,162.99 l 33.795,160.6 l 25.534,164.81 l 27.167,167.6 l 28.704,170.09 l 30.433,172.77 l 32.354,175.17 l 34.372,177.47 l 36.389,179.68 l 38.598,181.69 l 40.712,183.61 l 43.113,185.53 l 45.515,187.25 l 48.013,188.88 l 50.606,190.32 l 53.2,191.67 l 55.89,192.91 l 58.58,193.97 l 61.27,194.93 l 64.248,195.79 l 67.129,196.46 l 70.011,196.94 l 72.893,197.42 l 75.775,197.71 l 78.753,197.8 l 81.539,197.8 l 84.613,197.71 l 87.591,197.32 l 90.665,196.84 l 93.451,196.27 l 96.429,195.5 l 99.311,194.54 l 102.19,193.58 l 105.07,192.24 l 107.96,190.99 l 107.96,190.99 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 105.84,186.87 l 110.84,183.99 l 115.45,180.64 l 119.58,176.9 l 123.33,172.77 l 126.69,168.36 l 129.47,163.76 l 131.88,158.87 l 133.8,153.79 l 135.14,148.51 l 136.01,143.14 l 136.39,137.77 l 136.3,132.21 l 135.53,126.74 l 134.28,121.37 l 132.45,116 l 130.05,110.73 l 128.61,108.14 l 127.17,105.74 l 125.54,103.34 l 123.81,101.14 l 121.98,99.029 l 120.06,97.015 l 118.04,95.097 l 115.93,93.275 l 113.82,91.549 l 111.61,89.919 l 109.3,88.481 l 106.9,87.138 l 104.5,85.891 l 102,84.741 l 99.503,83.782 l 96.909,82.823 l 94.316,82.055 l 91.722,81.48 l 89.032,81.001 l 86.342,80.617 l 83.556,80.329 l 80.867,80.233 l 78.081,80.233 l 75.391,80.329 l 72.605,80.617 l 69.915,81.096 l 67.129,81.672 l 64.44,82.343 l 61.75,83.206 l 59.06,84.165 l 56.37,85.316 l 53.777,86.563 l 48.781,89.44 l 44.17,92.796 l 40.039,96.536 l 36.293,100.66 l 33.027,104.97 l 30.145,109.67 l 27.743,114.56 l 25.918,119.65 l 24.477,124.83 l 23.612,130.2 l 23.324,135.66 l 23.42,141.13 l 24.189,146.59 l 25.438,152.06 l 27.263,157.43 l 29.664,162.7 l 31.105,165.29 l 32.546,167.69 l 34.179,170.09 l 35.909,172.29 l 37.734,174.4 l 39.655,176.42 l 41.672,178.34 l 43.69,180.16 l 45.899,181.88 l 48.109,183.51 l 50.414,184.95 l 52.72,186.3 l 55.217,187.54 l 57.619,188.69 l 60.213,189.65 l 62.71,190.61 l 65.304,191.38 l 67.994,191.95 l 70.684,192.43 l 73.374,192.82 l 76.063,193.1 l 78.753,193.2 l 81.539,193.2 l 84.325,193.1 l 87.015,192.82 l 89.801,192.34 l 92.49,191.76 l 95.18,191.09 l 97.87,190.23 l 100.56,189.27 l 103.25,188.12 z "> <path.fill> <solidcolorbrush color="#ff000000"/> </path.fill> </path> <path data="m 58.195,51.081 l 20.538,70.548 l 18.809,71.507 l 19.674,73.233 l 37.734,107.85 l 38.022,108.52 l 38.791,108.71 l 38.983,108.81 l 39.367,108.91 l 40.039,109.1 l 40.808,109.19 l 41.769,109.39 l 42.921,109.48 l 44.266,109.58 l 45.707,109.67 l 47.34,109.58 l 49.069,109.48 l 50.991,109.19 l 52.912,108.81 l 55.025,108.24 l 57.139,107.56 l 59.444,106.7 l 61.75,105.55 l 64.632,103.92 l 67.129,102.39 l 69.339,100.76 l 71.26,99.221 l 72.989,97.687 l 74.334,96.248 l 75.487,94.906 l 76.448,93.563 l 77.216,92.316 l 77.889,91.262 l 78.273,90.207 l 78.657,89.44 l 78.849,88.672 l 78.945,88.193 l 79.041,87.809 l 79.041,87.617 l 79.041,87.042 l 78.849,86.467 l 60.789,51.848 l 59.925,50.218 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 60.021,102.1 l 58.292,102.96 l 56.562,103.63 l 54.929,104.21 l 53.392,104.69 l 51.855,105.07 l 50.414,105.36 l 49.069,105.55 l 47.724,105.65 l 46.476,105.74 l 45.419,105.74 l 44.362,105.74 l 43.402,105.65 l 42.537,105.55 l 41.865,105.45 l 41.192,105.36 l 40.712,105.26 l 23.997,73.137 l 58.292,55.396 l 75.103,87.713 l 74.815,88.576 l 74.238,89.823 l 73.278,91.357 l 71.933,93.18 l 70.011,95.193 l 67.418,97.399 l 64.151,99.7 z "> <path.fill> <solidcolorbrush color="#ff000000"/> </path.fill> </path> <path data="m 50.222,155.32 l 47.917,150.33 l 46.284,145.16 l 45.323,140.07 l 44.939,134.89 l 45.131,129.81 l 45.995,124.92 l 47.34,120.22 l 49.262,115.72 l 47.532,116 l 45.995,116.19 l 44.458,116.19 l 43.017,116.19 l 41.672,116.1 l 40.424,115.81 l 39.271,115.52 l 38.214,115.24 l 36.101,119.74 l 34.564,124.44 l 33.699,129.43 l 33.315,134.51 l 33.603,139.79 l 34.564,144.96 l 36.197,150.14 l 38.406,155.22 l 40.135,158.2 l 41.961,160.98 l 44.074,163.66 l 46.284,166.06 l 48.589,168.27 l 51.087,170.28 l 53.68,172.1 l 56.37,173.73 l 59.156,175.17 l 61.942,176.32 l 64.92,177.28 l 67.898,178.05 l 70.876,178.53 l 73.854,178.72 l 76.928,178.72 l 79.906,178.53 l 77.696,177.95 l 75.391,177.28 l 73.181,176.51 l 71.068,175.55 l 68.955,174.5 l 66.937,173.35 l 64.92,172.01 l 62.903,170.57 l 61.077,169.03 l 59.252,167.4 l 57.523,165.68 l 55.89,163.76 l 54.353,161.84 l 52.816,159.73 l 51.471,157.62 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 102.87,110.63 l 98.35,116.29 l 98.734,116.87 l 99.215,117.54 l 99.599,118.11 l 100.08,118.78 l 100.46,119.36 l 100.85,120.03 l 101.14,120.61 l 101.52,121.28 l 101.81,121.76 l 102,122.24 l 102.29,122.72 l 102.48,123.2 l 104.02,122.72 l 108.15,120.7 l 107.67,119.26 l 107.09,117.92 l 106.42,116.58 l 105.84,115.33 l 105.07,114.09 l 104.4,112.84 l 103.63,111.69 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 116.12,113.8 l 115.35,112.45 l 114.59,111.02 l 113.72,109.77 l 112.95,108.43 l 111.99,107.18 l 111.03,105.93 l 110.07,104.69 l 109.11,103.54 l 105.27,108.24 l 106.13,109.39 l 107,110.63 l 107.86,111.98 l 108.63,113.32 l 109.4,114.66 l 110.07,116.19 l 110.74,117.63 l 111.32,119.26 l 117.37,116.48 l 117.08,115.81 l 116.79,115.14 l 116.51,114.47 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 101.71,104.21 l 105.36,99.7 l 104.02,98.742 l 102.58,97.783 l 101.14,96.919 l 99.599,96.056 l 98.062,95.289 l 96.525,94.618 l 94.988,93.947 l 93.355,93.275 l 90.473,98.837 l 91.722,99.221 l 93.067,99.796 l 94.508,100.28 l 95.853,100.95 l 97.294,101.62 l 98.734,102.39 l 100.27,103.25 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 88.744,102.29 l 85.574,108.43 l 86.823,108.81 l 88.167,109.29 l 89.416,109.77 l 90.569,110.35 l 91.818,110.92 l 92.971,111.59 l 94.123,112.26 l 95.18,113.03 l 99.695,107.37 l 98.254,106.41 l 96.813,105.65 l 95.372,104.88 l 94.027,104.21 l 92.586,103.63 l 91.338,103.15 l 89.993,102.67 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 112.95,97.974 l 114.39,99.413 l 115.83,100.95 l 117.18,102.48 l 118.43,104.11 l 119.68,105.84 l 120.93,107.66 l 121.98,109.48 l 123.04,111.4 l 125.15,116.1 l 126.79,120.89 l 127.94,125.69 l 128.61,130.58 l 128.71,135.47 l 128.42,140.36 l 127.55,145.06 l 126.4,149.76 l 124.67,154.27 l 122.56,158.68 l 119.96,162.8 l 117.08,166.73 l 113.72,170.38 l 109.97,173.73 l 105.94,176.71 l 101.42,179.29 l 97.966,180.93 l 94.508,182.27 l 90.953,183.32 l 87.399,184.09 l 83.845,184.67 l 80.29,184.95 l 76.64,184.95 l 73.085,184.67 l 69.627,184.19 l 66.169,183.51 l 62.71,182.56 l 59.348,181.31 l 56.082,179.87 l 53.008,178.24 l 49.934,176.32 l 47.052,174.21 l 50.03,176.8 l 53.104,179.1 l 56.37,181.12 l 59.732,182.94 l 63.287,184.47 l 66.841,185.72 l 70.588,186.68 l 74.334,187.45 l 78.177,187.83 l 82.019,188.02 l 85.958,187.83 l 89.801,187.35 l 93.643,186.58 l 97.486,185.53 l 101.23,184.09 l 104.98,182.36 l 109.49,179.77 l 113.53,176.8 l 117.27,173.45 l 120.64,169.8 l 123.52,165.87 l 126.11,161.75 l 128.23,157.33 l 129.86,152.83 l 131.11,148.13 l 131.88,143.33 l 132.26,138.44 l 132.07,133.55 l 131.49,128.66 l 130.34,123.87 l 128.71,119.07 l 126.59,114.37 l 125.25,111.98 l 123.81,109.67 l 122.17,107.47 l 120.54,105.36 l 118.72,103.34 l 116.89,101.43 l 114.97,99.605 z "> <path.fill> <solidcolorbrush color="#ff383838"/> </path.fill> </path> <path data="m 52.624,63.739 l 69.243,95.865 l 69.723,95.385 l 70.203,95.002 l 70.588,94.522 l 71.068,94.138 l 71.452,93.659 l 71.74,93.275 l 72.125,92.892 l 72.413,92.508 l 56.466,61.725 z "> <path.fill> <solidcolorbrush color="#ff383838"/> </path.fill> </path> <path data="m 47.628,66.328 l 64.728,99.317 l 65.4,98.837 l 66.073,98.358 l 66.649,97.974 l 67.225,97.495 l 50.318,64.89 z "> <path.fill> <solidcolorbrush color="#ff383838"/> </path.fill> </path> <path data="m 44.554,70.26 l 46.187,69.397 l 47.724,68.438 l 49.262,67.575 l 50.702,66.616 l 52.143,65.657 l 53.392,64.698 l 54.641,63.739 l 55.794,62.78 l 56.851,61.821 l 57.811,60.862 l 58.676,59.903 l 59.444,58.944 l 60.117,58.081 l 60.693,57.218 l 61.173,56.355 l 61.462,55.492 l 61.75,54.245 l 61.75,53.19 l 61.558,52.327 l 61.27,51.656 l 61.077,51.368 l 60.885,51.081 l 60.597,50.697 l 60.309,50.409 l 59.925,50.122 l 59.444,49.834 l 58.868,49.546 l 58.292,49.355 l 57.427,49.163 l 56.466,48.971 l 55.41,48.875 l 54.257,48.971 l 53.008,48.971 l 51.759,49.163 l 50.414,49.45 l 49.069,49.738 l 47.532,50.122 l 46.091,50.601 l 44.554,51.081 l 42.921,51.656 l 41.384,52.327 l 39.751,52.999 l 38.022,53.766 l 36.389,54.629 l 34.564,55.588 l 32.642,56.739 l 30.913,57.794 l 29.184,58.944 l 27.551,60.191 l 26.014,61.342 l 24.573,62.588 l 23.228,63.835 l 22.075,65.082 l 21.115,66.328 l 20.346,67.575 l 19.674,68.822 l 19.29,69.972 l 19.193,71.123 l 19.29,72.178 l 19.674,73.233 l 19.866,73.521 l 20.058,73.808 l 20.346,74.096 l 20.634,74.384 l 21.115,74.767 l 21.595,75.055 l 22.075,75.343 l 22.748,75.534 l 23.612,75.726 l 24.573,75.918 l 25.63,75.918 l 26.686,75.918 l 27.935,75.822 l 29.184,75.63 l 30.529,75.439 l 31.97,75.151 l 33.411,74.767 l 34.852,74.288 l 36.389,73.808 l 38.022,73.233 l 39.559,72.562 l 41.192,71.89 l 42.921,71.123 z "> <path.fill> <solidcolorbrush color="#ff777777"/> </path.fill> </path> <path data="m 23.132,71.411 l 23.132,71.219 l 23.132,71.027 l 23.132,70.836 l 23.228,70.548 l 23.805,69.397 l 24.765,68.055 l 26.11,66.616 l 27.839,64.986 l 29.857,63.26 l 32.354,61.534 l 35.14,59.807 l 38.214,58.081 l 39.847,57.314 l 41.384,56.547 l 42.921,55.876 l 44.458,55.3 l 45.803,54.725 l 47.244,54.245 l 48.589,53.862 l 49.838,53.478 l 50.991,53.286 l 52.143,52.999 l 53.2,52.903 l 54.161,52.807 l 55.025,52.807 l 55.794,52.807 l 56.466,52.903 l 57.043,52.999 l 57.331,53.095 l 57.523,53.19 l 57.715,53.382 l 57.811,53.478 l 57.907,53.766 l 57.811,54.149 l 57.619,54.629 l 57.331,55.204 l 56.947,55.971 l 56.37,56.643 l 55.602,57.506 l 54.833,58.369 l 53.873,59.328 l 52.72,60.287 l 51.471,61.342 l 50.03,62.397 l 48.397,63.451 l 46.668,64.602 l 44.746,65.657 l 42.729,66.808 l 41.096,67.575 l 39.559,68.342 l 38.022,69.013 l 36.485,69.589 l 35.14,70.164 l 33.699,70.644 l 32.354,71.027 l 31.105,71.315 l 29.953,71.603 l 28.8,71.794 l 27.743,71.986 l 26.783,72.082 l 25.918,72.082 l 25.149,72.082 l 24.477,71.986 l 23.901,71.794 l 23.612,71.699 l 23.42,71.603 l 23.228,71.507 z "> <path.fill> <solidcolorbrush color="#ff000000"/> </path.fill> </path> <path data="m 41.769,58.752 l 40.52,57.698 l 39.079,56.451 l 37.542,54.917 l 36.197,53.286 l 35.044,51.56 l 34.179,49.93 l 33.891,48.396 l 34.083,46.957 l 35.332,43.697 l 36.101,41.012 l 36.389,38.614 l 36.197,36.504 l 35.62,34.586 l 34.66,32.86 l 33.507,31.038 l 32.066,29.12 l 31.586,28.545 l 31.105,27.874 l 30.625,27.298 l 30.145,26.627 l 30.145,26.627 l 29.568,26.148 l 28.896,25.86 l 28.127,25.86 l 27.455,26.243 l 26.975,26.819 l 26.686,27.49 l 26.686,28.257 l 26.975,28.929 l 26.975,28.929 l 27.551,29.6 l 28.031,30.271 l 28.512,30.942 l 28.992,31.518 l 30.241,33.052 l 31.201,34.491 l 31.97,35.833 l 32.354,37.272 l 32.45,38.806 l 32.162,40.628 l 31.586,42.834 l 30.529,45.423 l 29.953,48.204 l 30.433,51.081 l 31.778,53.766 l 33.603,56.259 l 35.524,58.369 l 37.446,60.095 l 38.791,61.342 l 39.463,61.821 l 39.463,61.821 l 40.135,62.205 l 40.904,62.205 l 41.576,61.917 l 42.153,61.438 l 42.537,60.766 l 42.537,59.999 l 42.249,59.328 l 41.769,58.752 z "> <path.fill> <solidcolorbrush color="#ffff1900"/> </path.fill> </path> <path data="m 59.925,139.59 l 60.597,130.2 l 27.167,127.61 l 26.398,137 z "> <path.fill> <solidcolorbrush color="#ff000000"/> </path.fill> </path> </canvas> </usercontrol>
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; namespace bombdropper { /// <summary> /// bomb.xaml 的交互逻辑 /// </summary> public partial class bomb : usercontrol { public bomb() { initializecomponent(); } public bool isfalling { get; set; } } }
现在已经完成了bombdropper游戏的代码。然而,可进行诸多改进。例如,可执行如下改进:
- 为炸弹添加爆炸动画效果。这种效果使炸弹周围的火焰闪耀或发射在canvas面板上四处飞溅的炸弹碎片。
- 为背景添加动画。此改进易于实现,可添加精彩的可视化效果。例如,可创建上移的线性渐变,产生移动感,或创建在两种颜色之间过渡的效果。
- 添加深度。实现这一改进比想象得更容易。基本技术是为炸弹设置不同尺寸。更大的炸弹应当具有更高的zindex值,确保大炸弹重叠在小炸弹之上,而且应为大炸弹设置更短的动画时间,从而确保他们下落得更快。还可使炸弹半透明,从而当一个炸弹下落时,仍能看到它背后的其他炸弹。
- 添加音效。可是准确计时的声音效果以强调炸弹爆炸或拆除。
- 使用动画缓动。如果希望炸弹在下落、弹离屏幕时加速,或更自然地摆动,可为此处使用的动画缓动函数。并且,正如所期望的,可使用代码构造缓动函数,就像在xaml中构建缓动函数一样容易。
- 调整参数。可为修改行为提供更多细节(例如,当游戏运行时设置如何修改炸弹运动时间、轨迹以及投放频率的变量),还可插入更多随机因素(例如,使拆除的炸弹以稍有不同的方式弹离canvas面板)
上一篇: 865旗舰2月齐发!细数各厂商新机参数及发布会时间
下一篇: 人参果的价格和你不知道的营养价值