Creating Games With Silverlight

Written by:  • Edited by: Linda Richter
Updated May 30, 2010
• Related Guides: Silverlight

A Silverlight development tutorial that shows you how to create your own 2D web based games. This series attempts to recreate a game originally developed with Flash, and can be used to highlight the differences between the two platforms.

Comparing With Other RIAs

When it has come to displaying rich content on the web Flash was king. Its widespread deployment on Internet connected devices and the lack of any real competition made it the only choice for taking a web page beyond a static collection of pictures and text. The playing field is changing though. As web services start to take over from traditional desktop applications (you only need to look at Google Docs, Zoho and Buzzword for an example of this) some major players like Microsoft and Sun, not wanting to be left out of this Web 2.0 revolution, have introduced their own RIA (Rich Internet Application) platforms in the form of Silveright and JavaFX respectively.

The series Flash Game Development with Flex and Actionscript looked at using Adobe Flex to create a Flash game in Actionscript that could be played in a web browser. In the interests of fairness this series will focus on recreating that same game with Silverlight using C#.

To begin with we first need to set up a base for creating a game. This will involve creating a render loop, embedding and loading some resources, and finally displaying them to the screen. Lets start with the render loop.

When you create a new Silverlight project the Page.xaml and Page.xaml.cs files are created automatically. We will implement the render loop in the Page.xaml.cs file.

Page.xaml.cs C# Silverlight source code

In Silverlight 1 creating a render loop was a complicated process involving storyboards and timelines. Thankfully in Silverlight 2 we have the ability to attach a function to the CompositionTarget.Rendering event, which is called before the screen is drawn. This gives us the starting point for our render loop. In order to distribute this event to other game objects we create a enterFrame event object. Our game objects will attach themselves to this event object which will be triggered once every frame, allowing the game objects to update themselves.

In order to use the render loop we create a class called BaseObject. This class will simply be a base for the game objects, and will expose a function called enterFrame to any class that extends it. This class will be expanded later on to include user input and collision detection, but for now its only purpose is to provide an easy way to tap into the render loop.

BaseObject.cs C# Silverlight source code

Making use of the BaseObject class is the AnimatedGameObject. This class will display an animated bitmap sprite on the screen.

AnimatedGameObject.cs C# Silverlight source code

The images used to make up the amination have been embedded (this is done simply by including the image files in the Silverlight Visual Studio project), and then accessed using a neat little utility class called ResourceHelper which you can download from http://www.silverlightexamples.net/post/Load-Bitmap-Image-From-Resource-in-a-Single-Line-of-Code.aspx. These images are then applied to a Rectangle object, which has been added to the main application canvas.

Lastly we need a place to create these new AnimatedGameObjects. For this we will create the ApplicationManager. It is the purpose the the ApplicationManager to “oversee” the game.

ApplicationManager.cs C# Silverlight source code

As you can see the ApplicationManager is a very simple class, with the startupApplicationManager creating two new AnimatedGameObjects. The startupApplicationManager function itself is called from the Application_Startup function from the automatically generated App.xaml.cs file.

private void Application_Startup(object sender, StartupEventArgs e)

{

this.RootVisual = new Page();

ApplicationManager.Instance.startupApplicationManager();

}

So as a starting point we have our render loop and some base classes that allow us to easily update game objects with the render loop and display bitmap animations. Check out the online demo, and download the source code from the Sourceforge SVN repository, or download an archive containing all the source code here.

Back to Silverlight Game Programming Tutorials

Images

Silverlight Bitmap Animation Demo

Related Article

google-o3d-demo Google O3D Programming Tutorial - Getting Started

Learn to build a simple O3D 3D web application with this free tutorial.


Comments

Showing all 8 comments
 
Adon Jul 16, 2011 11:36 PM
Nice, the result...
Well I just started 3 days ago to decide and try silverlight.
http://test.durasales.biz/TestPage.html
I'm trying to see the posibility of using a Gameloop with updates and stuff and see if Silverlight has capabilities to clone what I did in XNA. so far so good but seems like some concepts cannot be done in silverlight. Usually in a game loop I redraw on a canvas/image buffer. silverlight seems to view everything as a UIElement and I can only provide each items as a rect on the canvas for now..
B N Jan 30, 2011 6:16 PM
SVN Down?
Tried to access http://silverlight.svn.sourceforge.net/viewvc/silverlight/ as of Jan 30, 2011 and got a "Webpage is not available" error. Are you still keeping the SVN source around?
Logan Aug 4, 2010 11:13 PM
A few questions
Hello and thank you for your great tutorial. I had a few questions that I hoped you could answer. I'm using Expression Blend 4.

I wanted to know how you would cause the game to run on a page and not the mainpage. ie, a page that you could navigate too. I'm having issues with doing so, because of the UserControl aspect.

Another question would be: How do you make the children of the layoutroot collapsed rather removed, so that when you press escape, you can again navigate through your other features.

I was also hoping that you could show some in game menue features.

Thanks again for all your hard work in putting this together!
sai prasad Jun 30, 2010 2:18 AM
RE: Clarifications on LayoutRoot and ResourceHelper Methods
(SilverlightApplication.App.Current.RootVisual as Page).LayoutRoot.Children.Add(rect);

i am getting error missing directory reference for LayoutRoot method
same this is happening for line
imageBrush.ImageSource =ResourceHelper.GetBitmap(animationData.frames[currentFrame]);
for ResourceHelper missing directory reference
Steve Westbrook May 20, 2010 1:44 PM
Image Source
Nice tutorial Matthew, however there is an easier way to get an imagesource:

new ImageSourceConverter().ConvertFromString("Globe.png") as ImageSource

will return an imagesource without the need for an external library.
Mark Sanchez Feb 11, 2010 11:58 PM
Using SilverLight 3.0 - trouble?
This is a great tut. This is my first C# project ever but games are right up my ally.

Everything is building fine but when the browser window comes up the are no graphics displayed?

I made my own media directory in the solution folder with the graphics. Could there be something in SL3.0 I need to set? Is there some special way to add the graphics to the project?

Thank you for any help, Mark
Matthew Casperson Oct 2, 2009 7:35 PM
RE: Creating Games With Silverlight
That sounds like a good idea. I'll definitely keep that in mind when I update the article.

Thanks
Soshimo Oct 2, 2009 12:28 PM
Another Great Tut
Thanks again for another great tut. I told the guys at my consulting firm that SilverLight will be a viable replacement for flash games but they laughed me off. Microsoft geeks can be so serious!

One suggestion that I would like to make, however, is to decouple the MainPage from the rest of the game objects. That way you can reuse the base objects - maybe even put them into a lib. You can do that by creating a generic publish/subscribe manager. I've done this many times in the past for guaranteed event distribution (think MMQ) and have used it in a few simulations as well. Basically you have a class that allows publishers and subscribers to register. When a publisher publishes an event the manager searches for any subscribers of that event type and asynchronously dispatches it. It doesn't care what type it is, just that it has that event. That way you can remove the MainPage specific code in BaseObject and it can now be reused regardless of the name of the page with the main canvas.

Anyway, great tut, please keep up the outstanding work!
 
blog comments powered by Disqus
Email to a friend