XNA Windows Phone Theme Library


Danny, one of our First Year students, is hard at work on his First Year programming coursework. This year the game is “Sweepy Cleaner”, an XNA game where you get to control a robot vacuum. I’ve even bought the domain name.

Danny made a version of Evil Squash last year for Windows Phone and he is now building a Sweepy Cleaner for the phone as well. And he has discovered that:

  1. It is nice if a game has the same colour theme as the ones set for the phone by the user.
  2. It is hard to get these colours from an XNA game

So he has written a little bit of code to get this information into a game, and released it as a dll that any XNA developer can use. Which is nice. You can find it here.

Reaching for XNA


We came across this issue in the lecture this afternoon. If you are using a machine which lacks really high performance graphics, perhaps because it has an on-board graphics display, you might get an error when you try to run an XNA program on it, because the graphics hardware can’t keep up.

If this happens you can try changing the Game Profile in the project settings for the game. If you set them to “Reach” this will reduce the demands that are made on the machine, and might make the game work.

It worked for me in the lecture….

Positioning Sprites with Rectangles and Vectors in XNA


Staying with the XNA techie theme, now a word about performing sprite positioning in a game. A sprite is something that has a texture (the image to be displayed when the sprite is drawn) and a position (the place to draw it). Generally sprites map onto objects in a game. If I am creating a game and I need to draw a bat, ball, spaceship, alien or asteroid I will use a sprite to do this.

There are two different ways to position sprites in XNA and each has its advantages and disadvantages. However, it is worth knowing about both.

Rectangle Positioning

With this form you create a rectangle which tells XNA where to draw the item, and how big it is:

Rectangle r = new Rectangle(0, 0, 200, 100);
spriteBatch.Draw(texture, r, Color.White);

This draws our texture in the top left hand corner (0,0) , in a rectangle 200 pixels wide and 100 pixels high.

Using a rectangle like this works well, it also gives you a ready made “bounding box” which you can use to test for collisions:

