Tuesday, August 31, 2010

How to: Remove the Game1.cs file

First off, sorry for the absence of posts. I don't really have an excuse other then I didn't know what to post about. Anyway, I have a few quick tutorials on using xna that might help. Now lets begin with the first one. Now when you create your game it comes with a few things and two of those items are a Game1.cs and a program.cs which I have never liked. Now if you were to delete the Game1 and just create a new class that does the same stuff as Game1 your game will not work and that is because of whats in the program.cs file. Now if you have done any console programming for c# you will easily recognize the code block within the program.cs class ( if you have programmed in c++ it is equivalent to the "int main" code block ) . The code block itself is just were the program will start when you execute it and it just says to run your game that it has conveniently named Game1. So all we need to do is change that to our new game that we will create. So lets begin by creating that new game class. So add a new class and name it whatever you want your base game class that I will call Base Game. Just like in Game1 we want to inherit the Xna framework game class and you can add all the variables you want here.
For shortness I deleted all the comments and collapsed all the functions and deleted the unload method but there all the same as Game1.

Now after we have our new game class we need to use it within the code block that is inside the program.cs file. Now this code block does not need to be inside the program class as long as its a static class titled Main it will be executed first no matter where it is. So we can delete the program class and move the Main func
tion into our Base Game class at the bottom. Just be sure to make it within the class and not outside of it by itself otherwise it will not work.
Some extra notes, we do not need the string array as a parameter that's only used when executing your code from a command like compiler and I doubt you would do that over just pressing F5. Without the curly braces we only have 2 lines of code that can be added to if you wanted to add extra stuff such a try / catch block for error checking or whatever.

Well thanks for reading this how to guide, any comments helpful.
Hopefully the next post will not take as long...

No comments:

Post a Comment