#region License /* MIT License Copyright © 2006-2007 Rob Loach http://www.robloach.net All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #endregion License #region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; #endregion namespace PongXna { /// /// PongXna - A simple recreation of the classic Pong game. /// /// Version 2.0 public class PongXnaGame : Game { // Graphics GraphicsDeviceManager graphics; ContentManager content; Dictionary textures = new Dictionary(); SpriteBatch sprites; // Sound private AudioEngine audioEngine = new AudioEngine("Sounds/PongXna.xgs"); private WaveBank waveBank; private SoundBank soundBank; // Game objects Entity paddle1, paddle2, ball; float paddleAcceleration = 12000f; public PongXnaGame() { // Create core components graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); waveBank = new WaveBank(audioEngine, "Sounds/PongXnaWaves.xwb"); soundBank = new SoundBank(audioEngine, "Sounds/PongXnaSounds.xsb"); } protected override void BeginRun() { // Set the initial states of the entities float paddleWidth = textures["paddle1"].Width * 1.5f; float paddleHeight = textures["paddle1"].Height * 1.5f; paddle1 = new Entity(20, (float)Window.ClientBounds.Height / 2f - paddleHeight / 2f, paddleWidth, paddleHeight); paddle2 = new Entity(Window.ClientBounds.Width - 20 - paddleWidth, (float)Window.ClientBounds.Height / 2f - paddleHeight, paddleWidth, paddleHeight); ball = new Entity((float)Window.ClientBounds.Width / 2f - ball.Size.X / 2f, (float)Window.ClientBounds.Height / 2f - ball.Size.Y / 2f, textures["ball"].Width * 2, textures["ball"].Height * 2); ball.Speed.X = ball.Speed.Y = 150f; } protected override void LoadGraphicsContent(bool loadAllContent) { // Load all graphics content using the content manager if (loadAllContent) { textures["background"] = content.Load("Graphics/background"); textures["ball"] = content.Load("Graphics/ball"); textures["paddle1"] = content.Load("Graphics/paddle1"); textures["paddle2"] = content.Load("Graphics/paddle2"); sprites = new SpriteBatch(this.graphics.GraphicsDevice); } } protected override void UnloadGraphicsContent(bool unloadAllContent) { // Dispose of any allocated memory if (unloadAllContent) content.Unload(); sprites.Dispose(); audioEngine.Dispose(); waveBank.Dispose(); soundBank.Dispose(); } protected override void Update(GameTime gameTime) { // Update the game world based on elapsed time float elapsed = (float)gameTime.ElapsedRealTime.TotalSeconds; // Update locations of entities paddle1.Position.Y += paddle1.Speed.Y * elapsed; paddle2.Position.Y += paddle2.Speed.Y * elapsed; ball.Position.X += ball.Speed.X * elapsed; ball.Position.Y += ball.Speed.Y * elapsed; // Check for round winning conditions if (ball.Speed.X < 0f) // left side { if (ball.Position.X + ball.Size.X < 0f) ResetBall(); else { if ((ball.Position.X < paddle1.Position.X + paddle1.Size.X && ball.Position.X + ball.Size.X > paddle1.Position.X) && (ball.Position.Y > paddle1.Position.Y) && (ball.Position.Y < paddle1.Position.Y + paddle1.Size.Y)) { soundBank.PlayCue("bounce"); ball.Speed.X *= -1.1f; } } } else // right side { if (ball.Position.X > Window.ClientBounds.Width) ResetBall(); else { if ((ball.Position.X + ball.Size.X > paddle2.Position.X && ball.Position.X < paddle2.Position.X + paddle2.Size.X) && (ball.Position.Y > paddle2.Position.Y) && (ball.Position.Y < paddle2.Position.Y + paddle2.Size.Y)) { soundBank.PlayCue("bounce2"); ball.Speed.X *= -1.1f; } } } // Ball bounce off of wall if ((ball.Speed.Y < 0f && ball.Position.Y < 0f) || (ball.Speed.Y > 0f && ball.Position.Y + ball.Size.Y > Window.ClientBounds.Height)) { soundBank.PlayCue("wall"); ball.Speed.Y *= -1f; } // Move AI player if (ball.Speed.X > 0) { if (paddle2.Position.Y + (float)paddle2.Size.Y / 2f < ball.Position.Y + (float)ball.Size.Y / 2f) paddle2.Speed.Y = paddleAcceleration * elapsed; else paddle2.Speed.Y = -paddleAcceleration * elapsed; } else paddle2.Speed.Y = 0f; // GamePad support GamePadState gamepad = GamePad.GetState(PlayerIndex.One); if (gamepad.IsConnected) { if (gamepad.ThumbSticks.Left.Y < 0 || gamepad.DPad.Down == ButtonState.Pressed) paddle1.Speed.Y = paddleAcceleration * elapsed; else if (gamepad.ThumbSticks.Left.Y > 0 || gamepad.DPad.Up == ButtonState.Pressed) paddle1.Speed.Y = -paddleAcceleration * elapsed; else paddle1.Speed.Y = 0f; if (gamepad.Buttons.Back == ButtonState.Pressed) this.Exit(); } else { // Use keyboard on Windows machines when there isn't a gamepad KeyboardState keyboard = Keyboard.GetState(); if (keyboard.IsKeyDown(Keys.Down)) paddle1.Speed.Y = paddleAcceleration * elapsed; else if (keyboard.IsKeyDown(Keys.Up)) paddle1.Speed.Y = -paddleAcceleration * elapsed; else paddle1.Speed.Y = 0f; if (keyboard.IsKeyDown(Keys.Escape)) this.Exit(); } // Keep the paddles on the screen CheckPaddleBoundry(ref paddle1); CheckPaddleBoundry(ref paddle2); // Give the audio engine to perform periodic work audioEngine.Update(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { // Draw the background and all game entities sprites.Begin(); sprites.Draw(textures["background"], new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White); paddle1.Draw(sprites, textures["paddle1"]); paddle2.Draw(sprites, textures["paddle2"]); ball.Draw(sprites, textures["ball"]); sprites.End(); base.Draw(gameTime); } private void ResetBall() { ball.Speed.X = ball.Speed.Y = 150f; ball.Position = new Vector2((float)Window.ClientBounds.Width / 2f - ball.Size.X / 2f, (float)Window.ClientBounds.Height / 2f - ball.Size.Y / 2f); soundBank.PlayCue("win"); } private void CheckPaddleBoundry(ref Entity paddle) { if (paddle.Position.Y + paddle.Size.Y > Window.ClientBounds.Height) paddle.Position.Y = Window.ClientBounds.Height - paddle.Size.Y; else if (paddle.Position.Y < 0f) paddle.Position.Y = 0f; } } }