Tuesday, May 3, 2011

XNA Recipes

public void LeftThumbStickMove(GamePadState gamePad)
    position.X += gamePad.ThumbSticks.Left.X * speed;
    position.Y -= gamePad.ThumbSticks.Left.Y * speed;         

public void RightThumbStickMove(GamePadState gamePad)
    position.X += gamePad.ThumbSticks.Right.X * speed;
    position.Y -= gamePad.ThumbSticks.Right.Y * speed;

public void DPadMove(GamePadState gamePad)
    if (gamePad.DPad.Right == ButtonState.Pressed)
        position.X += speed;
    if (gamePad.DPad.Left == ButtonState.Pressed)
        position.X -= speed;
    if (gamePad.DPad.Up == ButtonState.Pressed)
        position.Y -= speed;
    if (gamePad.DPad.Down == ButtonState.Pressed)
         position.Y += speed;

// Bounds checking
sprite.position.X = MathHelper.Clamp(sprite.position.X, 0, GraphicsDevice.Viewport.Width - sprite.width);
sprite.position.Y = MathHelper.Clamp(sprite.position.Y, 0, GraphicsDevice.Viewport.Height - sprite.height);

// Draw text to screen
spriteBatch.DrawString(font, "Some text here", new Vector2(20, 45),     Color.White);

Saturday, April 30, 2011

XNA Tutorial (Part 1: Introduction)

Microsoft's XNA framework allows developers to create exciting games for the PC, Xbox360, Zune, and Windows Phone 7. In these tutorials I will explain how to use the XNA framework with Visual Studios and C#. I make the assumption that you are already familiar with programming, the C# language, and some version of Visual Studios (express edition is fine).

System Requirements

Once you have everything installed on your computer open up Visual Studios. Then navigate to file -> New Project. A new project dialog box will pop up. 

Select the Windows Game (3.1) template from the available project types and rename the solution if you like. Click ok and a new solution will be created for you. 

Creating a new Windows Game in Visual Studios Professional 2008
If you run the solution as is you will only get a window with a blue background. However the solution has all the basics you need to start writing your own video game.

Looking in the solution explorer we can see that by default XNA creates: two code files for us named Program.cs and Game1.cs, two image files Game.ico and GameThumbnail, and one special folder named Content. Program.cs is the entry point of the application and contains our Main() method. Game1.cs can be thought of as our games 'Engine', and as such I often rename the file to Engine.cs.  This file exposes many useful methods of XNA and will be the base we build our game on. The Content folder is a special folder in XNA that we will place all off our game assets in. Things like images, sounds, 3D models, effect files, bitmaps, spritefonts, etc.

By looking at the pseudo code below you should be able to get an understanding of the basic flow and logic of a XNA game.

Game1() - General initialization.
Initialize() - Game initialization.
LoadContent() - Load graphics resources.
Run() - Start game loop. Can be found in Program.cs.
    Update() - Get input, perform calculations, and check for game ending criteria. 
    Draw() - Render graphics.
UnloadContent() - Free graphics resources. Not used often in smaller projects. .Net takes care of freeing resources with garbage collection.

Understanding the concept of the game loop is important to understanding how XNA works. Unlike some business applications that are 'event based' and wait for user input before doing something, XNA must do something even when the user is not supplying input. .

Tuesday, February 22, 2011


ShadyGames is a new website created by me to host tutorials on game creation and my own games. I am very excited to be working on this project and hope to add lots of content soon.


Thursday, May 27, 2010

C# Notes

Looping threw a range
foreach(int i in Enumerable.Range(min_number, max_number))

String to int

Reading a text file
StreamReader sr = new StreamReader(file_path);
string data = sr.ReadLine();

Checking for whole number
if((someValue % 1) == 0)
    Console.WriteLine("someValue is a whole number");

