Advertisement
DaveVoyles

Health bar not displaying, XNA C#

Apr 13th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.52 KB | None | 0 0
  1. I can't manage to get my HUD to appear on screen. It is simply a health bar. I'm following George Clingerman's tutorial (http://www.xnadevelopment.com/tutorials/notsohealthy/NotSoHealthy.shtml) , but must be doing something wrong here.
  2.  
  3. I'm no longer receiving any errors or warnings, so I'm not sure of what I am missing. My only guess is
  4. the lack of "base.Draw(gameTime);" at the end of my Draw method for my HUD class. When I do insert that however, I receive errors.
  5.  
  6. What do you think the issue is? I've included both my Game1 and HUD classes.
  7.  
  8. ///////////////////////////////////////////////
  9.  
  10.  
  11. namespace Pong
  12. {
  13.     using System;
  14.     using Microsoft.Xna.Framework;
  15.     using Microsoft.Xna.Framework.Graphics;
  16.     using Microsoft.Xna.Framework.Input;
  17.     using Microsoft.Xna.Framework.Audio;
  18.     using Microsoft.Xna.Framework.Media;
  19.  
  20.  
  21.     /// <summary>
  22.     /// This is the main type for your game
  23.     /// </summary>
  24.     public class Game1 : Microsoft.Xna.Framework.Game
  25.     {
  26.        
  27.         public static GameStates gamestate;
  28.         private GraphicsDeviceManager graphics;
  29.         private Texture2D backgroundTexture;
  30.         private SpriteBatch spriteBatch;
  31.         private Bat rightBat;
  32.         private Bat leftBat;
  33.         private Ball ball;
  34.         private Menu menu;
  35.         private SpriteFont arial;
  36.         private int resetTimer;
  37.         private bool resetTimerInUse;
  38.         private bool lastScored;
  39.  
  40.         private SoundEffect menuButton;
  41.         private SoundEffect menuClose;
  42.         public Song MainMenuSong { get; private set; }
  43.         public Song PlayingSong { get; private set; }
  44. //        public Song mySong;
  45.        
  46.         private Input input;
  47.         public int screenWidth;
  48.         public int screenHeight;
  49.  
  50.         // For resetting the speed burst of the paddle
  51.         int coolDown = 0;
  52.         int disableCooldown = 0;
  53.         int powerEnableCooldown = 5000;
  54.         int powerDisableCooldown = 2000;
  55.  
  56.         public static Game1 Instance;
  57.  
  58.         public enum GameStates
  59.         {
  60.             Menu,
  61.             Running,
  62.             End
  63.         }
  64.  
  65.         // Constructor (I'm a n00b, remember?)
  66.         public Game1()
  67.         {
  68.             graphics = new GraphicsDeviceManager(this);
  69.             Content.RootDirectory = "Content";
  70.  
  71.             // Creates an instance of the class
  72.             Instance = this;
  73.            
  74.         }
  75.        
  76.  
  77.         /// <summary>
  78.         /// Allows the game to perform any initialization it needs to before starting to run.
  79.         /// This is where it can query for any required services and load any non-graphic
  80.         /// related content.  Calling base.Initialize will enumerate through any components
  81.         /// and initialize them as well.
  82.         /// </summary>
  83.  
  84.         protected override void Initialize()
  85.         {
  86.             screenWidth = 1280;
  87.             screenHeight = 720;
  88.             menu = new Menu();
  89.             gamestate = GameStates.Menu;
  90.             resetTimer = 0;
  91.             resetTimerInUse = true;
  92.             lastScored = false;
  93.             graphics.PreferredBackBufferWidth = screenWidth;
  94.             graphics.PreferredBackBufferHeight = screenHeight;
  95.             graphics.IsFullScreen = false;
  96.             graphics.ApplyChanges();
  97.  
  98.             ball = new Ball(Content, new Vector2(screenWidth, screenHeight));
  99.             SetUpMulti();
  100.             input = new Input();
  101.             base.Initialize();
  102.         }
  103.  
  104.  
  105.         /// <summary>
  106.         /// LoadContent will be called once per game and is the place to load
  107.         /// all of your content.
  108.         /// </summary>
  109.         protected override void LoadContent()
  110.         {
  111.             arial = Content.Load<SpriteFont>("Arial");
  112.             // Create a new SpriteBatch, which can be used to draw textures.
  113.             spriteBatch = new SpriteBatch(GraphicsDevice);
  114.             backgroundTexture = Content.Load<Texture2D>(@"gfx/background");
  115.             menuButton = Content.Load<SoundEffect>(@"sfx/menuButton");
  116.             menuClose = Content.Load<SoundEffect>(@"sfx/menuClose");
  117.             MainMenuSong = Content.Load<Song>(@"sfx/getWeapon");
  118.             PlayingSong = Content.Load<Song>(@"sfx/boomer");
  119.             MediaPlayer.IsRepeating = true;
  120.             MediaPlayer.Play(MainMenuSong);
  121.             //            mySong = Content.Load<Song>(@"sfx/getWeapon");
  122.             //            MediaPlayer.Play(mySong);
  123.  
  124.            
  125.         }
  126.  
  127.         /// <summary>
  128.         /// UnloadContent will be called once per game and is the place to unload
  129.         /// all content.
  130.         /// </summary>
  131.         protected override void UnloadContent()
  132.         {
  133.             // TODO: Unload any non ContentManager content here
  134.         }
  135.  
  136.         private void SetUpSingle()
  137.         {
  138.             rightBat = new AIBat(Content, new Vector2(screenWidth, screenHeight), false);
  139.             leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  140.         }
  141.  
  142.         private void SetUpMulti()
  143.         {
  144.             rightBat = new Bat(Content, new Vector2(screenWidth, screenHeight), false);
  145.             leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  146.         }
  147.  
  148.         private void IncreaseSpeed()
  149.         {
  150.             ball.IncreaseSpeed();
  151.             leftBat.IncreaseSpeed();
  152.             rightBat.IncreaseSpeed();
  153.         }
  154.  
  155.  
  156.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  157.         ///
  158.         protected override void Update(GameTime gameTime)
  159.         {
  160.  
  161.             input.Update();
  162.  
  163.             if (gamestate == GameStates.Running)
  164.             {
  165.                 if (leftBat.GetPoints() > 5)
  166.                 {
  167.                     menu.InfoText = "Game, blouses.";
  168.                     gamestate = GameStates.End;
  169.                 }
  170.                 else if (rightBat.GetPoints() > 5)
  171.                 {
  172.                     menu.InfoText = "You just let the AI beat you.";
  173.                     gamestate = GameStates.End;
  174.                 }
  175.                 if (resetTimerInUse)
  176.                 {
  177.                     resetTimer++;
  178.                     ball.Stop();                
  179.                 }
  180.  
  181.                 if (resetTimer == 120)
  182.                 {
  183.                     resetTimerInUse = false;
  184.                     ball.Reset(lastScored);
  185.                     resetTimer = 0;
  186.                 }
  187.  
  188.                              
  189.                 if (rightBat.GetType() != typeof(Pong.AIBat))
  190.                 {
  191.                     if (input.LeftDown) leftBat.MoveDown();
  192.                     else if ((input.LeftUp)) leftBat.MoveUp();
  193.                     if (input.RightDown) rightBat.MoveDown();
  194.                     else if (input.RightUp) rightBat.MoveUp();
  195.                 }
  196.          
  197.                 else if (rightBat.GetType() == typeof(Pong.AIBat))
  198.                 {
  199.                     if (input.LeftDown) leftBat.MoveDown();
  200.                     else if ((input.LeftUp)) leftBat.MoveUp();
  201.                     if (input.RightDown) leftBat.MoveDown();
  202.                     else if (input.RightUp) leftBat.MoveUp();
  203.                  }
  204.  
  205.              
  206.                 leftBat.UpdatePosition(ball);
  207.                 rightBat.UpdatePosition(ball);
  208.                 ball.UpdatePosition();
  209.  
  210.  
  211.                 if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
  212.                 {
  213.                     if (rightBat.GetSize().Intersects(ball.GetSize()))
  214.                     {
  215.                         ball.BatHit(CheckHitLocation(rightBat));
  216.                     }
  217.                 }
  218.                 else if (leftBat.GetSize().Intersects(ball.GetSize()))
  219.                 {
  220.                     ball.BatHit(CheckHitLocation(leftBat));
  221.                 }
  222.  
  223.  
  224.                 // Triggers the turbo button and cooldown
  225.                 if (input.SpaceDown)
  226.                 {
  227.                     if (disableCooldown > 0)
  228.                     {
  229.                         leftBat.isTurbo = true;
  230.                         coolDown = powerEnableCooldown;
  231.                         leftBat.moveSpeed = 40.0f;
  232.                         disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
  233.                     }
  234.                     else
  235.                     {
  236.                         leftBat.DisableTurbo();
  237.                     }
  238.                 }
  239.                 else if (!input.SpaceDown)
  240.                 {
  241.                     leftBat.DisableTurbo();
  242.                     coolDown -= gameTime.ElapsedGameTime.Milliseconds;
  243.                     if (coolDown < 0)
  244.                     {
  245.                         disableCooldown = powerDisableCooldown;
  246.                     }
  247.                 }
  248.  
  249.  
  250.                 // Makes sure that if Turbo is on, it automatically turns of. Kills it after () seconds
  251.                 if(leftBat.isTurbo)
  252.  
  253.                     disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
  254.                         if(disableCooldown < 0)
  255.                          {
  256.                         leftBat.isTurbo = false;
  257.                          }
  258.            
  259.            
  260.  
  261.            
  262.                 if (!resetTimerInUse)
  263.                 {
  264.                     if (ball.GetPosition().X > screenWidth)
  265.                     {
  266.                         resetTimerInUse = true;
  267.                         lastScored = true;
  268.  
  269.                         // Checks to see if ball went out of bounds, and triggers warp sfx
  270.                         ball.OutOfBounds();
  271.                         leftBat.IncrementPoints();
  272.                         IncreaseSpeed();
  273.                     }
  274.                     else if (ball.GetPosition().X < 0)
  275.                     {
  276.  
  277.                         resetTimerInUse = true;
  278.                         lastScored = false;
  279.  
  280.                         // Checks to see if ball went out of bounds, and triggers warp sfx
  281.                         ball.OutOfBounds();
  282.                         rightBat.IncrementPoints();
  283.                         IncreaseSpeed();
  284.                     }
  285.                 }
  286.             }
  287.             else if (gamestate == GameStates.Menu)
  288.                  
  289.             {
  290.  
  291.                 if (input.RightDown || input.LeftDown)
  292.                 {
  293.                     menu.Iterator++;
  294.                     menuButton.Play();
  295.                 }
  296.                 else if (input.RightUp || input.LeftUp)
  297.                 {
  298.                     menu.Iterator--;
  299.                     menuButton.Play();
  300.                 }
  301.  
  302.                 if (input.MenuSelect)
  303.                 {
  304.  
  305.                     if (menu.Iterator == 0)
  306.                     {
  307.                         gamestate = GameStates.Running;
  308.                         SetUpSingle();
  309.                         menuClose.Play();
  310.                     }
  311.                     else if (menu.Iterator == 1)
  312.                     {
  313.                         gamestate = GameStates.Running;
  314.                         SetUpMulti();
  315.                         menuClose.Play();
  316.                     }
  317.                     else if (menu.Iterator == 2)
  318.                     {
  319.                         this.Exit();
  320.                         menuClose.Play();
  321.                     }
  322.                     menu.Iterator = 0;
  323.  
  324.                 }
  325.  
  326.             }
  327.  
  328.             else if (gamestate == GameStates.End)
  329.             {
  330.                 if (input.MenuSelect)
  331.                 {
  332.                     gamestate = GameStates.Menu;
  333.  
  334.                 }
  335.             }
  336.  
  337.  
  338.             base.Update(gameTime);
  339.         }
  340.  
  341.         private int CheckHitLocation(Bat bat)
  342.         {
  343.             int block = 0;
  344.             if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20) block = 1;
  345.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2) block = 2;
  346.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3) block = 3;
  347.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4) block = 4;
  348.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5) block = 5;
  349.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6) block = 6;
  350.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7) block = 7;
  351.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8) block = 8;
  352.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19) block = 9;
  353.             else block = 10;
  354.             return block;
  355.         }
  356.  
  357.  
  358.         protected override void Draw(GameTime gameTime)
  359.         {
  360.             GraphicsDevice.Clear(Color.Black);
  361.  
  362.             // TODO: Add your drawing code here
  363.             spriteBatch.Begin();
  364.             if (gamestate == GameStates.Running)
  365.             {
  366.                 spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  367.                 leftBat.Draw(spriteBatch);
  368.                 rightBat.Draw(spriteBatch);
  369.                 ball.Draw(spriteBatch);
  370.                 spriteBatch.DrawString(arial, leftBat.GetPoints().ToString(), new Vector2(screenWidth / 4 - arial.MeasureString
  371.                     (rightBat.GetPoints().ToString()).X, 20), Color.White);
  372.                 spriteBatch.DrawString(arial, rightBat.GetPoints().ToString(), new Vector2(screenWidth / 4 * 3 - arial.MeasureString
  373.                     (rightBat.GetPoints().ToString()).X, 20), Color.White);
  374.                                
  375.             }
  376.             else if (gamestate == GameStates.Menu)
  377.             {
  378.                 spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  379.                 menu.DrawMenu(spriteBatch, screenWidth, arial);
  380.             }
  381.             else if (gamestate == GameStates.End)
  382.             {
  383.                 spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  384.                 menu.DrawEndScreen(spriteBatch, screenWidth, arial);
  385.             }
  386.             spriteBatch.End();
  387.  
  388.             base.Draw(gameTime);
  389.         }
  390.  
  391.     }
  392. }
  393.  
  394.  
  395. ///////////////////////////////////////////////////////////
  396.  
  397.  
  398. using System;
  399. using System.Collections.Generic;
  400. using System.Linq;
  401. using System.Text;
  402. using Microsoft.Xna.Framework;
  403. using Microsoft.Xna.Framework.Content;
  404. using Microsoft.Xna.Framework.Graphics;
  405.  
  406.  
  407. namespace Pong
  408. {
  409.     class HUD
  410.     {
  411.  
  412.     //   GraphicsDevice graphicsDevice;
  413.  
  414.         SpriteBatch spriteBatch;
  415.         Texture2D mHealthBar;
  416.         int mCurrentHealth = 100;
  417.         private Rectangle titleSafeRectangle;            
  418.        
  419.        
  420.  
  421.          public void LoadContent(ContentManager content)
  422.         {
  423.  
  424.             // Create a new SpriteBatch, which can be used to draw textures.
  425.             spriteBatch = new SpriteBatch(Game1.Instance.GraphicsDevice); //this.
  426.  
  427.             //Load the HealthBar image from the disk into the Texture2D object
  428.             mHealthBar = content.Load<Texture2D>(@"gfx/HealthBar2");
  429.            
  430.              // Used in the Draw method
  431.             titleSafeRectangle = new Rectangle
  432.                 (Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.X,
  433.                 Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Y,
  434.                 Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Width,
  435.                 Game1.Instance.GraphicsDevice.Viewport.TitleSafeArea.Height);
  436.           }
  437.  
  438.  
  439.        
  440.         protected void Draw(GameTime gameTime)
  441.         {
  442.                        
  443.             spriteBatch.Begin();
  444.  
  445.             //Draw the negative space for the health bar
  446.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
  447.                                     new Rectangle(0, 45, mHealthBar.Width, 44), Color.Gray);
  448.            
  449.             //Draw the current health level based on the current Health
  450.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X - mHealthBar.Width / 2, 30,
  451.                 (int)(mHealthBar.Width * ((double)mCurrentHealth / 100)), 44),
  452.                 new Rectangle(0, 45, mHealthBar.Width, 44), Color.Red);
  453.  
  454.             //Draw the box around the health bar
  455.             spriteBatch.Draw(mHealthBar, new Rectangle(titleSafeRectangle.Center.X / 2 - mHealthBar.Width / 2, 30, mHealthBar.Width, 44),
  456.                 new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
  457.            
  458.             spriteBatch.End();
  459.                    
  460.         }
  461.      }
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement