How To Create Simple Game Play Application Using Cross Platform

Introduction

Xamarin is a platform that allows us to create multi-platform mobile applications like Windows phone, Apple iOS, and Android through a single integrated development environment (IDE). This platform allows the designers to develop and design various mobile platform applications within a limited period. We will discuss how to create game applications using Xamarin.Forms (or) cross platform from VS2017. There are many plugins available for Xamarin.forms including Stack Layout, Label, and button.

STEP 1

First, select FILE >> NEW >> PROJECT.

STEP 2

Then, in a few minutes, this page will appear. Click Visual C# >> Cross plateform >> “XAMARIN FORM OR NATIVE” and click OK button.

STEP 3

Next, double click on MainPage.xaml. The XAML code appears. If you need to select XamarinDemo (Portable), don’t click on another platform. For example - Android, UWP only and click on portable page.

 
STEP4

I have given the source code. You can use this code or you can use your own code.



SourceCode
  1. <StackLayout>  
  2.     <Label Text="This for demo app" VerticalOptions="Center" HorizontalOptions="Center" />  
  3.     <Button Text="1" VerticalOptions="CenterAndExpand" HorizontalOptions="Center" Clicked="ButtonClicked" />  
  4.     <Button Text="2" VerticalOptions="CenterAndExpand" HorizontalOptions="Center" Clicked="ButtonClicked" />  
  5.     <Label Text="Game Play!" VerticalOptions="Center" HorizontalOptions="Center" />   
  6. </StackLayout>  

STEP5

Then, let's go to the Solution Explorer, select down arrow, click on MainPage.xaml. Marked page of MainPage.xaml.cs should be there.

STEP6

MainPage.xaml.cs public portal class will be there. I have given source code below.

SourceCode

  1. static string demo = new Random().Next(1, 4).ToString();  
  2. static int scores = 0;  
STEP 7
 
Make any corrections or modifications.

Source Code
  1. async void ButtonClicked(object sender, EventArgs e) {  
  2.     Button button = sender as Button;  
  3.     if (button.Text == demo) {  
  4.         await DisplayAlert("Demo""!!Game Over!!""Again");  
  5.         demo = new Random().Next(1, 4).ToString();  
  6.         scores = 0;  
  7.     } else {  
  8.         scores += 1;  
  9.         await DisplayAlert("Demo""Scores" + scores, "Again");  
  10.         demo = new Random().Next(1, 4).ToString();  
  11.     }  
  12. }  

STEP 8

Check to verify MainPage.xaml and MainPage.xaml.cs connected button is the same. This button is for connecting a program.

STEP 9

Then, go back to Solution Explorer. Right click on XamarinDemo; you'll see "Build" button.

STEP 10

I have connected the debugging mobile but I’ve selected Visual Studio_android_ARM-phone (Android 6.0-API 23); then Deploy mode starts automatically.

STEP11

Just wait a few minutes to open Android Emulator. Click to check on your own created application. 



STEP12

Game will appear over screen. Just click the button to start the first step of game.

STEP13

Previous Android process changes to Android to Universal Windows Platform (UWP)

STEP14

Go on Solution Explorer and select UWP.  Next, Build tool appears; select Configuration Manager.

STEP 15

UWP platform is selected on ARM, then Build and Deploy boxes are selected.

STEP16

Again click on UWP from solution explorer and Click on Rebuild option

STEP17

Deploy from your local machine

STEP18

After process completes  Xamarim.UWP screen appears 

STEP19

Game over appears 

SUMMARY

In this article, you learned how to create mobile cross platform applications using Xamarin Forms with .NET Standard libraries.

If you have any questions/ feedback/ issues, please write in the comment box.