Create random int
Random random = new Random();
int randInt = random.Next(min, max);
Generate md5 hash from string
string GetMd5Hash(string inputString)
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

    byte[] byteString = Encoding.UTF8.GetBytes(inputString);
    byteString = md5.ComputeHash(byteString);

    StringBuilder stringBuilder = new StringBuilder();

    foreach(byte b in byteString)

    string hashString = stringBuilder.ToString();
    return hashString;

Wednesday, May 19, 2010

Formatting an external hard drive in windows

Recently I was playing around with a 160GB western digital hard drive. I came across some adapters to plug in the HD via USB. I needed to format the drive so I went to 'run' and typed in "diskmgmt.msc" with out quotes. This runs windows disk management utility. I found my drive, right clicked, and hit format. The program took about an hour to complete the format.

Wednesday, March 17, 2010

Basics of Cron Jobs

Cron is the name of the program that enables *nix users to execute commands or scripts automatically at a specified time/date. It is normally used by sys admins, to run back-ups or do other repetitive tasks like the command makewhatis, which builds a search database for the man -k command. However it can be used for anything. A common use for it today is connecting to the internet and downloading your email.

For today's example, which will be used to teach you the basics of what a cron job does, I will use the command:
rm /home/username/htdocs/*~

This command tells the machine to delete all of my backup files. Generally, *nix-based machines put a ~ after the file name to denote a backup. For example, if my machine froze while I was editing a file named work.php, the system would save a file named work.php~. If I didn't notice this, and work.php had database configuration information in it which would include my database name and password, then a hacker may be able to access the source of work.php by going to mysite.com/work.php~. Obviously, I don't want my passwords floating around, or source for my code, in some cases. Note: * is a wildcard in *nix, so I'm saying delete everything that ends in ~.

The basic cron job has 2 or 3 parts: date/time, command, and the log file [optional]. The date/time is formatted so that you can tell the machine how often to perform the task in a simple format:
minute hour day/month month day/week

For the day of week, 1 is monday, and therefore 0 is sunday. However, 7 is also considered sunday. Also, the cron job will execute when either day/month or day/week is true. Here is a common diagram of the fields that I used alot when I was new to cron jobs:

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- day of week (0 - 6) (Sunday=0)
| | | ------- month (1 - 12)
| | --------- day of month (1 - 31)
| ----------- hour (0 - 23)
------------- min (0 - 59)[/code]

The command for editing cronjobs is:
crontab -e

other parameters for crontab are:
crontab -l = display cron file
crontab -r = remove cron file
crontab -v = display last time crontab file was edited (not universally available)

Go ahead and type that first command, the one with the -e parameter. If you don't know vi, or you are pretty confused as to what just came up. I recommend learning vi, it's awesome, but in the mean time, just go ahead and type:

This should let you enter inset mode. Now, type:
* * * * * rm /home/username/htdocs/*~

then press the Esc key and then type:

Congrads your cronjob has been installed! It will now delete all backup files in your htdocs folder every minute.

You can also make log files. Go ahead and execute this command in your home folder:
mkdir cronlogs

Now, go back into your cron file like you did above and add the Following after the command:
> /home/username/remove_backups.log

your cronjob will now look like:
* * * * * rm /home/username/htdocs/*~ > /home/username/remove_backups.log

There we go! You have now made your first cronjob. However there is another way to use cron jobs. In the /etc directory you will probably find some sub directories called 'cron.hourly', 'cron.daily', 'cron.weekly' and 'cron.monthly'. If you place a script into one of those directories it will be run either hourly, daily, weekly, or monthly, depending on the name of the directory.

Thanks for reading, hope this helps people new to *nix.

Monday, February 15, 2010


Lite-Library is another free open source project that I have started. My goal is to build an OPAC like application that will allow users to add, edit, and remove books from there library. I am also implementing a search feature. So you may search your library by title, author, publisher, genre, or isbn number. This project is pretty simple but once we have this base down we should be able to start adding features from our wishlist.

The project is of course hosted by Google code, using the mercurial repository. The application will be written in Python, using wxPython for its GUI, and SQLite for its database. If you would like to join the project please contact me shadytyrant@gmail.com.