Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- I'm trying to add health bar to my pong game (among other types of bars).
- 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".
- 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.
- Game1.screenWith / 2 isn't working when I go to place it within the Draw method for HUD. Here is George's method:
- //Draw the box around the health bar
- mBatch.Draw(mHealthBar, new Rectangle(this.Window.ClientBounds.Width / 2 - mHealthBar.Width / 2,
- 30, mHealthBar.Width, 44), new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
- **** How can I access my screenWidth and Height? *****
- */
- ////////////////////////////////////////
- namespace Pong
- {
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Media;
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- public static GameStates gamestate;
- private GraphicsDeviceManager graphics;
- private Texture2D backgroundTexture;
- private SpriteBatch spriteBatch;
- private Bat rightBat;
- private Bat leftBat;
- private Ball ball;
- private Menu menu;
- private SpriteFont arial;
- private int resetTimer;
- private bool resetTimerInUse;
- private bool lastScored;
- private SoundEffect menuButton;
- private SoundEffect menuClose;
- public Song MainMenuSong { get; private set; }
- public Song PlayingSong { get; private set; }
- // public Song mySong;
- private Input input;
- public int screenWidth;
- public int screenHeight;
- // For resetting the speed burst of the paddle
- int coolDown = 0;
- int disableCooldown = 0;
- int powerEnableCooldown = 5000;
- int powerDisableCooldown = 2000;
- public enum GameStates
- {
- Menu,
- Running,
- End
- }
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- ///
- // USUALLY THIS IS 'PRIVATE'. I JUST SET IT TO 'PUBLIC' TO TEST THIS OUT AND ALLOW THE HUD CLASS TO ACCESS IT
- public override void Initialize()
- {
- screenWidth = 1280;
- screenHeight = 720;
- menu = new Menu();
- gamestate = GameStates.Menu;
- resetTimer = 0;
- resetTimerInUse = true;
- lastScored = false;
- graphics.PreferredBackBufferWidth = screenWidth;
- graphics.PreferredBackBufferHeight = screenHeight;
- graphics.IsFullScreen = false;
- graphics.ApplyChanges();
- // TODO: Add your initialization logic here
- ball = new Ball(Content, new Vector2(screenWidth, screenHeight));
- SetUpMulti();
- input = new Input();
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent()
- {
- arial = Content.Load<SpriteFont>("Arial");
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- backgroundTexture = Content.Load<Texture2D>(@"gfx/background");
- menuButton = Content.Load<SoundEffect>(@"sfx/menuButton");
- menuClose = Content.Load<SoundEffect>(@"sfx/menuClose");
- MainMenuSong = Content.Load<Song>(@"sfx/getWeapon");
- PlayingSong = Content.Load<Song>(@"sfx/boomer");
- MediaPlayer.IsRepeating = true;
- MediaPlayer.Play(MainMenuSong);
- // mySong = Content.Load<Song>(@"sfx/getWeapon");
- // MediaPlayer.Play(mySong);
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- private void SetUpSingle()
- {
- rightBat = new AIBat(Content, new Vector2(screenWidth, screenHeight), false);
- leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
- }
- private void SetUpMulti()
- {
- rightBat = new Bat(Content, new Vector2(screenWidth, screenHeight), false);
- leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
- }
- private void IncreaseSpeed()
- {
- ball.IncreaseSpeed();
- leftBat.IncreaseSpeed();
- rightBat.IncreaseSpeed();
- }
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- ///
- protected override void Update(GameTime gameTime)
- {
- input.Update();
- if (gamestate == GameStates.Running)
- {
- if (leftBat.GetPoints() > 5)
- {
- menu.InfoText = "Game, blouses.";
- gamestate = GameStates.End;
- }
- else if (rightBat.GetPoints() > 5)
- {
- menu.InfoText = "You just let the AI beat you.";
- gamestate = GameStates.End;
- }
- if (resetTimerInUse)
- {
- resetTimer++;
- ball.Stop();
- }
- if (resetTimer == 120)
- {
- resetTimerInUse = false;
- ball.Reset(lastScored);
- resetTimer = 0;
- }
- if (rightBat.GetType() != typeof(Pong.AIBat))
- {
- if (input.LeftDown) leftBat.MoveDown();
- else if ((input.LeftUp)) leftBat.MoveUp();
- if (input.RightDown) rightBat.MoveDown();
- else if (input.RightUp) rightBat.MoveUp();
- }
- else if (rightBat.GetType() == typeof(Pong.AIBat))
- {
- if (input.LeftDown) leftBat.MoveDown();
- else if ((input.LeftUp)) leftBat.MoveUp();
- if (input.RightDown) leftBat.MoveDown();
- else if (input.RightUp) leftBat.MoveUp();
- }
- leftBat.UpdatePosition(ball);
- rightBat.UpdatePosition(ball);
- ball.UpdatePosition();
- if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
- {
- if (rightBat.GetSize().Intersects(ball.GetSize()))
- {
- ball.BatHit(CheckHitLocation(rightBat));
- }
- }
- else if (leftBat.GetSize().Intersects(ball.GetSize()))
- {
- ball.BatHit(CheckHitLocation(leftBat));
- }
- // Triggers the turbo button and cooldown
- if (input.SpaceDown)
- {
- if (disableCooldown > 0)
- {
- leftBat.isTurbo = true;
- coolDown = powerEnableCooldown;
- leftBat.moveSpeed = 40.0f;
- disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
- }
- else
- {
- leftBat.DisableTurbo();
- }
- }
- else if (!input.SpaceDown)
- {
- leftBat.DisableTurbo();
- coolDown -= gameTime.ElapsedGameTime.Milliseconds;
- if (coolDown < 0)
- {
- disableCooldown = powerDisableCooldown;
- }
- }
- // Makes sure that if Turbo is on, it automatically turns of. Kills it after () seconds
- if(leftBat.isTurbo)
- disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
- if(disableCooldown < 0)
- {
- leftBat.isTurbo = false;
- }
- if (!resetTimerInUse)
- {
- if (ball.GetPosition().X > screenWidth)
- {
- resetTimerInUse = true;
- lastScored = true;
- // Checks to see if ball went out of bounds, and triggers warp sfx
- ball.OutOfBounds();
- leftBat.IncrementPoints();
- IncreaseSpeed();
- }
- else if (ball.GetPosition().X < 0)
- {
- resetTimerInUse = true;
- lastScored = false;
- // Checks to see if ball went out of bounds, and triggers warp sfx
- ball.OutOfBounds();
- rightBat.IncrementPoints();
- IncreaseSpeed();
- }
- }
- }
- else if (gamestate == GameStates.Menu)
- {
- if (input.RightDown || input.LeftDown)
- {
- menu.Iterator++;
- menuButton.Play();
- }
- else if (input.RightUp || input.LeftUp)
- {
- menu.Iterator--;
- menuButton.Play();
- }
- if (input.MenuSelect)
- {
- if (menu.Iterator == 0)
- {
- gamestate = GameStates.Running;
- SetUpSingle();
- menuClose.Play();
- }
- else if (menu.Iterator == 1)
- {
- gamestate = GameStates.Running;
- SetUpMulti();
- menuClose.Play();
- }
- else if (menu.Iterator == 2)
- {
- this.Exit();
- menuClose.Play();
- }
- menu.Iterator = 0;
- }
- }
- else if (gamestate == GameStates.End)
- {
- if (input.MenuSelect)
- {
- gamestate = GameStates.Menu;
- }
- }
- base.Update(gameTime);
- }
- private int CheckHitLocation(Bat bat)
- {
- int block = 0;
- if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20) block = 1;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2) block = 2;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3) block = 3;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4) block = 4;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5) block = 5;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6) block = 6;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7) block = 7;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8) block = 8;
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19) block = 9;
- else block = 10;
- return block;
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.Black);
- // TODO: Add your drawing code here
- spriteBatch.Begin();
- if (gamestate == GameStates.Running)
- {
- spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
- leftBat.Draw(spriteBatch);
- rightBat.Draw(spriteBatch);
- ball.Draw(spriteBatch);
- spriteBatch.DrawString(arial, leftBat.GetPoints().ToString(), new Vector2(screenWidth / 4 - arial.MeasureString
- (rightBat.GetPoints().ToString()).X, 20), Color.White);
- spriteBatch.DrawString(arial, rightBat.GetPoints().ToString(), new Vector2(screenWidth / 4 * 3 - arial.MeasureString
- (rightBat.GetPoints().ToString()).X, 20), Color.White);
- }
- else if (gamestate == GameStates.Menu)
- {
- spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
- menu.DrawMenu(spriteBatch, screenWidth, arial);
- }
- else if (gamestate == GameStates.End)
- {
- spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
- menu.DrawEndScreen(spriteBatch, screenWidth, arial);
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
- //////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Media;
- namespace Pong
- {
- class HUD
- {
- SpriteBatch mBatch;
- Texture2D mHealthBar;
- GraphicsDevice graphicsDevice;
- protected override void LoadContent(ContentManager content)
- {
- // TODO: Load any ResourceManagementMode.Automatic content
- //Initialize the Sprite batch
- mBatch = new SpriteBatch(this.graphicsDevice);
- //Load the HealthBar image from the disk into the Texture2D object
- mHealthBar = content.Load<Texture2D>(@"gfx/HealthBar2");
- }
- protected override void Draw(GameTime gameTime)
- {
- //TODO: Add your drawing code here
- mBatch.Begin();
- //Draw the health for the health bar
- mBatch.Draw(mHealthBar, new Rectangle( //TRYING TO PUT THE screenWith property in here / 2 - mHealthBar.Width / 2,
- 30, mHealthBar.Width, 44), new Rectangle(0, 45, mHealthBar.Width, 44), Color.Red);
- //Draw the box around the health bar
- mBatch.Draw(mHealthBar, new Rectangle(this.Window.ClientBounds.Width / 2 - mHealthBar.Width / 2,
- 30, mHealthBar.Width, 44), new Rectangle(0, 0, mHealthBar.Width, 44), Color.White);
- mBatch.End();
- base.Draw(gameTime);
- }
- }
- }
RAW Paste Data