Can a Raspberry Pi 2 with Windows 10 IoT Core run a game made in Unity?

Today, something somewhat weird occured to me. If you build a game in Unity engine, you can easily publish it for Windows Phone, even in old and very low-end devices like the Lumia 520. This phone has a Qualcomm Snapdragon S4 MSM8227 with ISA ARMv7 supported. The Raspberry Pi 2 Model B device also has an ARM v7 processor and can be loaded with Windows 10, that support the Universal Windows Plarform, which Unity supports. You can imagine the question, can a game built with Unity be published into a Raspberry Pi 2?

Read More »

Snake and Tetris-like games with Sense HAT on a Raspberry Pi 2 loaded with Windows 10

tl;dr – me n33dz the codez => check here on the SenseHatGames folder

The Sense HAT is a small and inexpensive board  for Raspberry Pi. It sits on top of it and offers a variety of sensors and (most importantly), a 8×8 RGB LED matrix and a 5-direction joystick. More specifically, it offers access to these sensors

  • Gyroscope
  • Accelerometer
  • Magnetometer
  • Temperature
  • Barometric pressure
  • Humidity

IMG_20160125_234729929_TOP.jpgSense HAT on top of a Raspberry Pi 2. You can clearly see the 8×8 RGB LED matrix whereas the joystick is on the bottom right of the HAT.

Absolutely worth mentioning, this board has also gone to space in the Astro Pi mission as part of a competition in the UK! I recently purchased one and, apart from the fun I had interacting with the sensors, I really liked the LED matrix and the joystick. So, I thought, why don’t I try and create a couple of games on it?

Read More »

Building the 2048 game in Unity via C# and Visual Studio

2048 is a very cool game that can make you spend hours playing it. Goal is to ‘merge’ tiles of identical values together, in order to have their value duplicated. When player swipes in her desired direction, items are moved towards there and a new item is created. If the player reaches the number 2048, then she has won the game. In this blog post, we’ll discuss how we can create it in Unity via C# and Visual Studio 2015.

As always, you can find the source code here on GitHub and test the game here via WebGL.

image

Screenshot showing the game in the Unity editor. On the left we can see the score and a restart button, on the middle the main game screen and on the right a visualization of the game’s 2 dimensional array contents, for debugging purposes.

Input methods

We have implemented two methods to get user input in the game. First one is via keyboard’s arrow keys, the other is via swipe in a touch screen (or mouse). We have implemented an enumeration to get user’s input and an interface which must be implemented by each input method we want to use. Moreover, if we need to add another input method in the future, e.g. input from an XBOX controller, we could simply implement the IInputDetector interface.


public enum InputDirection
{
    Left, Right, Top, Bottom
}

public interface IInputDetector
{
    InputDirection? DetectInputDirection();
}

Read More »

Using an integer to store multiple values in C#

Suppose that you are creating a game in which your character obtains some awards out of some predefined one, as the game goes by. Or, you’re creating software in which you want your user to select some entries/items (imagine something like a checked list box). In both cases, you could try and use these two approaches

  1. Create or use a data structure (e.g. Dictionary<K,V>) in which you store a reference to all possible items along with the relevant Boolean value. Easy right? In order to answer the question if user has obtained item X, you just check the value of the relevant key in the dictionary.
  2. Create or use a data structure (e.g. List<T>) in which you store references to the items the user has obtained/picked. So, you can use the List.Contains method to check for an item’s existence.

Both described methods are efficient, easy to grasp and use. However, there is another method in which you do not need to use nothing more advanced than a simple integer. We’ll be using binary system arithmetic and logic to accomplish this purpose.

Each number can be written in binary format in a sequence of 1s and 0s. For instance, 15 is binary 1111 and 2 is binary 0010. Most importantly, all numbers that are a power of two start with 1 and finish with some 0s. Check the below list

Binary – Base 10
1-1
10-2
100-4
1000-8
10000-16
100000-32
1000000-64

So, if you assign e.g. “valueA”to 1, “valueB” to 2, “valueC” to 4 etc., you can easily assign multiple values into an integer. How? You just add the relevant base 10 value to this integer, let’s call it storage. For instance, the integer 13 contains 3 values: a)1, b)4, c)8. In binary form, it is written as 1101. The way to check for a value’s existence is a logical bitwise AND between the storage and the respective value. In C#, we use the & for this purpose. If the result of the operation is equal to the value, then storage contains this value. Simple, right? Check the below C# code for the value 19.

