Friday, October 22, 2010

Input System: Part 5 Thumbsticks

This tutorial will go over creating two basic functions to use in our input class that will use the thumbsticks. These two functions will be identical in concept to the triggers but will involve a little more work to get the same result. Thumbsticks are very important to most games so having an easy function will probably save a lot of trouble. Before I start I would like to first say that the thumbsticks use a normal Cartesian coordinate system where the center is the origin, that is you haven't moved the thumbstick at all. To the right is the positive X, left is negative X, up is positive Y and down is negative Y. This is slightly different from the screen coordinate system where down is positive Y and up is negative Y so that is just a heads up. 

So we want to create two functions one that will return the value of a thumbstick ( left or right ) and another that will return a bool for if we passed the threshold in any direction ( left, right, up or down ). So were going to start by creating two enums one for the thumbstick and another for each direction. The thumbsticks enum is the same as the trigger enum so we could combine to one enum, but I doubt having two enums with the same values will make any performance issue and the clarity we get is really nice.

Sunday, October 10, 2010

Input System: Part 4 Triggers

This part of the tutorial will cover using the triggers on the game pads. The triggers give a value between 0 and 1 indicating how far the trigger is pressed, 0 being not pressed and 1 being fully pressed. There are two common ways we use triggers, using them as a button like shooting a gun and using them as a trigger were you use the value between 0 and 1 to correspond to an action such as a gas pedal of a car. The first step were going to do is to distinguish between our left and right triggers by creating an enum. For simplicity reasons I created this enum inside the Input file directly above the input class.

Next I created a const variable to distinguish the threshold to pass in order for the trigger to act as a button. You can change this value or even remove it entirely and have it as a parameter in your function, but I found that I always just used one number in all cases so it made the most sense to declare a constant.

Next were going to create the two functions were going to use for our triggers. Trigger will just return the value of that trigger and Trigger Threshold will return if we just passed the threshold or not.

Saturday, October 9, 2010

Input System: Part 3 Keys and Buttons

Due to the extremely easy nature of this part of the tutorial there will not be much in terms of explanations. This part is going to cover key presses, button presses, key down and button down methods that we will add into our input class. First were going to make our prototypes, all these methods return bool so we can easily use them with an if state. Note the red underline is because they don't return anything which is required for non-void functions. Also click on the pictures if you can't read them.

Simply enough return if the key is down for the key down method. And in the key pressed return if our previous states key is up and our current state key is down.

Friday, October 8, 2010

Input System: Part 2 Input Basics

So for the first part of the input class were creating the basic aspects of the input system. The input system will handle controls for the game pads and keyboard we will not be needing the mouse since the mouse does not work on the xbox and well I never really use it. Keyboard controls are good for simple debug commands such as pressing F for full screen or pushing a button to go back to the main menu without having to use a game pad button. Now were going to start with two functions, one that will initialize our variables since static classes can not have a constructor and another one to update our game pad and keyboard states so they will be named Initialize and Update. The class itself will just be called Input and will be a static class therefore our functions must be static as well. Here is what our class looks like so far except that your namespace will probably be different. 

Wednesday, September 29, 2010

Input System: Part 1 Static Classes

This is going to be a mini tutorial on how to create a simple input system now there are many ways you can go about creating an input system and this is just my approach and is in no way the best but works for what I use it for which is all that matters. Now before I go into the code itself I want to first discuss static classes since there are major discussions with why static classes are bad and should be avoided and such. I will also try and explain when to use static classes and how to use them in relation to c#. 

Now lets start with the good parts of static classes. First off static classes do not need to be created they are automatically created at runtime and because of that these classes can be accessed from anywhere in your code without creating any new variables. This is the main reason why we are using a static class for our input because we want to be able to get whether or not buttons were pressed from not only our main class but a lot of sub classes will also need them. Because there is only one instance of the static class you will never need to create multiple instances of the input class which will simplify a lot of code. 

These characteristics of static classes do come with some downsides though. Now because static classes are created at runtime automatically they can not contain and constructors which in our case is not a problem. And because there are only one version of the class objects like bullets or particles are completely out of the question. Also people do not like it when you continually  reference static classes in your code because it makes it so they can not reuse only part of the code they would need the extra static class for it to function. Now this argument makes sense in a lot of ways but most of the time for me it is the static classes that I want to reuse anyway. For example whenever I create a new project I will instantly copy and paste in my input class and my base game class and build the game up from that. I can see it if someone was to use nothing but static classes to do everything in their game that it would get quite annoying to have to look at all the different classes to understand what is going on, but in this example our input class will be so self explanatory that this will be unnecessary. If this doesn't make complete since it should when I go over how to create and use static classes in the next part. 

