Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Windows.Foundation;
  6. using Windows.Foundation.Collections;
  7. using Windows.UI.Xaml;
  8. using Windows.UI.Xaml.Controls;
  9. using Windows.UI.Xaml.Controls.Primitives;
  10. using Windows.UI.Xaml.Data;
  11. using Windows.UI.Xaml.Input;
  12. using Windows.UI.Xaml.Media;
  13. using Windows.UI.Xaml.Navigation;
  14.  
  15. using Windows.UI.Xaml.Media.Animation;
  16.  
  17. // The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237
  18.  
  19. namespace Save_the_Humans
  20. {
  21. /// <summary>
  22. /// A basic page that provides characteristics common to most applications.
  23. /// </summary>
  24. public sealed partial class MainPage : Save_the_Humans.Common.LayoutAwarePage
  25. {
  26. Random random = new Random();
  27. DispatcherTimer enemyTimer = new DispatcherTimer();
  28. DispatcherTimer targetTimer = new DispatcherTimer();
  29. bool humanCaptured = false;
  30.  
  31. public MainPage()
  32. {
  33. this.InitializeComponent();
  34.  
  35. enemyTimer.Tick += enemyTimer_Tick;
  36. enemyTimer.Interval = TimeSpan.FromSeconds(2);
  37.  
  38. targetTimer.Tick += targetTimer_Tick;
  39. targetTimer.Interval = TimeSpan.FromSeconds(.1);
  40. }
  41.  
  42. void targetTimer_Tick(object sender, object e)
  43. {
  44. progressBar.Value += 1;
  45. if (progressBar.Value >= progressBar.Maximum)
  46. EndTheGame();
  47. }
  48.  
  49. private void EndTheGame()
  50. {
  51. if (!playArea.Children.Contains(gameOverText))
  52. {
  53. enemyTimer.Stop();
  54. targetTimer.Stop();
  55. humanCaptured = false;
  56. startButton.Visibility = Visibility.Visible;
  57. playArea.Children.Add(gameOverText);
  58. }
  59. }
  60.  
  61. private void enemyTimer_Tick(object sender, object e)
  62. {
  63. AddEnemy();
  64. }
  65.  
  66. /// <summary>
  67. /// Populates the page with content passed during navigation. Any saved state is also
  68. /// provided when recreating a page from a prior session.
  69. /// </summary>
  70. /// <param name="navigationParameter">The parameter value passed to
  71. /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
  72. /// </param>
  73. /// <param name="pageState">A dictionary of state preserved by this page during an earlier
  74. /// session. This will be null the first time a page is visited.</param>
  75. protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
  76. {
  77. }
  78.  
  79. /// <summary>
  80. /// Preserves state associated with this page in case the application is suspended or the
  81. /// page is discarded from the navigation cache. Values must conform to the serialization
  82. /// requirements of <see cref="SuspensionManager.SessionState"/>.
  83. /// </summary>
  84. /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
  85. protected override void SaveState(Dictionary<String, Object> pageState)
  86. {
  87. }
  88.  
  89. private void startButton_Click(object sender, RoutedEventArgs e)
  90. {
  91. StartGame();
  92. }
  93.  
  94. private void StartGame()
  95. {
  96. human.IsHitTestVisible = true;
  97. humanCaptured = false;
  98. progressBar.Value = 0;
  99. startButton.Visibility = Visibility.Collapsed;
  100. playArea.Children.Clear();
  101. playArea.Children.Add(target);
  102. playArea.Children.Add(human);
  103. enemyTimer.Start();
  104. targetTimer.Start();
  105. }
  106.  
  107. private void AddEnemy()
  108. {
  109. ContentControl enemy = new ContentControl();
  110. enemy.Template = Resources["EnemyTemplate"] as ControlTemplate;
  111. AnimateEnemy(enemy, 0, playArea.ActualWidth - 100, "(Canvas.Left)");
  112. AnimateEnemy(enemy, random.Next((int)playArea.ActualHeight - 100),
  113. random.Next((int)playArea.ActualHeight - 100), "(Canvas.Top)");
  114. playArea.Children.Add(enemy);
  115.  
  116. enemy.PointerEntered += enemy_PointerEntered;
  117. }
  118.  
  119. void enemy_PointerEntered(object sender, PointerRoutedEventArgs e)
  120. {
  121. if (humanCaptured)
  122. EndTheGame();
  123. }
  124.  
  125. private void AnimateEnemy(ContentControl enemy, double from, double to, string propertyToAnimate)
  126. {
  127. Storyboard storyboard = new Storyboard() { AutoReverse = true, RepeatBehavior = RepeatBehavior.Forever };
  128. DoubleAnimation animation = new DoubleAnimation()
  129. {
  130. From = from,
  131. To = to,
  132. Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)))
  133. };
  134. Storyboard.SetTarget(animation, enemy);
  135. Storyboard.SetTargetProperty(animation, propertyToAnimate);
  136. storyboard.Children.Add(animation);
  137. storyboard.Begin();
  138. }
  139.  
  140. private void human_PointerPressed(object sender, PointerRoutedEventArgs e)
  141. {
  142. if (enemyTimer.IsEnabled)
  143. {
  144. humanCaptured = true;
  145. human.IsHitTestVisible = false;
  146. }
  147. }
  148.  
  149. private void target_PointerEntered(object sender, PointerRoutedEventArgs e)
  150. {
  151. if (targetTimer.IsEnabled && humanCaptured)
  152. {
  153. progressBar.Value = 0;
  154. Canvas.SetLeft(target, random.Next(100, (int)playArea.ActualWidth - 100));
  155. Canvas.SetTop(target, random.Next(100, (int)playArea.ActualHeight - 100));
  156. Canvas.SetLeft(human, random.Next(100, (int)playArea.ActualWidth - 100));
  157. Canvas.SetTop(human, random.Next(100, (int)playArea.ActualHeight - 100));
  158. humanCaptured = false;
  159. human.IsHitTestVisible = true;
  160. }
  161. }
  162.  
  163. private void playArea_PointerMoved(object sender, PointerRoutedEventArgs e)
  164. {
  165. if (humanCaptured)
  166. {
  167. Point pointerPosition = e.GetCurrentPoint(null).Position;
  168. Point relativePosition = grid.TransformToVisual(playArea).TransformPoint(pointerPosition);
  169. if ((Math.Abs(relativePosition.X - Canvas.GetLeft(human)) > human.ActualWidth * 3)
  170. || (Math.Abs(relativePosition.Y - Canvas.GetTop(human)) > human.ActualHeight * 3))
  171. {
  172. humanCaptured = false;
  173. human.IsHitTestVisible = true;
  174. }
  175. else
  176. {
  177. Canvas.SetLeft(human, relativePosition.X - human.ActualWidth / 2);
  178. Canvas.SetTop(human, relativePosition.Y - human.ActualHeight / 2);
  179. }
  180. }
  181. }
  182.  
  183. private void playArea_PointerExited(object sender, PointerRoutedEventArgs e)
  184. {
  185. if (humanCaptured)
  186. EndTheGame();
  187. }
  188.  
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement