Fruit Game demonstrates how to create a simple random game to match three fruit-like symbols together.
Step 1
If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.
Step 2
Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…
Step 3
From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
Step 4
Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.
The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates
Step 5
Once done select from the Menu, Project, then Add New Item…
Step 6
From the Add New Item window select Visual C#, then Code 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 7
Once in the Code View for Library.cs the following should be entered:
using System; using System.Linq; using Windows.Foundation; using Windows.UI; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Shapes; public class Library { private const string app_title = "Fruit Game"; private const int size = 3; private readonly string[] values = { "Apple", "Lemon", "Orange", "Strawberry", "Blackberry", "Cherry" }; private int _spins = 0; private int[] _board = new int[size]; private Random _random = new Random((int)DateTime.Now.Ticks); public void Show(string content, string title) { IAsyncOperation<IUICommand> command = new MessageDialog(content, title).ShowAsync(); } private Path Ellipse(int x, int y, Color fill) { return new Path() { Data = new EllipseGeometry() { Center = new Point(20, 20), RadiusX = x, RadiusY = y, }, StrokeThickness = 5, Margin = new Thickness(10), Fill = new SolidColorBrush(fill) }; } private UIElement Fruit(int type) { switch (type) { case 0: // Apple return Ellipse(20, 18, Colors.LawnGreen); case 1: // Lemon return Ellipse(16, 20, Colors.Yellow); case 2: // Orange return Ellipse(20, 20, Colors.Orange); case 3: // Strawberry return new Polygon() { Points = new PointCollection { new Point(0, 0), new Point(15, 30), new Point(30, 0) }, Stretch = Stretch.Uniform, StrokeLineJoin = PenLineJoin.Round, Height = 40, Width = 40, Fill = new SolidColorBrush(Colors.IndianRed), Stroke = new SolidColorBrush(Colors.IndianRed), StrokeThickness = 5, Margin = new Thickness(10) }; case 4: // Blackberry return new Path() { Data = new GeometryGroup() { Children = new GeometryCollection() { new EllipseGeometry() { Center = new Point(10, 10), RadiusX = 8, RadiusY = 8 }, new EllipseGeometry() { Center = new Point(30, 10), RadiusX = 8, RadiusY = 8 }, new EllipseGeometry() { Center = new Point(20, 30), RadiusX = 8, RadiusY = 8 } } }, Fill = new SolidColorBrush(Colors.MediumPurple), StrokeThickness = 5, Margin = new Thickness(10) }; case 5: // Cherry return new Path() { Data = new GeometryGroup() { Children = new GeometryCollection() { new EllipseGeometry() { Center = new Point(10, 25), RadiusX = 8, RadiusY = 8 }, new EllipseGeometry() { Center = new Point(30, 25), RadiusX = 8, RadiusY = 8 } } }, Fill = new SolidColorBrush(Colors.MediumVioletRed), StrokeThickness = 5, Margin = new Thickness(10) }; default: return null; } } private void Add(ref Grid grid, int column, int type) { Viewbox viewbox = new Viewbox() { Height = 100, Width = 100, Stretch = Stretch.UniformToFill, Child = Fruit(type), }; viewbox.SetValue(Grid.ColumnProperty, column); grid.Children.Add(viewbox); } private void Layout(ref Grid grid) { grid.Children.Clear(); grid.ColumnDefinitions.Clear(); // Setup Grid for (int column = 0; (column < size); column++) { grid.ColumnDefinitions.Add(new ColumnDefinition()); _board[column] = _random.Next(0, 6); Add(ref grid, column, _board[column]); } } public void New(ref Grid grid) { Layout(ref grid); _spins++; // Check Winner if (_board.All(item => item == _board.First())) { Show($"Spin {_spins} matched {values[_board.First()]}", app_title); _spins = 0; } } }
In the Library.cs there are using statements to include the necessary functionality. There are Constants including string[] to represent the names of the fruit and Members including Random to pick a randomised selection from. The Show Method is used to display MessageDialog, Ellipse is used to create some of the shapes needed along with Fruit which will build up the various shapes to represent the fruit named earlier using combinations of Path and GeometryGroup in different colours. The Add Method is used to place items that will make up the set of Fruit and Layout is used by New to start the game and create the look-and-feel of the application.
Step 8
In the Solution Explorer select MainPage.xaml
Step 9
From the Menu choose View and then Designer
Step 10
The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:
<Viewbox> <Grid Margin="50" Name="Display" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Viewbox> <CommandBar VerticalAlignment="Bottom"> <AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/> </CommandBar>
The first block of XAML the main user interface of the Application, this features a ViewBox containing a Grid Control that will be the look-and-feel of the game. The second block of XAML is is the CommandBar which contains New – to start a game.
Step 11
From the Menu choose View and then Code
Step 12
Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:
Library library = new Library(); private void New_Click(object sender, RoutedEventArgs e) { library.New(ref Display); }
Below the MainPage() Method an instance of the Library Class is created, then New_Click is used to call the New method to setup the game in the Library Class
Step 13
That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application
Step 14
After the Application has started running use New to start playing, you can win by getting three identical fruit in the Application
Step 15
To Exit the Application select the Close button in the top right of the Application