Now I do not know exactly what static classes do behind the scenes but this is what I have read and noticed when I use them but I could be wrong in some parts. If you want to do some extra reading you can go here. This was a little more then I wanted to do on static classes since most of this will make since when I show you the code in the next few parts but oh well. Should have the next one ready soon. 

Tuesday, September 14, 2010

Fun Computer Prank

So I was at my school in the computer lab when my friend and I were messing around with the command prompt when we combined forces to create a fun prank. This prank requires access to the command prompt so if your trying it on a school computer make sure you can use it. Now the first step is to take a screenshot of your desktop by pressing the print screen button located above the insert key. Then go into Microsoft Paint and paste it in there ( Control + V ) then save the image ( Control + S ) somewhere like the desktop. Open the image and set it as the desktop background. Then you have to delete all the desktop icons if there are any, these are easily recoverable from the recycle bin or just replacing them so its not that bad. You can also open up a program that takes up all or most of the screen and take the screenshot instead of being confused on the desktop they will try and use or close the program. I would suggest using a common program like internet browser or just a folder thats open. So far this is just a normal prank but this is when things get fun.

Next your going to open the command prompt and type in the following command.

taskkill /f /im explorer.exe

This command ends the task of the windows explorer. You may think the explorer is just to browse your folders or using the internet but explorer does a lot more. After you do this command the task bar is removed and what is showing at the bottom is instead the desktop background which means it is worthless. The start button also doesn't function anymore and no new windows will open. If you close the command prompt there is no way for them to do anything other then restarting which will restart the windows explorer and everything will work just like it did before. If you do not close the command prompt the command that will restart it is.


This will turn back on the windows explorer and return everything to normal and will not do any damage to the computer or anything like that. Besides deleting the desktop icons and changing the desktop background but those are easily fixed.

To long didn't read:
1: Take screenshot of desktop
2: Set background to screenshot
3: type in "taskkill /f /im explorer.exe" into console
4: close console
5: ???
6: profit

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.

Wednesday, June 16, 2010

2D Particles: Part 8 Even More Options Part 2

Welcome to the eighth part of the 2d particles tutorial. It has been a while since I last posted and hopefully I post more frequently now that school is over and its summer. So lets get to it. In this final part of the tutorial we will add particle fading in and out as well as color interpolation. This is not very difficult all we are going to do is add in an enum representing the different styles of fading which we will use to update our particles. Also we will add in three color variables in the particle class, a starting color, ending color, and one to use to draw. And that will be all for this tutorial. I might do a slightly more advanced particles tutorial later that will use emitters and potentially be in 3d not 2d. So without any more delay lets begin by creating our fade style enum in our particle class above the Particle class itself, see source if you do not know where.

Wednesday, May 12, 2010

2D Particles: Part 7 Even More Options Part 1

Welcome to the seventh part of the 2d particles engine today we will be adding in some more variation to the engine. Also we will be making it run slightly smoother through something I should of realized a long time ago but didn't because I had an idea with it that I probably will not be adding. So lets get to it, first thing were going to do is add and remove some variables from our particles class. We removed the texture variable while adding scale, Acceleration and Origin. Instead of each particle having its own texture because there all the same were just going to use one from the particle engine class. Acceleration and origin will be the same for every particle and for every instance so were going to declare them within the particle engine class when we initialize our particles. You could have also set them in the constructor but there might be more variables like this later so this is fine for now.

Saturday, May 8, 2010

2D Particles: Part 6 Optimization

Welcome to the sixth part of the particle engine tutorial today we will be optimizing our code to run faster. If you have been following along you will remember the graph in the last tutorial were our particles took up 54% of the memory after only a minute. After the adjustments we will compare our new graph with our old graph. First thing we have to do is get rid of that nasty list in our particle engine and replace it will an array and queue.
Now the goal of using an array and queue is to take a particle from our free particles queue and enable it within our array. This is going to change how we create our particles so lets change that first. The particles constructor will no longer set all the properties instead we will create a new method named Initialize to do that.

Wednesday, May 5, 2010

2D Particles: Part 5 More Options

