Guest User

Untitled

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