if (rect1.Intersects(rect2)) {
    // We have a collision between rect1 and rect2

However, there are some bad things about using rectangles that make me less keen on them

  • Rectangles are positioned using integers. The x and y properties you use describe where they are do not have a fractional part. This means that if you want to move a rectangle slowly around the screen (i.e. less than a pixel per update) you can’t just use the x and y properties to do this.
  • You can use rectangles to scale drawing, but this gets a bit tedious as you have to work out the size of rectangle you need, and you also need to be mindful of the aspect ratio of the item you are drawing so that it doesn’t end up squashed or stretched.
  • It is impossible to rotate a sprite which is positioned using a rectangle

So, rectangles are good for very simple sprites, but once you have become more familiar with XNA I think it is worth moving on to Vector positioning.

Vector Positioning

A vector is something that has direction and magnitude. That sounds posh. Actually it just contains an X and Y value, just like a coordinate. The “direction and magnitude” bit kicks in if you draw a line from the origin (0,0) to the X, Y position given. This line is the vector. The direction is the way the line points, and the magnitude is how long the line is.  In XNA terms the Vector2 type is the one you can use to position 2D sprites:

Vector2 v = new Vector2(10, 10);
spriteBatch.Draw(texture, v, Color.White);

This code draws the texture at position (10,10) . The texture is drawn in whatever size it happens to be, i.e. if the texture image was 200 pixels by 100 it would be drawn that size.  This means that you might need to scale your textures to fit a particular screen size – but as we shall see later this is not  a huge problem.

There are quite a few good things about vectors though.

  • The X and Y values of a vector are floating point, so you have very good control of sprite speed
  • The XNA framework supports vector mathematics directly. So you can write code like this:

    position = position + speed;

    - where position and speed are both vectors

When it comes to scaling and rotating a sprite positioned using a vector you can use a more complex version of the draw command (find a detailed description here) to do all this. Also, bear in mind that if you targeting Windows Phone you can fix your resolution in the game to a particular value and then make all your to assets fit.

graphics.PreferredBackBufferWidth = 480;
graphics.PreferredBackBufferHeight = 800;

The phone hardware will scale the display automatically to match whatever size you specify, now and in the future.

If you want to detect bounding box collisions you can write code like this:

public bool Contains(Vector2 pos)
    if (pos.X < position.X) return false;
    if (pos.X > (position.X + texture.Width)) return false;
    if (pos.Y < position.Y) return false;
    if (pos.Y > (position.Y + texture.Height)) return false;
    return true;

This returns true if the area covered by the sprite contains the given position.

For bounding box collisions you can use this:

public bool Intersects(Sprite c)
    if (pos.X + texture.Width < c.pos.X) return false;
    if (c.pos.X + c.texture.Width < pos.X) return false;
    if (pos.Y + texture.Height < c.pos.Y) return false;
    if (c.pos.Y + c.texture.Height < pos.Y) return false;
    return true;

This test will tell you if the bounding box around the two sprites intersects. However, if the sprites are textures that don’t fill the entire sprite rectangle this test is not a very accurate one. I’ll be covering pixel level collision detection next.

Game Object State Management in XNA

Vegas Building

Hmm. This sounds a bit deep for a Saturday blog post. I suppose it is, but I got asked a question at Mix 11 and I’ve been pondering it ever since then. The question was about some code like this:

class Cloud : CloudGame.ISprite
    public Texture2D CloudTexture;
    public Vector2 CloudPosition;
    public Vector2 CloudSpeed;
    public bool Burst = false;

    public void Draw(CloudGame game)
        if (!Burst)
   // rest of cloud code here

This is a tiny part of my “Cloud Bursting” game demo, where players touch clouds to burst them. The above code is the definition of the cloud itself. This has a texture, position, speed of movement and a Burst flag as properties.

Burst is how the cloud “knows” if the cloud has been burst. If the cloud is burst it is not drawn. At the start of the game all the clouds have the Burst flag set to false to indicate that they are still part of the game. As the player touches them the Burst flag is set true.

You see this in a lot of game play situations. Anywhere that things are “killed” they have to be able to remember whether they are still alive or not. The code above is simple and works well, at any instant a cloud is either burst or not. If it is burst (i.e. the Burst flag is true) plays no further part in the game and is not drawn.

However, this design gave rise to the question: “Why do we need to have a flag? Surely it would be more efficient to remove a ‘burst’ cloud from the list of things to be drawn?”

This is a very good question. To understand it you need to remember that in a game we will have many objects on the screen at any given time. To manage this the game contains a collection of some kind (usually a List). During Update and Draw the game works through the list acting on each item in it. If an item is removed from the list it will no longer be part of the game.  When the player bursts a cloud, rather than setting a flag you could just remove that cloud from the list of active game objects.

This approach has the advantage that it is more efficient. Instead of a cloud having to decide not to draw itself the game never tries to draw burst clouds at all.  However, it also adds complication. The game must now have a “master” list of clouds and an “active” list of clouds. At the start of a game the master cloud references are copied into the active list. This is to avoid the overheads of creating and destroying objects, something you really don’t want to be doing on a regular basis.

Furthermore, the time that is saved is probably not going to be that much use to us. If the game works fine with all the clouds visible (which it should do) then saving small amounts of processor time when some clouds are removed is not going to make that much difference to performance. In this situation it is the drawing of the objects that takes the time, not deciding whether or not to draw them.

The fundamental principle here is that you should go for simplicity first, then optimise when you discover you have a performance issue.  I like having the flags present, it makes for greater cohesion and easier debugging. I can look at any cloud and decide whether or not it should be drawn simply by inspecting the flag.  If the flag wasn’t there I’d have to check which lists held the cloud, and so on.

So, I go with internal flags, not lists, which is pretty much what I said at the time as I remember.

Some Mix Videos to Watch


If you weren’t at Mix and you want to watch the video of my session you can now view it here:


You can get all the demo code that I talk about here:


This afternoon I was invited to take part in a Live Channel 9 session on video game design. Also on the stage was Brandon Foy, perhaps the coolest person I have ever met. He is the creator of an astonishing Windows Phone fan video that you can view on YouTube here:


Microsoft have said that if they get more that 200,000 views they will pay to put it out as a TV advertisement.  So, if you want to cost Microsoft some money, go take a look. Actually, it is a stunning piece of work and well worth 2 minutes of anyone’s time.

The video game session itself was great fun, as well as Brandon there was Nic Fillingham  from the XNA team and John Papa kept us all under control. For me the interesting thing was how much we all agreed on what is important in video game design. In a nutshell:

  • Figure out what your game is about and then protect this idea against all who would wish to “improve” it.
  • Get help with the graphics. Programmers think they can solve any problem with software. They probably can, but graphical design is not a problem as such. You really need someone with a good eye for that, and a coder is not guaranteed to be that person.
  • Put your game into the hands of people who can give you proper feedback. This doesn’t mean family or friends, this means those whose only reason the like the game is the game itself.
  • Design in the persistence features right at the start. A phone game will have to stop and start. When you make the game objects you need to figure out which bits must be saved and when. Adding this stuff right at the end of the development is really hard. If you have this stuff there all the time it means it will be properly tested when you send the game out.

After this I staggered back to the hotel to do some packing. My flight leaves at 7:15 am tomorrow. Lovely.

Mix 11 XNA Madness

Room Left

This is the left hand side of the room. The right hand side were just as good looking. You can find that picture on Flickr, just click through to get there.

Just done my Mix 11 session. Great fun. Needed nerves of steel (or at least corrugated cardboard) when the video projector shut down just at the start. Fortunately the audience was super terrific. And most of them like cheese. Some great questions at the end.

You can find the slides for my session here.

I’ve made a brand new demos site with all the session demos for today, plus loads of other Windows Phone good stuff. You can find it all here:


I’ll keep this up to date with any new stuff I write, and move some other things in there that are presently hard to find.

If you want to see me go through any of the other demos, catch me at the Windows Phone Booth today at 2:00.

The Rob Miles Roadshow

Next week I’m doing a Mix session all about game development in XNA, stay tuned for some brand new examples and sample code, but unfortunately no new jokes. I’ve just found out that I’m also doing Ask the Experts on Tuesday evening starting at 6:00 pm  (and for once I’m not going to be doing the asking).  They are using a “speed dating” arrangement where we meet up with a bunch of folks for 20 minute slots where we get over the gist of what we are at Mix for. I’ll be doing Windows Phone games (of course) and I’d love to meet you if you come along. Strange this, nothing for years and then two sessions of speed dating in a matter of a weeks.


If you are lucky enough to have a Windows Phone you can download the official Mix application which is great. And also mentions me. Search the marketplace for “Mix 11”.

The “Rob Miles Roadshow”™ then rumbles on to TechDays later this month. They also have a really good Windows Phone application too, although this one works best if you can speak Dutch. Search the marketplace for “Techdays”.

DevDays Speaker

I’m doing a session on Windows Phone games, another on the .NET Micro Framework and a third (deep breath) where I’m going to be talking about Windows Phone development in rhyme, just for Geek Night. And I’m also making an appearance at one of the keynotes too. Now, if that’s not a reason to buy a new pair of jeans, I don’t know what is… Find out more here.

XNA HiDef you Don’t Want

I was doing a lecture this afternoon to the first years and, frankly, it wasn’t going well. The microphone was not working, which meant I had to shout, and I’d not uploaded my slide deck correctly so I had to wait for my little laptop to download the presentation from Live Mesh.  Then I fired up my first empty XNA project to show the class how it worked and was rewarded with this.


Not good. Id used the machine to write Windows Phone games with no problem and I’d never seen that message before.  Fortunately we were near the end of the lecture and so I just showed that I could in fact get Windows Phone to work and then everyone had to leave.

Someone at the back had shouted “Use Reach” but at the time this hadn’t registered. Later, I took a look at the project settings:


Turns out that if you have fairly simple graphics hardware, like the one in my little laptop, it can’t handle the HiDef graphics and you have to select the Reach option for Game Profile.

I did this and everything worked, so I should be OK for tomorrow.

Free “Windows Phone Blue Book” Course


Windows Phone 7 Blue Book

Microsoft Faculty Connection have just released my “Windows Phone Programming Blue Book”. This is kind of a companion to the "C# Yellow Book” which has been around for a while. However, it has a lot more stuff with it. There are demo programs and also a whole set of presentations and exercises that you could use to create a complete course on Windows Phone 7. You can find the content here:


Mad Game Development in the Press

306 mag

You might remember our Mad Game Development a few weeks ago. Fifteen teams spent 24 hours on campus writing a game. Great fun. Writer Peter Gothard (now of GamesTM Magazine) came along for the ride (actually it was his idea….)

Anyhoo, he has now written up the whole thing as a superb article in 360 Magazine this month. With pictures of all the teams and screenshots of their games it is well worth a read. In all good newsagents now..

Pong HD in the Windows Marketplace


Harry Overs, one of our second year students, has taken the version of Pong that he wrote for coursework in his first year and made it into a Windows phone game. It is now on the Marketplace for anyone to download. Harry got a free Marketplace membership from Dreamspark.com (any student can do this) and submitted the app a little while back. He did the bulk of the development on the emulator and then just popped into my office and tried the game on my device to make sure that the touch and tilt elements were properly calibrated.

Well done Harry. If you want to check out the game you can find it by searching the Marketplace for Pong HD. Any other Hull students out there with games they want to try on a real device, just drop round to my office and we’ll fire them up.

Note: This doesn’t include the two First Year students who bought the first two HTC HD7 devices in Hull, and stopped me from getting one….

Hull Digital Windows Phone Fun

Hull Developers

I didn’t actually shout out “Look ‘Thoughtful’ Folks”, but it looks as if I might have done….

Had a great time at the Hull Digital Group meeting tonight. The topic for the night was mobile development. First up was John Connolly who gave a smashing talk on the pitfalls and potential of mobile development.

Then there was me. I gave a “Biased Overview of Windows Phone 7” where I extolled the virtues of the platform and very nearly showed my Windows Phone Twitter reader working. The audience was great, with some lovely discussion and very thoughtful questions at the end.  You can find the presentation and code files (including a version of that video game grate “Cheese Lander”) here.

XNA Mad Game Development Ends


Mad Game Development Final Survivors

Compare this with the original picture……

Well, that was some wild ride. I feel like I’ve been to a really good, but really long, party. And in a way I have. We started yesterday at 11:00 with a game idea, some assets and some partially finished game engines. We ended today with 14 teams showing off some polished and workable game programs that I hope you will see on XNA Indie Games real soon. In between we’ve had pizza, snacks,  chocolate, a whole bunch of fizzy drinks and a lot of development. A lot.

I kept going into the labs thinking to myself “They must be ready for a break by now” and finding everyone working flat out. At three in the morning, four in the morning and five in the morning.  I watched teams get stuck, get inspired and finally get it working. Amazing stuff. Not everything went as planned,  but having that happen is just part of the development experience….

There is nothing quite like debugging somebody else’s code at 3 in the morning.  Peter and Sarah from 360 magazine were taking notes and pictures and getting into the spirit of the whole thing. Peter was even producing artwork for one team.

This morning we had the judging and the awards and then everyone went home to collapse. Especially me.  Congratulations to the winners:

  • Judge’s Choice: “Team Ice Cream”
  • People’s Choice: “Unhandled Exceptions”
  • Technical Flair Award: “Left 4 Dev”
  • Look and Feel:  “Who needs to go outside?”
  • Cheesiest Game: “Generic Game Developers”

The judges also wanted to make a special award to team “Rusty Spoons” who came in on Saturday as First Year students who have only been coding for a few weeks and produced a fully working game by the end.

Mad Game Development Final Judges Choice

We then had a bunch of further awards and then headed off to bed. Best bit for me? When I asked if they wanted to do it again and everyone looked up with sleep deprived eyes and said “Oh yes”.

Great fun. Thanks to Peter Gothard from 360 Magazine for the idea and the swag. Microsoft UK for the extra swag, Warren for paying for the food and our two other judges, Jon Purdy and David Miles.

I’ll have a full list of all the awards and winners later, and there are some more pictures on Flickr. But I’m off to bed now…