Saturday, November 28, 2009

Write .ICO files [C#]

Greetings. Here's a console application I wrote, which uses the BinaryWriter to create icon files (.ico image file format). It is based on these specifications.
[Note: .NET ImageFormat.Icon doesn't really write .ico files]

You can drag and drop one or more .NET compatible image files over the app icon (equivalent to console arguments).
The program creates a Bitmap of each image, and checks if it is within the 256x256 pixels limit. Then, the Bitmap data is used by the BinaryWriter to create the converted file.

CreateNew mode on the FileStream avoids file overwriting.
Since the app is targeted to run from its' icon, exceptions are shown via MessageBoxes.

Saturday, November 7, 2009

Snake Game [C#]

Hello again. I wrote a snake game in C#, let me tell you a little bit about it:

The game features two classes of sprites: Vertebra, representing a single snake link; and Pizza, which is, of course, the pizza.
Each of these classes inherits from the PictureBox class, and uses a Timer for a nice animation when these objects appear.

Game pause is achieved simply by start and stop of the game Timer.

There is a top 10 high scores, which is stored in an xml file.
This file is created automatically on the first time it is requested by the game.
The handling of the high scores is done with LINQ to XML, which is awesome!

Here is the code for the Main form:

Here is the code of the Vertebra Class:

And here are a couple of screenshots: