Jump to content


Save The Humans

  • Please log in to reply
No replies to this topic

#1 Kyle Workman

Kyle Workman

    New Member

  • Members
  • Pip
  • 1 posts

Posted 19 May 2014 - 06:30 PM

EDIT**** fixed the problem for some reason in the public MainPage() the this.InitializeComponent was not generated automatically by the IDE ( or i accidentally deleted it).

Hi, I am new to c# and I've been working through chapter 1 and hit many bumps in the save the humans app, all of which I have resolved myself. I am currently on page 35, the app compiles and runs but it just shows the big X and then shows a black screen afterwards with numbers at the top of the screen. I went back through checking my code with the book making sure I followed the book correctly and I am still stumped.

note: I am currently using VS2013 ultimate edition.

I would like to make an extreme difficulty to this game by speeding up the speed of the bouncing aliens by pressing a key or button how would i go about doing that?
using an IF/else statement?
if button_click
do Duration = new Duration(TimeSpan.FromSeconds(random.Next(2, 4)))
else Duration = new Duration(TimeSpan.FromSeconds(random.Next(4, 6)))

using SavetheHumans2.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using Windows.UI.Xaml.Media.Animation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace SavetheHumans2
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class MainPage : Page
        Random random = new Random();
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();

        /// <summary>
        /// This can be changed to a strongly typed view model.
        /// </summary>
        public ObservableDictionary DefaultViewModel
            get { return this.defaultViewModel; }

        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and 
        /// process lifetime management
        /// </summary>
        public NavigationHelper NavigationHelper
            get { return this.navigationHelper; }

        public MainPage()
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
            this.navigationHelper.SaveState += navigationHelper_SaveState;

        /// <summary>
        /// Populates the page with content passed during navigation. Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session. The state will be null the first time a page is visited.</param>
        private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="sender">The source of the event; typically <see cref="NavigationHelper"/></param>
        /// <param name="e">Event data that provides an empty dictionary to be populated with
        /// serializable state.</param>
        private void navigationHelper_SaveState(object sender, SaveStateEventArgs e)

        #region NavigationHelper registration

        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="GridCS.Common.NavigationHelper.LoadState"/>
        /// and <see cref="GridCS.Common.NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.

        protected override void OnNavigatedTo(NavigationEventArgs e)

        protected override void OnNavigatedFrom(NavigationEventArgs e)


        private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)


        private void startButton_Click(object sender, RoutedEventArgs e)

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

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

    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"

        <!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
        <x:String x:Key="AppName">Save the Humans</x:String>
        <ControlTemplate x:Key="EnemyTemplate" TargetType="ContentControl">
                <Ellipse Fill="Red" Height="100" Stroke="Black" Width="100"/>

        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    <Grid Background="#FF1D1D1D">
            <ColumnDefinition Width="160"/>
            <ColumnDefinition Width="160"/>
            <RowDefinition Height="160"/>
            <RowDefinition Height="160"/>

        <!-- Back button and page title -->
        <Grid Grid.ColumnSpan="3">
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
            <Button x:Name="backButton" Margin="39,59,39,0" Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
                        Style="{StaticResource NavigationBackButtonNormalStyle}"
                        AutomationProperties.ItemType="Navigation Button"/>
            <TextBlock x:Name="pageTitle" Text="Save the Humans" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1" 
                        IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,30,40"/>
        <Button x:Name="startButton" Content="START!" HorizontalAlignment="Center" Grid.Row="2" VerticalAlignment="Center" Click="startButton_Click"/>
        <ProgressBar Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Height="20"/>
        <StackPanel Grid.Column="2" Orientation="Vertical" Grid.Row="2">
            <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Text="Avoid These" VerticalAlignment="Center" SelectionChanged="TextBlock_SelectionChanged" Style="{StaticResource SubheaderTextBlockStyle}"/>
            <ContentControl Content="ContentControl" HorizontalAlignment="Center" VerticalAlignment="Center" Template="{StaticResource EnemyTemplate}"/>
        <Canvas x:Name="playArea" Grid.Row="1" Grid.ColumnSpan="3">
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF3556AC" Offset="0.31"/>
                    <GradientStop Color="#FF19611F" Offset="1"/>
            <StackPanel x:Name="Human" Orientation="Vertical">
                <Ellipse Fill="White" Height="10" Stroke="Black" Width="10"/>
                <Rectangle Fill="White" Height="25" Stroke="Black" Width="10"/>
            <TextBlock x:Name="gameOverText" Canvas.Left="425" TextWrapping="Wrap" Text="Game Over!" Canvas.Top="150" FontFamily="Arial Black" FontSize="100" FontWeight="Bold" FontStyle="Italic"/>
            <Rectangle x:Name="target" Height="50" Stroke="Black" Width="50" Canvas.Left="1035" Canvas.Top="55" RenderTransformOrigin="0.5,0.5">
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    <CompositeTransform Rotation="45"/>

Edited by Kyle Workman, 19 May 2014 - 07:04 PM.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users