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.