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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Texture2D DrawTexture { get; set; } | |
public Vector2 Position { get; set; } | |
public Vector2 Direction { get; set; } | |
public float Speed { get; set; } | |
public int ActiveTime { get; set; } | |
public int TotalActiveTime { get; set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Bullet(Texture2D texture, Vector2 position, Vector2 direction, float speed, int activeTime) | |
{ | |
this.DrawTexture = texture; | |
this.Position = position; | |
this.Direction = direction; | |
this.Speed = speed; | |
this.ActiveTime = activeTime; | |
this.TotalActiveTime = 0; | |
} | |
public void Update(GameTime gameTime) | |
{ | |
this.Position += Direction * Speed; | |
this.TotalActiveTime += gameTime.ElapsedGameTime.Milliseconds; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Draw(SpriteBatch spriteBatch) | |
{ | |
spriteBatch.Draw( | |
DrawTexture, | |
Position, | |
null, | |
Color.White, | |
0f, | |
new Vector2( | |
DrawTexture.Width / 2, | |
DrawTexture.Height / 2), | |
1.0f, | |
SpriteEffects.None, | |
1.0f); | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool IsShooting { get; set; } | |
Texture2D bulletTexture; | |
List<Bullet> bullets; | |
int timeBetweenShots = 300; // Thats 300 milliseconds | |
int shotTimer = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Rocket(Texture2D texture, Vector2 position, Vector2 direction, | |
float rotation, float Speed, Texture2D bulletTexture) | |
{ | |
this.DrawTexture = texture; | |
this.Position = position; | |
this.Direction = direction; | |
this.Rotation = rotation; | |
this.Speed = Speed; | |
this.bulletTexture = bulletTexture; | |
this.IsShooting = false; | |
bullets = new List<Bullet>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (IsShooting) | |
{ | |
shotTimer += gameTime.ElapsedGameTime.Milliseconds; | |
if (shotTimer > timeBetweenShots) | |
{ | |
shotTimer = 0; | |
Bullet b = new Bullet( | |
bulletTexture, | |
this.Position, | |
this.Direction, | |
12, // The Speed | |
2000); // The active time in Milliseconds | |
bullets.Add(b); | |
} | |
} | |
for (int i = 0; i < bullets.Count; i++) | |
{ | |
bullets[i].Update(gameTime); | |
if (bullets[i].TotalActiveTime > bullets[i].ActiveTime) | |
bullets.RemoveAt(i); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreach (Bullet b in bullets) | |
{ | |
b.Draw(spriteBatch); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rocket = new Rocket( | |
Content.Load<Texture2D>("rocket"), | |
new Vector2(200, 200), | |
Vector2.Zero, | |
0f, | |
0f, | |
Content.Load<Texture2D>("Bullet")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (mouse.LeftButton == ButtonState.Pressed) | |
{ | |
rocket.IsShooting = true; | |
} | |
else | |
{ | |
rocket.IsShooting = false; | |
} | |
rocket.Update(gameTime); |
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.