Category Archives: c#

Vector graphics game using C# / WPF and the Canvas

This began as a simple 2d maths exercise that turned into a legacy arcade game to give the project some focus. I’ve never written a game before so part of the fun was working out the algorithms myself and not looking up any accepted way to write a game loop or perform the real time calculations. The end result is a pretty good game that runs fine at 50fps (frames per second). My choice of display technology was WPF and the Canvas. I read some time ago that Microsoft advise against using WPF for games, but the whole project was meant to be fun and I figured it would be interesting to see where WPF failed. As it was, WPF didn’t fail. The WPF rendering engine had to update almost 1000 spinning lines at an attempted 50fps before it started to stutter (on a first generation i7 2.8GHz processor). I’ll re-factor the game using either OpenGL or Direct2D when I get the chance and see how they compare.

The game is smoother than the video suggests but my screen scraper added some jerkiness. To stay within the limits of the rendering engine I had to reduce the lifetime of both shrapnel and explosions and give the floating scores a distinct lifetime (1500ms) which reduces their effect. It will be interesting to see if I can remove these constraints with OpenGL and Direct2D.

A clear omission is collision detection between asteroids. It would have been easy to add this, but I want to implement it properly as the existing collision detection is simply “if touch then explode”. For the asteroids, I would like to create a “physics engine” so rather than just bounce off each other like billiard balls, their contours, momentum and the angle of the collision are all taken into consideration when calculating their impact and new velocity. A nice feature would be if a sharp edge of one asteroid squarely hits a straight edge of another then the second asteroid cracks at the point of contact or, if the impact is sufficient, breaks into smaller pieces.

All sounds and music are in-game.