Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Asteriods in C# Using GDI+
By Danna Boneheart

Summary

For my first experiment in game design, rather than using some kind of directX wrapper I wanted to see what was possible with GDI+. GDI+ has a lot of nice features but is slow, so not very practical for fast paced games, in this example you can see why. The game also includes sound...hey, it wouldn't be much of a game otherwise!

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 these classes together into the same playing field, handles collision detection and player interaction. Everything is treated on a tick by tick basis via the use of a timer object.

Installation

All source/compiled files and resources should be in the same directory.

Collision Detection
Simple use of a rectangle object that represents the space occupied by the on screen object, using the "IntersectsWith" Method we can find if other rectangles overlap.

Physics

The in game physics are rudimentary at this stage, Basic Trigonometry is used to determine the new point location of 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 to play full screen you will experience significant lag unless you've got a high end PC, and if you play with 4 starting asteroids... 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 implement double buffering... and without it, well just comment them out and see for yourself. The Timer interval is 50 so refresh is about 20 fps.. maybe this could be less.

Conclusion

I hope you find this useful and fun, I plan to implement more features in the immediate future, I would be very interested in hearing any comments on performance and alternative ways to handle display with GDI+. Please visit the forums at www.aurora-soft.co.uk

Download the Source Code

Thanks to:
Mike Gold's article on space Invaders - for the sound implementation and some of the in game sounds :)