Snake - using the GDI+
Written by Mike James   
Tuesday, 04 August 2009
Article Index
Snake - using the GDI+
Getting started
Persistent graphics


This is an exercise in creating a very basic graphics game using the most basic of graphics facilities.

It illustrates some interesting ideas and techniques and might even have a value now that simpler games are in vogue due to the rise of the hand held device and phone which don't support super 3D graphics.

We start off by looking at some these basic techniques and then apply them to a simple game – Snake.

Topics covered in this project include:

• Using the GDI+ to draw 2D graphics

• How to make graphics persistent using the Paint event

• An easier way of making graphics persistent using the Background

• Using a Bitmap as a buffer to give flicker free animation

• The use of keyboard events to get real time user input


If you have programmed low level graphics then you might well already know about the Windows GDI – Graphic Device Interface.

This is a low level API that gives programs access to the fundamental graphics facilities of Windows and the .NET Framework classes. GDI+ can be regarded as an object oriented extension of this.Of course the GDI/GDI+ has been superseded by WPF but there are still reasons for preferring it - its more efficient, works with less capable hardware and it works with Windows forms based applications. In fact if you just want to add a little graphics to your application the GDI+ is a good choice.

On the negative side the GDI+ is a software implemented graphics system and doesn't take advantage of any graphics hardware acceleration a system might have. So if your real aim is to create 3D graphics you need either DirectX, XNA or WPF.

The examples are all written in C# using C# Express  which can be downloaded for free from the Microsoft website. It would be easy, however, to convert the examples any .NET language such as VB .NET or indeed C++.




Last Updated ( Wednesday, 26 August 2009 )