int storage = 19; //10011
int a = 1; //1
int b = 2; //10
int c = 4; //100
int d = 8;//1000
int e = 16;//10000
Console.WriteLine("--------------------------------");
Console.WriteLine("Check for the value 19 - 10011");
Console.WriteLine($"Does {storage} contain \"a\" with the value of 1? " + ((storage & a) == a)); //true
Console.WriteLine($"Does {storage} contain \"b\" with the value of 2? " + ((storage & b) == b)); //true
Console.WriteLine($"Does {storage} contain \"c\" with the value of 4? " + ((storage & c) == c)); //false
Console.WriteLine($"Does {storage} contain \"d\" with the value of 6? " + ((storage & d) == d)); //false
Console.WriteLine($"Does {storage} contain \"e\" with the value of 8? " + ((storage & e) == e)); //true

Read More »

My .NET and C# samples

Recently, I uploaded on GitHub a lot of .NET and C# samples I have created over the years, as part of my presentations and trainings. You can find them here and download them. All the samples are comprised of  few lines of code that do a specific thing, relevant to the sample’s name. Some of the samples are pretty old and have been superceded by new language/framework advancements (e.g. Linq To XML vs XmlReader) but I’ve decided to keep them in case they prove useful to someone. Small description follows for each sample below:

  • C# Arguments – demonstrates usage of arguments in C# methods
  • Chat with TCP – a chat server and client using the TCP protocol
  • Collections – usage of .NET collections
  • Complex – usage of operator overloading
  • Default Arguments – demo of Parallel.ForEach
  • Delegates – usage of delegates in C#
  • DirectorySearcher – search directories using the DirectoryInfo class
  • DownloadString – WebClient demo
  • Events – simple Button.Click event handling
  • Exceptions – demo of Thread.Abort
  • Extension Methods – C# extension methods
  • Fibonacci – recursively calculating the Fibonacci sequence in C# using Func delegate
  • FileReaderWriter  – FileStream, StreamReader and StreamWriter demo on how to read and write to a file using C#
  • FileSystemWatcher – get notified of file system events (file created, deleted etc.)
  • Generics and IO – demo of a generic Dictionary being written and read from a file
  • Globalization – using globalization classes to display currencies and date/time information in a variety of countries
  • HttpWebRequest – HttpWebRequest demo to connect to an HTTP server
  • MD5 Hashing  – MD5 hashing using C#
  • MultiplyMatrices – multiply two matrices
  • Object Serialization – serialize an object using XmlSerializer class
  • Processes enumeration – get processes that are currently running on your machine
  • Reflection – use reflection in C# to discover information about unknown types and classes
  • Regex and pattern matching – use of regular expressions in C#
  • SendMail – how to send e-mail using C#
  • StopWatch – using a stopwatch in C# to count passing time
  • StringBuilder – an efficient class for heavy string manipulation
  • Threading – how to use threads in C# using classes in the System.Threading namespace
  • Timers – how to use timers in C#
  • XML DOM – read XML via XmlTextReader class

Read More »

Raspberry, Weather Shield, Azure IoT Hub, Streaming Analytics and more!

Intro

