C# Projects

Maeve's Quest - 2D XNA Brawler

Recently I have put together a 2D side-scroller bralwer action game using Microsoft XNA. The main of the project was to better understand sprites and 2D game programming. I incorporated a free online tutorial sprite starter system and modified to to handle multiple sprites with different types of animations (ie. looped, unlooped, draw depth, timing, etc.). I also made use of OOP to divide up and organize the project. The classes make use of inheritance as well as polymorphism.

  • Entity Class
  • Sprite Class
  • Sprite Update
  • Download Source

    Solution Screen:

    Entity Class

    View Full Code :

    abstract public class Entity
    {
    #region Fields
     
    protected Vector2 ePosition;  // position of entity
    protected float eSpeed;// speed of movement
    protected float eGravity;     // gravity
    protected int eHealth;// health of entity
    protected int eDamage;// damage of entity
    protected int eScore;  // score of entity
    protected bool eMoving;// if true, the entity is moving
    protected bool eIsAlive;      // if true, the entity is alive
    protected byte eDirection;    // direction of entity
    protected bool moveLevel;     // allow background to move
    public int screenWidth;// maximum X position for the entity
    public int screenHeight;      // maximum Y position for the entity
     
    #endregion
     
    #region Initialize
    public Entity()
    {
      ePosition = Vector2.Zero;// position of entity
      eSpeed = 0.0f;        // speed of movement
      eGravity = 0.0f;      // gravity
      eHealth = 0;  // health of entity
      eDamage = 0;  // damage of entity
      eMoving = false;      // if true, the entity is moving
      eIsAlive = true;      // if true, the entity is alive
      eDirection = 0;       // 0 = right;  1 = left
      moveLevel = false;    // enemies are alive, don't move
      eScore = 0;
    }
     
    public virtual void LoadContent(GraphicsDevice graphics)
    {
      screenWidth = (int)(graphics.Viewport.Width);
      screenHeight = (int)(graphics.Viewport.Height);
    }
    #endregion

    Sprite Class

    View Full Code :

    /// <summary>
    /// Add an animation to the animations dictionary.
    /// </summary>
    /// <param name="Name">Sprite-specific name of the animation.</param>
    /// <param name="X">X Location of the upper left corner of first frame</param>
    /// <param name="Y">Y Location of the upper left corner of first frame</param>
    /// <param name="Width">Width of the animation's frames</param>
    /// <param name="Height">Height of the animation's frames</param>
    /// <param name="Frames">Number of frames in the animation</param>
    /// <param name="FrameLength">Length (in seconds) to display each frame</param>
    public void AddAnimation(string Name, 
      int X, int Y, int Width, int Height, int Frames, float FrameLength)
    {
      faAnimations.Add(Name, 
        new FrameAnimation(X, Y, Width, Height, Frames, FrameLength));
      iWidth = Width;
      iHeight = Height;
      v2Center = new Vector2(iWidth / 2, iHeight / 2);
    }
     
    /// <summary>
    /// Add an animation to the animations dictionary.
    /// </summary>
    /// <param name="Name">Sprite-specific name of the animation.</param>
    /// <param name="X">X Location of the upper left corner of first frame</param>
    /// <param name="Y">Y Location of the upper left corner of first frame</param>
    /// <param name="Width">Width of the animation's frames</param>
    /// <param name="Height">Height of the animation's frames</param>
    /// <param name="Frames">Number of frames in the animation</param>
    /// <param name="NextAnimation">Name of the next animation after this</param>
    public void AddAnimation(string Name, 
      int X, int Y, int Width, int Height, 
      int Frames, float FrameLength, string NextAnimation)
    {
      faAnimations.Add(Name, 
        new FrameAnimation(X, Y, Width, Height, Frames, FrameLength, NextAnimation));
      iWidth = Width;
      iHeight = Height;
      v2Center = new Vector2(iWidth / 2, iHeight / 2);
    }

    Sprite Update

    View Full Code :

    #region Update
    public void Update(GameTime gameTime)
    {
      if (bAnimating)
      {
        // If there is not a currently active animation
        if (CurrentFrameAnimation == null)
        {
          // Make sure we have an animation associated with this sprite
          if (faAnimations.Count > 0)
          {
            // Set the active animation to the first animation
            // associated with this sprite
            string[] sKeys = new string[faAnimations.Count];
            faAnimations.Keys.CopyTo(sKeys, 0);
            CurrentAnimation = sKeys[0];
          }
          else
          {
            return;
          }
        }
        // If the current animation has played more than once
        if (CurrentFrameAnimation.PlayCount > 0)
        {
          // Update damge
          bDamage = true;
     
          // Check to see if there is a "followup" animation named for this animation
          if (!String.IsNullOrEmpty(CurrentFrameAnimation.NextAnimation))
          {
            // If it has, set up the next animation
            CurrentAnimation = CurrentFrameAnimation.NextAnimation;
          }
          // Else, set next animation to default
          else
          {
            string[] sKeys = new string[faAnimations.Count];
            faAnimations.Keys.CopyTo(sKeys, 0);
            CurrentAnimation = sKeys[0];
          }
        }
        else // Else, if it hasn't played more than once
          bDamage = false; // reset damage
     
        CurrentFrameAnimation.Update(gameTime);
      }
    }
    #endregion
     
     
    #region Draw
    public void Draw(SpriteBatch spriteBatch, int XOffset, int YOffset)
    {
      if (bAnimating)
      {
        // set default direction to RIGHT
        SpriteEffects spriteEffect = SpriteEffects.None;
        // If player is facing LEFT
        if (flipImage == 1)
        {
          // flip the sprite image to face LEFT
          spriteEffect = SpriteEffects.FlipHorizontally;
        }
        spriteBatch.Draw(t2dTexture,
              (v2Position + new Vector2(XOffset, YOffset) + v2Center),
                CurrentFrameAnimation.FrameRectangle, colorTint,
                fRotation, v2Center, fScale, spriteEffect, 0);
      }
    }
    #endregion