After some time I have finally put together part 5 of the particles tutorial. The reason I wasn't sure of what to do for part 5 will be more clear later on. In this tutorial we will be adding more options to our particle engine, as of right now everything is predefined and pretty boring. Well lets change that now. First were going to change our const speed, rotation speed and life time to a minimum and maximum range. We are also going to add in a minimum and maximum angle.

Sunday, May 2, 2010

How to: Use comments with intellisense

Now I am not much of a comment user but there are some times when comments are very helpful and since I am going to start adding comments in my tutorials I thought I would briefly explain how to use them before hand.

One reason I use comments is to signify that something will be changing. The reason I do this is so that in case I forget to change it when I look back on the code I can remember. Now this isn't the best way to use comments as it doesn't really help the readability of your code or your coding efficiency.

The next reason I comment stuff is for things that I may find confusing if I were to look at it in a month. Things such as variables that are sort of complicated, code blocks with a lot of math or random things that look like they don't belong. Other things may include brief explanations for the designer who will be changing that specific variable or group of variables.

Saturday, May 1, 2010

2D Particles: Part 4 Using the Current Engine

So short tutorial today that will cover adding in our current particle engine to a simple game. Jumping right into it all we need to declare in game1 will be a position vector and our engine.
The following code is optional and will remove the 60 frame rate limitation which shows that the engine doesn't rely on the default frame rate, I also changed the screen resolution so that it had more room to show the particles.

Friday, April 30, 2010

2D Particles: Part 3 Basic Engine

Welcome to the third part of the 2d particle engine tutorial. Today we will be adding our basic particle engine class that will hold all of our particles info. Now what we want this class to do is load, create, update and draw our particles as well as creating new ones. Now before we begin let me say that this class is the core of our engine and will be very complex in the end so throughout the tutorial as we add in more stuff I will also be adding in comments to help explain what everything does. There are some special ways to adding in comments that I will explain part way between the tutorial that will help when coding large projects.

So lets begin, first lets create a new class named ParticleEngine and add in the following variables.
Now lets go over the variables we added since some of them might not be obvious. Our random variable is going help us in creating a lot of completely different particles using random numbers. Our list of particles holds our particles and texture is the texture of our particles. Our speed variable is the speed of our particles, amount is how many particles we want to create every second and our time per particle lets us know how long it takes for each particle. This is similar to our sprite animation updating. Lifetime is how long our particles will be alive which we set to 1 second, our speed of rotation for our particles and the blend mode we will be using for our drawing. This is set to alpha blend now but additive is also a good one. The last two keep track of how many particles we have to create this frame and how much time has passed before our next particle is created. More explanation when we go over the updating method.

Thursday, April 29, 2010

2D Particles: Part 2 Basic Particle

Welcome to the second part, first actual programming part, of my 2d particle engine tutorial. Today we will be creating a basic particle class which will be expanded later but for now it will be simple and clean. Now lets begin, first create a new class named Particle and add the following variables into it. All of these variables are private because we just need them in this class except for IsAlive which we will use to destroy the particles later.
Lets go over each variable. Texture is just the texture of the particle that we will also use to calculate origin. Position is the position that our particle is currently at. Velocity is how fast our particle is moving, more on this later. Rotation is the current rotation of the particle and rotation speed is how fast we are rotating. LifeTime is how long our object will be alive when we first set it and lifeLeft is how much time is actually left before we destroy our particle. IsAlive is whether or not the particle still has time left. This is pretty easy so far, now were going to add the constructor which we will use in a different class to create our particle. We basically want to set all the variables in the constructor because its easier than assigning each value in our other class.

Wednesday, April 28, 2010

2D Particles: Part 1 The Basics

My next tutorial will be on a 2D particle engine. A 2d Particle engine is pretty simple to create, but requires a lot of variables. Therefore it can be overwhelming at first, but hopefully I can break it up in a way that everyone can follow. The engine itself is just a way to control your particles, and the particles are just a texture that is moving, rotating, scaling, and fading. Although there will be a lot of variables in this engine, with a few math tricks and the random number generator, you can create very cool effects with minimal code.

Tuesday, April 27, 2010

2D Animation: Part 7 Using Custom Animations

