Step 1
Download Visual Studio Community 2015 and install it onto your computer, if it’s already downloaded and installed select Launch to start Visual Studio Community 2015 or if it has already been downloaded and installed then start the application you may also need to Enable your device for development.
Step 2
Once Visual Studio Community 2015 has started select File, then New, then Project… from the Menu.
Step 3
From the New Project window select Visual C# from Installed, Templates then select Blank App (Windows Universal) from the list, then type in a Name and select a Location to save to before selecting Ok to create the Project.
Step 4
Once done select from the Menu, Project, then Add New Item…
Step 5
From the Add New Item window select Visual C# from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project
Step 6
Once in the Code View for Library.cs the following should be entered:
using System; using System.Collections.Generic; using Windows.Foundation; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; public class Library { private const int length = 800; private const int speed = 600; private const int light = 300; private int turn = 0; private int count = 0; private bool play = false; private DispatcherTimer timer = new DispatcherTimer(); private List<int> items = new List<int>(); private Random random = new Random((int)DateTime.Now.Ticks); public Brush Background { get; set; } public void Show(string content, string title) { try { IAsyncOperation<IUICommand> command = new MessageDialog(content, title).ShowAsync(); } catch { } } private void highlight(Grid grid, int index) { Canvas canvas = (Canvas)grid.FindName("Pad" + index.ToString()); Brush brush = canvas.Background; // Original Background canvas.Background = Background; // New Background DispatcherTimer lightup = new DispatcherTimer(); lightup.Interval = TimeSpan.FromMilliseconds(light); lightup.Tick += (object sender, object e) => { canvas.Background = brush; // Original Background lightup.Stop(); }; lightup.Start(); } private List<int> select(int start, int finish, int total) { int number; List<int> numbers = new List<int>(); while ((numbers.Count < total)) // Select Numbers { // Random non-unique Number between Start and Finish number = random.Next(start, finish + 1); numbers.Add(number); // Add Number } return numbers; } public void New(Grid grid) { items = select(0, 3, length); play = false; turn = 0; count = 0; timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(speed); timer.Tick += (object sender, object e) => { if (count <= turn) { highlight(grid, items[count]); count++; } if (count > turn) { timer.Stop(); play = true; count = 0; } }; timer.Start(); } public void Tapped(Grid grid, Canvas canvas) { if (play == true) { int value = int.Parse(canvas.Name.Replace("Pad", string.Empty)); highlight(grid, value); if (value == items[count]) { if (count < turn) { count++; } else { play = false; turn++; count = 0; timer.Start(); } } else { timer.Stop(); Show("Game Over! You scored " + turn + "!", "Touch Game"); play = false; turn = 0; count = 0; } } } }
It should then appear as such:
Step 7
From the Solution Explorer select MainPage.xaml
Step 8
Select from the Menu, View then Designer
Step 9
The Design View will be displayed along with the XAML View and in this above <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”> enter the following XAML:
<Page.BottomAppBar> <AppBar IsOpen="True" IsSticky="True"> <StackPanel Orientation="Horizontal"> <AppBarButton Icon="Page" Label="New" Click="New_Click"/> </StackPanel> </AppBar> </Page.BottomAppBar>
While still in the XAML View below <Grid Background=”{ThemeResource ApplicationPageBackgroundThemeBrush}”>enter the following XAML:
<Grid Name="Display" HorizontalAlignment="Center" Height="300" Width="300"> <Grid.RowDefinitions> <RowDefinition Height="50*"/> <RowDefinition Height="50*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="50*"/> <ColumnDefinition Width="50*"/> </Grid.ColumnDefinitions> <Canvas Name="Pad0" Background="Crimson" Grid.Row="0" Grid.Column="0" Margin="10" Tapped="Pad_Tapped"/> <Canvas Name="Pad1" Background="Green" Grid.Row="0" Grid.Column="1" Margin="10" Tapped="Pad_Tapped"/> <Canvas Name="Pad2" Background="Blue" Grid.Row="1" Grid.Column="0" Margin="10" Tapped="Pad_Tapped"/> <Canvas Name="Pad3" Background="Gold" Grid.Row="1" Grid.Column="1" Margin="10" Tapped="Pad_Tapped"/> </Grid>
It should appear as such:
Step 10
Select from the Menu, View then Code
Step 11
Once in the Code View below the public MainPage() { … } the following should be entered:
public Library Library = new Library(); private void New_Click(object sender, RoutedEventArgs e) { Library.Background = ((Brush)App.Current.Resources["ApplicationSecondaryForegroundThemeBrush"]); Library.New(Display); } private void Pad_Tapped(object sender, TappedRoutedEventArgs e) { Library.Tapped(Display, (Canvas)sender); }
It should then appear as such:
Step 12
That completes the Windows Universal Application so Save the Project then select the Debug and Simulator option to run the Application
Step 13
Once the Simulator has started the Application should then appear
Step 14
After the Application has started running you can then tap the New Button, then one of the squares will highlight, select the correct one, then each time one more square will highlight each turn, match the patterns to continue
Step 15
To Exit the application select Stop Debugging in Visual Studio Community 2015
Step 16
Another option is to run as a Windows Phone application, select Debug and select Emulator 10.0.1.0 WVGA 4 inch 512MB option to run the Application
Step 17
Once the Emulator has started the Application should then appear
Step 18
After the Application has started running you can then tap the New Button, then one of the squares will highlight, select the correct one, then each time one more square will highlight each turn, match the patterns to continue
Step 19
To Exit the application select Stop Debugging in Visual Studio Community 2015
One thought on “Windows 10 Universal Windows Platform – Touch Game”