Asteriods in C# Using GDI+
Summary
For my first experiment in game design, ratherthan using some kind of directX wrapper I wanted to see what waspossible with GDI+.GDI+ has a lot of nice features but is slow, so not very practical forfast paced games, in this example you can see why.The game also includes sound…hey, it wouldn't be much of a gameotherwise!
Overview
3 Classes, 1 for each on screen object to be represented:
SpaceShip
Asteroid
Bullet
These classes do have many common methods and properties so they could inherit from the same base class.
The windows form object pulls all theseclasses together into the same playing field, handles collisiondetection and player interaction.Everything is treated on a tick by tick basis via the use of a timerobject.
Installation
All source/compiled files and resources should be in the same directory.
Collision Detection
Simple use of arectangle object that represents the space occupied by the on screenobject, using the "IntersectsWith" Method we can find if otherrectangles overlap.
Physics
The in game physics are rudimentary at thisstage, Basic Trigonometry is used to determine the new point locationof each object based on speed and direction.
Sound
winmm.dll is imported using InteropServices and its PlaySound Method is used for in game sound.
Performance
The performance is bad very bad, if you try toplay full screen you will experience significant lag unless you've gota high end PC, and if you play with 4 startingasteroids… well don't say I didn't warn you!
The killer lines are in the form constructor:
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
Unfortunately this is what you do to implementdouble buffering… and without it, well just comment them out and seefor yourself.The Timer interval is 50 so refresh is about 20 fps.. maybe this couldbe less.
Conclusion
I hope you find this useful and fun, I plan toimplement more features in the immediate future, I would be veryinterested in hearing any comments on performanceand alternative ways to handle display with GDI+. Please visit theforums at www.aurora-soft.co.uk
Download the Source Code
Thanksto:
Mike Gold's article on space Invaders – for the sound implementation and some of the in game sounds












No comments yet... Be the first to leave a reply!