Welcome to part 7 of the 2d sprite animation tutorial this will be essentially part 2 of using custom animations were I will go over using the new custom animations to allow you to move left, right, up and down with an idle state for each direction using just two animations, an eight frame animation for moving and a four frame animation for your idle state. I would suggest downloading the Dude sprite sheet in the source code at the bottom of the page and following along using your part 6 source code as it is the exact same for now. So lets declare our two main variables, our keyboard state and our dude.
Now that we have our dude animation lets load him in using our generic constructor and setting his position as usual. Now instead of just a name, row and frames where now going to add in an Animation Class which for typing reasons i named 'ani'. Now our animations are all the same just at different rotations so with each new animation we just have to change the rotation and add it to our dictionary just like this.

Monday, April 26, 2010

2D Animation: Part 6 Custom Animations

Welcome to the sixth part of my 2d sprite animation tutorial so far we have covered adding in sprite sheets with multiple animations but until now our animations are all the same except for the images. This tutorial is going to cover adding in a custom animation class that will allow us to have multiple animations with multiple settings such as different scales, size, rotations and more. Right now our animations are nothing more than a name and an array of rectangles with there properties being defined as a whole now we could change the settings of the animation whenever we change the animation but that can be troublesome so instead were going to make our own animation class.

This class is going to define what the animation settings are and will be on a per animation basis so that we can have complete control over our animations. So lets start by adding in our animation class which I named AnimationClass, I was going to call it just Animation but that runs into some other naming issues so Animation Class is fine. We already have all the properties of our animations in our sprite manager class so were going to cut and paste them into this new class. We are also going to add in the Is Looping boolean from the sprite animation class in here as well. The result of our animation class is the following.
Now we have our animation class but we now need to use it in our sprite manager class so were going to change the Rectangle array in our dictionaries to Animation Class, we will get the following for our sprite manager class.
After that we need to change our sprite manager constructor slightly to set the variable frames since some animations might have more frames than others we do not want this to be a global variable but instead a variable for each animation. We still want to know how many frames are in the total image so that we can calculate our width, height and origin but that is all.

Sunday, April 25, 2010

2D Animation: Part 5 Multiple Animations

Welcome to the fifth part of my 2d sprite animation tutorial, today I will be going over how to add in multiple animations. So for this example we are going to have our guy moving left and right with only a few modifications to our current sprite manager and sprite animation class. Before we begin lets think about how we want to arrange our animations, our final goal is to be able to create, check and assign our animations easily. For now our animations are nothing but an array of rectangles but we will extend this later. Now we can make a list of arrays and access them by index but that can get pretty irritating trying to remember which animation is which number. So instead we will use a Dictionary, a dictionary in C# works pretty much just like a normal dictionary, allow me to explain what a dictionary is and why we are choosing a dictionary type for our animations.

A dictionary is a type of collection with two variable types, one is the key and the other is the value, for our animations we are going to use a string for our key and a rectangle array for our value type. So we are going to replace our rectangle array with a dictionary, we are also going to store a string for our current animation simply named Animation, which we will use in our main class to change our animations or check which animation is currently playing.

Saturday, April 24, 2010

2D Animation: Part 4 Sprite Animation

Welcome to the fourth step in my 2D sprite animation tutorial, to follow this tutorial you are going to need to follow the second part which you can find here. If you followed part 3 of this tutorial you will find the first couple of steps very familiar. This tutorial is going to cover how to make animations that will update continuously, for this tutorial it will be a loading circle that . Now lets begin, lets start by creating our class named SpriteAnimation. This class is going to inherit from our sprite manager class just like in the frame animation. Because our sprite manager had a constructor that took in parameters we have to first initialize our constructor to allow us to use the sprite manager. So this is how our class should look so far.

Friday, April 23, 2010

2D Animation: Part 3 Frame Animation

Welcome to the third part of the 2D animation tutorial. I am going to assume you have the sprite manager class from part 2 which you can find here, or download from the source at the bottom of the page. This tutorial will cover the creation of frame animations which are animations that go from one from to another but not always in order. You could have the next frame be based on something that happened or like in our example a random frame when we press the space bar. Now before we begin adding in the frame animation class lets first do two things, one create our sprite sheet now I used my Sprite Tool to create a dice sheet that you can see here.

Dice Sprite Sheet

Thursday, April 22, 2010

2D Animation: Part 2 Sprite Manager

Before I get into the second part of my 2d animation tutorial I would like to mention that I updated project santa which is available here.

