DaveVoyles

screenWidth / screenHeight C#

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