Asteroids with FRBpart 3

Get ready, this is going to be a long chapter. First we edit the Rocks class to add more functionality and remove the hard coded numbers. Hard coded numbers, bad. We also add a new private function for when we add the collision detection, to remove a rock when its hit. I show you how to easily put the rocks where we want them, not in the center. Then we edit the GameScreen class because of the changes we make in the Rocks class. We do a test run.Next we edit the PlayerShip class, we add movement functionality. I hope you are excited kids! Only one file you can download at the end of this chapter, so pay attention!Then we edit the GameScreen class again, because of the changes we make in the PlayerShip class. We do another test run!

Step One

Open the Rocks.cs class file, we are going to do some editing.

In the Fields region, remove the iNumberOfAsteroids line, and add these two:

privateint iRockSize = 25;
privateint iRockSpeed = 10;

We move how many rocks we make to the function, with that there is no need to have a class scope variable anymore. We make a default rock size and speed variables instead, getting rid of those hard coded numbers. We will do more with that later, this will do for now.

Now for the next one I'm just going to show you the replacement method for CreateAsteroids:

As you can see, there have been quite a few changes. I made two while statements that make sure they are not spawned in the center, just like the real game. This is one reason why having 0,0 being the center makes things so much easier. You may also notice that a few integers are passed into the function, including rock size and speed as well as MaxX, and MaxY, and of course number of rocks. This makes the function much more versatile. We will need to size these later when we turn them into sprites, with textures.

This will be for when we are able to shoot the rocks, the collision detection happens in the GameScreen where the classes get together and play. So we need an interface to get rid of a rock. The Remove function of the shape manager gets rid of the rock number, Rock. We pass that in the function when its hit. Remember, the rocks are all in a list. After the rock is removed, say it was rock number 2, rock 3 becomes 2, and 4 becomes 3. That is done automatically.

This will wrap our rocks onto the opposite edge nicely, using the same camera function we use again later. As you can see we use the foreach, this is easier then a for next loop sense we are going through a list.

Now we change the Initialize public method This is the new one:

publicvoid Initialize(int NumberOfRocks)
{
// For now we create the asteroids as soon as the game starts, later we
// add more.
// Now we have RockSize, RockSpeed, MaxX and MaxY to worry about. We use
// the screen size for that.
// We also get the size of the screen using the built in camera function,
// though it is from the center to edge so
// multiply it times two for our function that uses the full
// width and height.
CreateAsteroids((int)SpriteManager.Camera.RelativeXEdgeAt(0) * 2, (int)SpriteManager.Camera.RelativeYEdgeAt(0) * 2, iRockSize, iRockSpeed, NumberOfRocks);
}

As you can see, I added the screen hight and width, and number of rocks. The later is then passed on to the createasteroids method that we changed earlier. This is our interface. Using the Camera Relative Edge At function built into the engine we get from the center to edge, now in our function I use the full width and hight, so I double it.

Add this line to the Activity public function:

WrapRocksActivity();

This will call the wraprockactivity function, when its called, from the GameScreen class.

Alright, this is all the changes for now, for this class.

Step Two

Open up the GameScreen.cs class, we now get to work here to make our needed changes.

In the Fields region add this line:

privateint iNumberOfRocks = 4;

This is our default for how many rocks the game starts with. We then can add more later as the game advances.

Change the RockSprites.Initialize method call to this:

RockSprites.Initialize(iNumberOfRocks);

This pases in our amount of rocks we want.

In the Activity public method add this line:

RockSprites.Activity();

This calls our activity method we added that other call to in step one.Now run, if you wait you should see rocks that appear on the opposite edges as the game runs, so that they wrap around the screen, just like the real game.If you have any issues, you can download this chapters project at the end of this chapter, or go over it, and see if you can debug it yourself. That usually helps one to learn.

Step Three

Open up the PlayerShip.cs class file, this is where it gets really fun.

Now here we have al lot of things going on. First in the GetKeyboard method we look to see if the Right Shift has been pressed, if it has it the FireShot method runs where we make a new Rectangle (Read here for help on Rectangles) set its scale to 2, with our 1 to 1 setting, and because the center is 0, it makes a 4 pixel wide and high rectangle. Next we have how long we want the bullet to last before it removes itself. We put that information inside instructions. (Read here for help with Instructions) You can insert statements into FRB objects, part of the engine. Really useful for bullets. Here we tell it to remove itself after 65 tenths and five hundredths of a second. That is what I timed it on the original game at. Next we want the bullet to come out of the nose, not the center of the ship, so we use the rotationmatrix method (read about math functions used in objects here) built into positioned objects. Comes in handy, right. Now, to get the speed right we figure in one forth of the ship velocity into the bullet velocity, and again we use that handy rotationmatrix to have it fire in the right direction. For 2D where 0 is to the right its always Right. Now we add that bullet we put all that data into, into the list. Boom there it is!Then we look for ship movement, first we have to see if the ship is already moving, and spinning. If so stop the spinning, or slow the ship. In the original game the ship slows down slowly. Now we see if you are pressing left or right if you are then it sets the rotation of the ship. Next is the thrust, now here we see just how fast you are going, and if you are not going too fast, then we let you do more. Remember this is a 0 centered game engine. So moving to the left is negative movement, and so is going up. So we must check both ways. Nothing too complected, this is just a tutor after all.

Here we make sure the bullets and the ship have not gone off the edge without warping to the other side. Just like the rocks. Notice the ship one differs slightly, because it can stop, we don't want it flickering back and forth.

This method as you can see is two for loops that go through the lists for Rocks and Bullets, using the built in CollideAgainst function of the game engine we can easily check to see if they collide. As you can see that public function we made earlier goes into effect. We have to break out of the for loop because of the change in count of rocks. You can remove that if you wanna see what happens if you don't. Maybe nothing, depends on what Rock it was on when it got hit.

Add this other line to Activity public class:

PlayerShotvsRocks();

I told you that would be fun, this is the most you have added at one time so far. There is allot of stuff working together here. Now run the project when you hit play, you should be able to move your ship around and fire bullets. Notice the ship slowly slows down just like in the original. Your bullets kind of follow your movement as well as hit rocks making them vanish! See how easy that was! I did my best to copy the original game so far. In the next chapter we add collision detection, then you can hit the rocks!