Now lets begin, to create our two separate sprite animation types where going to first create an abstract class that will allow us to keep track of the similar functions in one class instead of two. We will call this class the SpriteManager class. The way I like to program is to start out with the base functions first, add in the functionality to the functions while adding any extra functions that simplify the process or changing old functions to work with the features. So first where going to add in the two main functions, the constructor and draw method.

So far were really not doing anything but we know for sure we need to use these functions so we can add them write away. Next lets start adding all the variables where going to need to have to load and draw in our sprite sheet, for now were going assume there is only one animation. Well we know that to draw our image were going to need a Texture2D, Position, Color, Origin, Rotation, Scale, SpriteEffect and an array of rectangles. Now one thing for sure that needs to be loaded in by default is the texture and one practice I have is adding the required variables into the constructor. One bonus to this is that we can then create our array of rectangles within our constructor as well. So for now where going to make the following changes.

Wednesday, April 21, 2010

2D Animation: Part 1 The Basics

This is the first part in a five part series about 2D animations, I hope you will learn from this tutorial and if you have any questions, need help or whatever feel free to post a comment and I will get back to you asap. Now before I go into detail about animating in 2D I thought I would explain how exactly animating in 2D works, the different types of animating and my overall approach to simple animations. First 2D animations are not really animating at all, instead there just changing images very quickly to make it look like there animating, this is just like when you draw on multiple pages of a book and quickly flip the pages to see something move. To make this work you can either create a lot of images and draw them one after another but this can be very troublesome so instead most people create sprite sheets.

Sprite sheets are essentially a lot of images combined together to create one large image. These images are either arranged in a very specific order that the programmer can then use or after the sprite sheets are created there is an accompanied text or xml file that will give the programmer the correct locations of each image. I created a sprite sheet creator a while back that I have finalized and posted a few days ago, click here for the sprite tool ( I will be doing a video tutorial on how to create the sprite tool from start to finish in a while so check back later for that ).

Tuesday, April 20, 2010

Recommended Program: Dropbox

Dropbox is a simple program that allows people to share files just by dropping them in the dropbox folder that you specify. It is similar to a subversion repository except for it will automatically update whenever something is added or removed from the folder. This might not be ideal for all situations but for people who like sharing a lot of files between each other or a small group of people it is pretty nice. Also because your files are stored on the dropbox server you can not only get your files back if your computer breaks down but you can download them from the dropbox website from any computer directly from there website.

Monday, April 19, 2010

Basic Sprite Sheet Creator

The next program I would like to share is my sprite sheet creator. Using sprite sheets is, for the most part, pretty tedious especially to the new programmer. First, this sprite sheet creator is only meant for simple sprite sheets such as dice, loading animations or something similar to that. This post will just be an introduction with a brief introduction on how to use the tool with a screenshot, and latter I will explain step-by-step the code in the creator plus code that will take your sprite sheet and allow you to animate it in two different ways.

Screenshot of Sprite Tool

Sunday, April 18, 2010

Recommended Program: Ventrilo

This will be the first post in my series of recommended programs which will include programs for people who are developing games, beginners to programming, web apps or just programs that can be used for everyone. First I would like to start this off with a program I use every day and for good reason. Ventrilo, it allows people to connect to a server and talk to each other on a fast and lightweight program. I use this program to talk to friends while we play call of duty or to talk about our current game projects or just because were to lazy to type. Its very easy to set up even if you want to host your own server.

Preview of next few projects

I will be releasing a couple of projects and maybe a tutorial or two. First, I will be posting my very simple sprite sheet creator for very simple sprite sheets that are perfect for games that just need a few animations. Second, I will be posting the source code and example of my 2d particle engine which consists of just two classes. Also, I might do a tutorial on 2d grid based path finding using A*, but that is fairly complex so it might take some time to get it all done in a way that makes sense. Finally, I will most likely be posting some screen shots and a brief description of my current game project for which I am working on with my friend Carlos. More information can be found here.

On an unrelated note, kick ass was a great movie.

Friday, April 16, 2010


This is the first post of my blogging life and hopefully I keep up with this and help some people learn about programming or to laugh a little. I have a lot of programming topics to write about, things like using c# and mysql to create programs, setting up your own server and some xna game studio help classes to speed up your development. I will hopefully post my first real blog in a day or so about my project manager which uses c# and mysql to allow teams to stay organized with tasks, groups, announcements and more. I just have a few final adjustments before I upload it on to Codeplex.