Recently I became the happy owner of a brand new Raspberry Pi 2. In case you don’t know about it, Raspberry is a pretty powerful computing device in a form factor of a credit card. You can install Windows 10 IoT Core on the Raspberry and code against it using the Universal Windows Platform (UWP_, aka write programs for this small device using C# and XAML. So, I embarked on a new self-education journey and, after some reading, I had to pick a pre-made hands-on sample and replicate it. I picked this one, since the only extra hardware it requires is a shield (Sparkfun Weather Shield) and some cables to connect it with the Raspberry. The sample instructions on hackster.io are pretty straight forward and the source code provided for the Weather Shield + UWP integration is pretty self-explanatory. Only modification I made was to add a timer to pull humidity and temperature from the shield at a fixed time interval.

12374739_10153447116073742_392889873121113510_o

12369049_10153441934143742_8606620306222203016_n

However, I wanted to take this project one step further. Connect the device to Azure IoT Hub, send data to it, pass the data via a simple Azure Stream Analytics job and store it to Azure Table Storage, expose it via a Web API web service and consume it from an ASP.NET MVC web application. Well, it’s not as difficult as it sounds! I’ll briefly describe the steps I used

Azure IoT Hub

The Azure IoT Hub (in preview, at the time of writing) is a service in which millions of devices can connect and exchange messages. The IoT Hub enables bidirectional communication to and from each device, can manage each device separately and provides a high level of security. To create a new IoT Hub, login into the new Azure management portal and create it. There is also a free tier to support app development, which you can use. If you do not have an active Azure subscription, you can easily create a trial account. Even though it requires a credit card, you will not be charged at all, provided you stay within your spending limits.

image

Read More »

Universal Helpers library for the Universal Windows Platform

It’s been some time since I posted something about my Universal Helpers library. The biggest news is that the library has been updated to support the Universal Windows Platform, allowing its usage on projects for Windows desktops, laptops, tablets, mobile devices and more! Apart from that, I’ve updated the library to include some additional behaviors and helpful classes. Let’s take a walkthrough. All this was made possible due to the release (open source!) of XAML Behaviors for UWP.

Behaviors

DragElementBehavior

Allows you to make a UIElement draggable, either by mouse or touch. It has options for

– allow the user to rotate, via multitouch

– allow the user to scale, via multitouch. User can select the maximum scale and the minimum scale.

– you can assign a container so that the draggable element can never leave the container’s bounds

– option whether the draggable element has inertia (i.e. will continue movement by decelerating when the user drags it and lets it go)

image

TapStoryboardBehavior

The TapStoryboardbehavior is useful for scenarios where we want to tap an element, start a storyboard and execute a method when the storyboard finishes running. It also has an option to specify whether the method is on the code-behind file or in the VM (i.e. in the DataContext of the page, for MVVM scenarios).

image

FeedbackBehavior

The FeedbackBehavior allows the user to implement a feedback functionality when an element is tapped and “pointer over”, specifically a scale animation on pointer over and an opacity change when tapped. This is used to notify the end user that she has indeed tapped the specified element.

SelectAllTextOnFocusBehavior

This simple behavior makes all text on a TextBox selected when the user taps on the TextBox.

Validation Behaviors

Read More »

My articles featured on Gamasutra!

So, this summer I had my 15’ of fame as Microsoft sponsored some articles/tutorials on the well known game development website Gamasutra. Some of my Unity development blog posts were selected, specifically

Creating a simple puzzle game in Unity, source code here on GitHub

Create a Bubble Breaker style game in Unity, source code here on GitHub

– A tower defense-style game in Unity part 1, part 2, source code here on GitHub

A brick breaking game in Unity, source code here on GitHub

Check here for all the Microsoft sponsored articles on Gamasutra and here for all my game development blog posts.

Building a match-3 game (like Candy Crush) in Unity

This tutorial is meant for educational purposes only to showcase how to build certain types of games. Please respect the copyrights/trademarks of others!

If you are in a hurry, you can try the game here and find the source code here.

Match three games are pretty famous these days. From the original Bejeweled to Candy Crush Saga and even Evolve: Hunters Quest, many games are based on the match 3 mechanism while giving specialized bonuses to the user. Such an example is giving a special item if she matches more than three items. If the user creates a match that includes this bonus, then the whole row or column is destroyed.

In this blog post, we’ll try to dissect in what is needed to build such a game, using Unity 3D, Visual Studio and the C# programming language. Game code was written in Visual Studio (check the free Community edition here). For debugging purposes, don’t forget to check the Visual Studio tools for Unity here.

Let’s start with a screenshot of the game running in the Unity Editor

image_5062F746.png

Only external assets we’re using are some candy graphics (Public Domain, found on OpenGameArt here) and a very cool sound (found on FreeSound here) to build our game. User can drag (in an attempt to swap) one candy either horizontally or vertically. When the swap happens, the game checks for a match. As soon as a vertical or horizontal match of three (or more!) is encountered, the matched candies disappear. Remaining candies collapse, new candies get created to replace them which collapse, too (imagine gravity acting upon them). The game checks if another match of three is encountered (without any user intervention). If this happens, the matched ones disappear again, remaining candies collapse, new candies fall and so on and so forth. This goes on until no match of three exist and user intervention is required for the game to go on. If the user does not touch the screen for a while, potential matches (candies that if one of them gets swapped will form a match of three) start animating, to give the user a small hint in order to continue the game.

The described game flow can be visualized in the below diagram

image_659DD063

Read More »

Playing music on netduino via a web browser

I was really lucky lately to get my hands on a netduino. I’ve never programmed on such a small device before and I have to confess that it’s really fun and awesome!

First, take a look at the video to see what’s going on. Basically, we are instructing the netduino to play a specific set of noted via a web browser on our PC.

(Please visit the site to view this video)

We’re using a netduino device. For the unaware, it’s a small device that runs a small version of the “big” .NET Framework, called .NET Micro Framework, so one can easily use C# and Visual Studio to code for it. It’s connected to a Piezo, a small component that can create some noise.

WP_20150115_11_39_20_Pro__highres

We’ve connected the netduino to our PC via USB cable and via an Ethernet cable. The USB connection allows us to deploy and debug our stuff on the device whereas the Ethernet cable allows us to make a small LAN between netduino and our PC. Without any more hassle, let’s check the code!

Playing music

Code was found on Getting Started with Netduino book. It basically adds octaves on a hashtable, sets frequency and period, connects to the speaker (via PWM which is accepting input on the Digital 5 Pin) and plays the respective input. Check the full code here

image

image

Web Server

Read More »