Thursday, July 3, 2014

In the previous article we saw how to build an XNA project and how to display and move items on the


In the previous article we saw how to build an XNA project and how to display and move items on the screen. Then I suggest you begin to expand a bit our shooter allowing the ship to fire a laser, we also will prevent the screen out of the way collisions manager ;) The goal is that when the player support the space bar a laser is fired up the screen, it is not necessary that the ship can fire a burst of laser (not yet) so it will check that it is no longer visible before the vessel can draw another. Implementation of the project
There is very simple we will resume the last project available in the previous section and add a new graphic resource: "giga laser" laser, so right-click the link above and then click save as ... Finally copy this file image in your Content folder if you use monogamous or call directly drag the Content project associated with your game plan if you use Windows, Visual Studio and XNA. Implementation of the laser in the code
The code is very simple oga we will add 4 new private attributes: An object of type Texture2D represent the texture of the laser; A Vector2 structure type that will be used to direct the laser; A variable of type int that defines the speed of the laser (it is optional, but it is preferable, for the sake of cleanliness not to enter too many values oga hardcoded in the code); A variable of type bool that will tell us if we can make a new laser or not. public class Spacegame: Game {/ / Background and ship / / ... / / Variables laser laser Texture2D; Vector2 laserPosition; int Laserspeed; bool shooted; / / Constructor ...}
In the Initialize () method will now faloir instantiate two variables, the speed will be set at 4 and we can take a first laser the first time. canShoot = false; oga / / The laser was not fired Laserspeed = 4; / / Speed laser
In loading and unloading of laser texture always happens in the LoadContent oga () and UnloadContent () / / In LoadContent () = laser Content.Load ("laser"); / / In UnloadContent () laser.Dispose ();
Time to move on, we need to create a method that will fire a laser at the screen. This method oga will be responsible for detecting whether or not we can pull a new laser on the screen and whether to initialize the position of the laser on the screen. The laser is fired through the nose of the ship, which means that to determine the starting position we must take into account: The X and Y position of the vessel The image size of the vessel size image laser
The x position of the laser is determined by the position of the vessel in x, not only we can not simply write positionLaser.X = positionShip.X because the laser will be pasted to the left of the vessel (Figure 1). It is moreover necessary to add its position to the half of the width of the vessel (FIG. 2). Naturally to be centered subtract half the width of the laser at this value (Figure 3).
To set the y position of the laser will be taken the same as that of the vessel minus the height of the texture oga of the laser as it is at the correct height. Thus the starting position of the laser will be perfectly centered. (! shooted) private void shoot () {if {/ / The position of the laser is defined by: / / - the X position of the vessel + moitier its size / / - moitier least the size of the laser laserPosition = new Vector2 ((shipPosition.X + (ship.Width / 2)) - laser.Width / 2 shipPosition.Y); shooted = true; / / The laser is fired! }}
This method is very simple, oga we initially test the player can fire a laser, if that is the case then the position of the laser is positioned on the nose of the ship. It then passes the shooted variable to true to indicate that the player has fired.
In the update () method we have 3 tests to make: If the laser is fired must increment its x-position for that rises to the top of the display if the laser is fired AND it is no longer visible must stop the update and allow the player to fire again if the player supports the space key, it fires a laser (if possible)
This gives us the following code: protected override void Update (GameTime gameTime) {/ / If the laser is no longer visible and was fired / / We do longer updates if (canShoot && laserPosition.Y + laser.Height <= 0) canShoot = false; / / We can now remove another laser / / If the laser was fired up is done if (canShoot) oga laserPosition.Y - = Laserspeed; / / Tire laser if (Keyboard.GetState () IsKeyDown (Keys.Space).) Shoot ();

No comments:

Post a Comment