#region License /* PongXNA - A recreation of Pong using XNA Copyright (C) 2006 Rob Loach (http://www.robloach.net) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #endregion License using System; using System.IO; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Components; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; namespace PongXNA { /// /// This is the main type for your game /// partial class PongXNA : Microsoft.Xna.Framework.Game { // The entity helper class class Entity { public Entity(float x, float y) { Position = new Vector2(x, y); } public Entity() { ySpeed = xSpeed = 150f; } public float xSpeed, ySpeed; public Vector2 Position; } // Local variables Entity paddle1; Entity paddle2; Entity ball = new Entity(); float paddleAcceleration = 8000f; Texture2D paddle1Texture; Texture2D paddle2Texture; Texture2D ballTexture; Texture2D background; SpriteBatch sprites; public PongXNA() { InitializeComponent(); } protected override void OnStarting() { base.OnStarting(); // Make it so that when the graphics device is lost, it reloads the resources graphics.GraphicsDevice.DeviceReset += new EventHandler(GraphicsDevice_DeviceReset); // Setup the game LoadResources(); ResetBall(); paddle1 = new Entity(20, (float)Window.ClientHeight / 2f - (float)paddle1Texture.Height / 2f); paddle2 = new Entity(Window.ClientWidth - 20 - paddle2Texture.Width, (float)Window.ClientHeight / 2f - (float)paddle2Texture.Height / 2f); } void ResetBall() { ball.xSpeed *= -1f; // Reverse direction ball.Position = new Vector2((float)Window.ClientWidth / 2f - (float)ballTexture.Width / 2f, (float)Window.ClientHeight / 2f - (float)ballTexture.Height / 2f); } void GraphicsDevice_DeviceReset(object sender, EventArgs e) { // Load the resources again when the device is lost LoadResources(); } void LoadResources() { // Load graphics and setup the sprites batch paddle1Texture = Texture2D.FromFile(graphics.GraphicsDevice, Path.Combine("Graphics", "paddle1.bmp")); paddle2Texture = Texture2D.FromFile(graphics.GraphicsDevice, Path.Combine("Graphics", "paddle2.bmp")); ballTexture = Texture2D.FromFile(graphics.GraphicsDevice, Path.Combine("Graphics", "ball.bmp")); background = Texture2D.FromFile(graphics.GraphicsDevice, Path.Combine("Graphics", "background.bmp")); sprites = new SpriteBatch(graphics.GraphicsDevice); } protected override void Update() { // The time since Update was called last float elapsed = (float)ElapsedTime.TotalSeconds; // Update location of entities paddle1.Position.Y += paddle1.ySpeed * elapsed; paddle2.Position.Y += paddle2.ySpeed * elapsed; ball.Position.X += ball.xSpeed * elapsed; ball.Position.Y += ball.ySpeed * elapsed; // Check for round conditions if (ball.xSpeed < 0f) // left side { if (ball.Position.X < 0) ResetBall(); else { if (ball.Position.X < paddle1.Position.X + paddle1Texture.Width && ball.Position.X + ballTexture.Width > paddle1.Position.X) if (ball.Position.Y > paddle1.Position.Y) if (ball.Position.Y < paddle1.Position.Y + paddle1Texture.Height) ball.xSpeed *= -1f; } } else // right side { if (ball.Position.X > Window.ClientWidth) ResetBall(); else { if (ball.Position.X + ballTexture.Width > paddle2.Position.X && ball.Position.X < paddle2.Position.X + paddle2Texture.Width) if (ball.Position.Y > paddle2.Position.Y) if (ball.Position.Y < paddle2.Position.Y + paddle2Texture.Height) ball.xSpeed *= -1f; } } // Ball bounce off of wall if (ball.ySpeed < 0f && ball.Position.Y < 0f) ball.ySpeed *= -1f; else if (ball.ySpeed > 0f && ball.Position.Y + ballTexture.Height > Window.ClientHeight) ball.ySpeed *= -1f; // Move AI player if (ball.xSpeed > 0) { if (paddle2.Position.Y + (float)paddle2Texture.Height / 2f < ball.Position.Y + (float)ballTexture.Height / 2f) paddle2.ySpeed = paddleAcceleration * elapsed; else paddle2.ySpeed = -paddleAcceleration * elapsed; } else paddle2.ySpeed = 0f; // Move human player GamePadState gamepad = GamePad.GetState(PlayerIndex.One); KeyboardState keyboard = Keyboard.GetState(); if (gamepad.IsConnected) // Use gamepad if available { if (gamepad.DPad.Down == ButtonState.Pressed) paddle1.ySpeed = paddleAcceleration * elapsed; else if (gamepad.DPad.Up == ButtonState.Pressed) paddle1.ySpeed = -paddleAcceleration * elapsed; else paddle1.ySpeed = 0f; } else { if (keyboard.IsKeyDown(Keys.Down)) paddle1.ySpeed = paddleAcceleration * elapsed; else if (keyboard.IsKeyDown(Keys.Up)) paddle1.ySpeed = -paddleAcceleration * elapsed; else paddle1.ySpeed = 0f; } if (keyboard.IsKeyDown(Keys.Escape)) this.Exit(); // Keep the paddles on the screen CheckPaddleBoundry(paddle1, paddle1Texture); CheckPaddleBoundry(paddle2, paddle2Texture); // Let the GameComponents update UpdateComponents(); } void CheckPaddleBoundry(Entity paddle, Texture2D texture) { // Keep the paddle on the screen if (paddle.ySpeed > 0f) { if (paddle.Position.Y + texture.Height > Window.ClientHeight) { paddle.ySpeed = 0f; paddle.Position.Y = Window.ClientHeight - texture.Height; } } else if (paddle.Position.Y < 0f) { paddle.ySpeed = 0f; paddle.Position.Y = 0f; } } protected override void Draw() { // Make sure we have a valid device if (!graphics.EnsureDevice()) return; graphics.GraphicsDevice.Clear(Color.CornflowerBlue); graphics.GraphicsDevice.BeginScene(); // Draw all entities sprites.Begin(); sprites.Draw(background, new Vector2(0, 0), Color.White); sprites.Draw(paddle1Texture, paddle1.Position, Color.White); sprites.Draw(paddle2Texture, paddle2.Position, Color.White); sprites.Draw(ballTexture, ball.Position, Color.White); sprites.End(); // Let the GameComponents draw DrawComponents(); graphics.GraphicsDevice.EndScene(); graphics.GraphicsDevice.Present(); } } }