MGS: Pong (Pages 4-6)

 

In this fourth [page, second post] installment of the MGS: Pong article series, I am going to cover how to draw a background for the game. Before I start, I should tell you that there will be two more articles: one for creating the menu system and one for pulling all the classes together. Okay, let’s begin! The background class (Background.cs) is incredibly easy to create. First we need some private members to draw the background.

1
2
3
4
5
6
7
8
9
public class Background : Microsoft.Xna.Framework.DrawableGameComponent
{
    #region Private Members
    private SpriteBatch m_spriteBatch;
    private ContentManager m_conManager;
 
    private Texture2D m_bgTexture;
    private string m_texSource;
    #endregion

The next thing we need to do is handling how to load and unload content. This is nothing new, so no explanation is required.

Next we handle drawing! Again, this is nothing new so I will just let you see the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#region Drawing
public override void Draw (GameTime gameTime)
{
    base.Draw ( gameTime );
 
    // If the texture doesn't exist, we can't draw!
    if ( m_bgTexture == null )
        return;
 
    // Start drawing.
    m_spriteBatch.Begin ( SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState );
 
    // Draw the paddle.
    m_spriteBatch.Draw ( m_bgTexture, Vector2.Zero, Color.White );
 
    // End drawing.
    m_spriteBatch.End ();
}
#endregion

Finally we have a property so that we can set the texture source from outside of the class. As said before, the next article will cover the menu system.

1
2
3
4
5
6
7
8
9
10
#region Properties
/// <summary>
/// Gets or Sets the source of the paddle's texture.
/// </summary>
public string TextureSource
{
    get { return m_texSource; }
    set { m_texSource = value; }
}
#endregion

Pages: 1 2 3

Tags: , , ,

One Response to “MGS: Pong (Pages 4-6)”

  1. Tom Harris Says:

    Hi John,
    I am a long time Basic/VB/VB.NET developer. I use C# once in a while but our company has made the decision to stick with VB.NET(Using VS 2008). Long story short do you have any examples of XNA (PONG would be a great start) written in VB.NET? I would be interested and know that 100’s of Thousands of the boy and girls, men and women who use VB.NET throughout the world would be interested too.
    From setting up the development environment to using your frameworks would be an awesome start.
    Thanks,
    Tom Harris
    9d Interactive
    tharris@9d.com

Leave a Reply

You must be logged in to post a comment.