Sunday, February 19, 2012

XNA Tutorial: 2D Shooting

In this tutorial we are going to build on what we did in the last one and make a shooting rocket. I have refactored the code we did the last tutorial into classes and methods. Before we start you are going to have to download the project file here.

After you have downloaded the project file and opened it the first thing to do is add a new class and call it Bullet. We are going to model this class on our rocket class. Add your using directives.
Now we need to add these properties.
Now lets create our constructor and the update method. It's relatively similar to the rocket constructor and update. The only thing that's missing is the rotation. The draw method will also be nearly identical to the draw method of the rocket class. The fact that the bullet class and the rocket class are so similar would mean that we could have used inheritance. Where we would create a base class let's say a "Sprite" class and have both the rocket and bullet classes inherit from the sprite class. Perhaps in a future tutorial we will do that but for now lest stick with this method.

Now that the boring part is complete let's think about how we will handle the bullets. We want the player to be able to shoot a bullet every time they press the left mouse button and have it continuously shoot if they have it pressed down.

Next we need to modify the rocket class the rocket class will manage the bullets. Add these at the top below the properties and before the constructor. Now modify the constructor so if looks like the following Next thing to modify is the update method. In here we want to check if IsShooting is true and if it is increase the shot timer. If the shot timer is greater than the time between shots then we create a new bullet and give it the the same position and direction as the rocket and add it to the list of bullets. After we do that were going to have to loop through the list of bullets and update each one. We also need to check if the ActiveTime(the max time it can be drawn for) is greater than the TotalActiveTime(the current amount of time the bullet had been drawn). If it is we remove it from the list. That's most of it done. Now all we need to do is draw them and check if the left mouse button has been clicked. To draw the bullets go to the draw method in the rocket class and add the following just above where your drawing your rocket. One more thing we need to change before checking for the mouse button press. Go to the Game1.cs file and up in the LoadContent method you will see where we instantiated the rocket. Modify it so it looks like this. Now Finally we can check for the mouse button press. Go to the update method of the Game1 class and you should see that we are already checking for our mousestate we will use this variable now. Just after the line that updates the rotation of the rocket at the following code. There we have it, press f5 and run the game to see what you have created. When you press and hold the left mouse button the rocket will shoot bullets in the direction of the mouse cursor. Here is a video of what you should see.

I hope you found it interesting. If you find a typo or a mistake anywhere or have any questions please leave a comment below.

Next go to part 2, where I show you how to improve this system.