| using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using Microsoft.Xna.Framework; |
| using Microsoft.Xna.Framework.Audio; |
| using Microsoft.Xna.Framework.Content; |
| using Microsoft.Xna.Framework.GamerServices; |
| using Microsoft.Xna.Framework.Graphics; |
| using Microsoft.Xna.Framework.Input; |
| using Microsoft.Xna.Framework.Media; |
| using Microsoft.Xna.Framework.Net; |
| using Microsoft.Xna.Framework.Storage; |
| using System.IO; |
| using System.Diagnostics; |
| using System.Text; |
| |
| |
| namespace Pong |
| { |
| /// <summary> |
| /// This is a game component that implements IUpdateable. |
| /// </summary> |
| public class InGameState : Microsoft.Xna.Framework.DrawableGameComponent |
| { |
| #region Class Level Variables |
| // TODO: Add your calss level variables here |
| SpriteBatch spriteBatch; |
| |
| SpriteFont score_font; |
| |
| List<Paddle> paddle_list = new List<Paddle>(); |
| List<Ball> ball_list = new List<Ball>(); |
| List<Ball> lives_list = new List<Ball>(); |
| |
| LevelManager level_manager; |
| |
| bool paddle_colided; |
| bool block_colided; |
| |
| public int current_level = 1; |
| |
| int next_state; |
| #endregion |
| |
| public InGameState(Game game) |
| : base(game) |
| { |
| // TODO: Construct any child components here |
| } |
| |
| /// <summary> |
| /// Allows the game component to perform any initialization it needs to before starting |
| /// to run. This is where it can query for any required services and load content. |
| /// </summary> |
| public override void Initialize() |
| { |
| // TODO: Add your initialization code here |
| level_manager = new LevelManager(this.current_level, |
| Game.Content.Load<Texture2D>("Sprites//Block")); |
| |
| 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() |
| { |
| // Create a new SpriteBatch, which can be used to draw textures. |
| spriteBatch = new SpriteBatch(GraphicsDevice); |
| |
| score_font = Game.Content.Load<SpriteFont>("Fonts//ScoreFont"); |
| |
| // TODO: use this.Content to load your game content here |
| #region Add Paddles |
| // Add the paddles |
| paddle_list.Add(new Paddle( |
| Game.Content.Load<Texture2D>("Sprites//paddle"), |
| new Vector2(Game.Window.ClientBounds.Width, 680f), |
| new Vector2(5f, 5f), |
| 3)); |
| |
| // Initializes each paddle |
| foreach (Paddle p in paddle_list) |
| p.Initialize(); |
| #endregion |
|
| #region Add Balls |
| // Adds the balls |
| ball_list.Add(new Ball( |
| Game.Content.Load<Texture2D>("Sprites//ball"), |
| new Vector2(Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height), |
| new Vector2( -1f, 1f ))); |
| |
| // Initializes teh balls |
| foreach (Ball b in ball_list) |
| b.Initialize(); |
| #endregion |
|
| #region Add Lives |
| foreach (Paddle p in paddle_list) |
| { |
| for (int i = 0; i < p.get_paddle_lives; ++i) |
| { |
| int offset = 10 + i * 20; |
| lives_list.Add(new Ball( |
| Game.Content.Load<Texture2D>("Sprites//ball"), |
| new Vector2( offset, 35), |
| Vector2.Zero)); |
| } |
| } |
| #endregion |
| } |
| |
| /// <summary> |
| /// Allows the game component to update itself. |
| /// </summary> |
| /// <param name="gameTime">Provides a snapshot of timing values.</param> |
| public override void Update(GameTime gameTime) |
| { |
| // TODO: Add your update code here |
| #region Paddle Collision Check |
| foreach (Paddle p in paddle_list) |
| { |
| foreach (Ball b in ball_list) |
| { |
| if (p.get_paddle_rect.Intersects(b.get_ball_rect)) |
| this.paddle_colided = true; |
| |
| else |
| this.paddle_colided = false; |
| } |
| } |
| #endregion |
|
| #region Block Collision Check |
| foreach (Block bl in level_manager.get_block_list) |
| { |
| foreach (Ball ba in ball_list) |
| { |
| if (bl.get_block_rect.Intersects(ba.get_ball_rect)) |
| this.block_colided = true; |
| |
| else |
| this.block_colided = false; |
| } |
| } |
| #endregion |
|
| #region Update Paddles |
| foreach (Paddle p in paddle_list) |
| { |
| p.Update(gameTime, |
| Game.Window.ClientBounds, |
| Game.GraphicsDevice.Viewport); |
| } |
| #endregion |
|
| #region Updates Balls |
| foreach (Ball ba in ball_list) |
| { |
| ba.Update(gameTime, |
| Game.Window.ClientBounds, |
| Game.GraphicsDevice.Viewport, |
| this.paddle_colided, |
| this.level_manager.get_block_list); |
| } |
| #endregion |
|
| #region Updtae Level |
| level_manager.Update(gameTime, Game.Window.ClientBounds); |
| #endregion |
|
| #region Update Lives |
| foreach (Ball b in ball_list) |
| { |
| // TODO: Check if there are no more lives left and set state to game over |
| if (b.get_minus_life) |
| { |
| this.lives_list.RemoveAt(lives_list.Count - 1); |
| } |
| } |
| #endregion |
| |
| base.Update(gameTime); |
| } |
| |
| /// <summary> |
| /// This is called when the game should draw itself. |
| /// </summary> |
| /// <param name="gameTime">Provides a snapshot of timing values.</param> |
| public override void Draw(GameTime gameTime) |
| { |
| GraphicsDevice.Clear(Color.Black); |
| |
| // TODO: Add your drawing code here |
| spriteBatch.Begin(); |
|
| #region Draw Paddles |
| foreach (Paddle p in paddle_list) |
| p.Draw(gameTime, spriteBatch); |
| #endregion |
|
| #region Draw Balls |
| foreach (Ball b in ball_list) |
| b.Draw(gameTime, spriteBatch, Color.White); |
| #endregion |
|
| #region Draw Level |
| level_manager.Draw(gameTime, spriteBatch); |
| #endregion |
|
| #region Draw Score |
| foreach (Paddle p in paddle_list) |
| spriteBatch.DrawString( |
| score_font, |
| "Score: " + p.get_paddle_score, |
| new Vector2( Game.GraphicsDevice.Viewport.Width - score_font.MeasureString("Score: " + p.get_paddle_score).X - 10f, 0), |
| Color.LightGray); |
| #endregion |
|
| #region Draw Lives |
| foreach (Paddle p in paddle_list) |
| spriteBatch.DrawString( |
| score_font, |
| "Lives:", |
| new Vector2(Game.GraphicsDevice.Viewport.X + 10f, 0), |
| Color.LightGray); |
| |
| foreach (Ball b in lives_list) |
| b.Draw(gameTime, spriteBatch, Color.LightGray); |
| #endregion |
| |
| spriteBatch.End(); |
| |
| base.Draw(gameTime); |
| } |
| |
| public int get_next_state |
| { |
| get |
| { |
| return this.next_state; |
| } |
| } |
| } |
| } |