using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using System; //open content editor with mgcb-editor-windows namespace Pong { public class Game1 : Game { private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; private Random random; private KeyboardState kState; public Texture2D ballTexture; public Texture2D playerOneTexture; public Texture2D playerTwoTexture; public Rectangle playerOne; public Rectangle playerTwo; Vector2 ballPosition; Vector2 playerOnePosition; Vector2 playerTwoPosition; Vector2 ballDir; float ballSpeed; float playerSpeed; public Game1() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; Window.AllowUserResizing = true; } protected override void Initialize() { // TODO: Add your initialization logic here Window.Title = "Pong"; _graphics.PreferredBackBufferHeight = 720; _graphics.PreferredBackBufferWidth = 1280; _graphics.ApplyChanges(); random = new Random(); playerOneTexture = new Texture2D(_graphics.GraphicsDevice, 20, 125); playerTwoTexture = new Texture2D(_graphics.GraphicsDevice, 20, 125); //filling the player rectangles with color data Color[] data = new Color[playerOneTexture.Width*playerOneTexture.Height]; for (int i = 0; i < data.Length; ++i) data[i] = Color.White; playerOneTexture.SetData(data); playerTwoTexture.SetData(data); ballPosition = new Vector2(_graphics.PreferredBackBufferWidth / 2, _graphics.PreferredBackBufferHeight / 2); ballDir = new Vector2(random.Next(-5, 5), random.Next(-7, 7)); ballDir.X = ballDir.X < 0 ? -5 : 5; ballSpeed = 100.0f; playerSpeed = 500f; //players starting position playerOnePosition = new Vector2(25, _graphics.PreferredBackBufferHeight / 2 - playerOneTexture.Height / 2); playerTwoPosition = new Vector2(_graphics.PreferredBackBufferWidth - 50, _graphics.PreferredBackBufferHeight / 2 - playerTwoTexture.Height / 2); base.Initialize(); } protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here ballTexture = Content.Load("Textures/ball"); } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); kState = Keyboard.GetState(); //Player one movement if (kState.IsKeyDown(Keys.W)) { playerOnePosition.Y -= playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; if(playerOnePosition.Y < 0) { playerOnePosition.Y = 0; } } if (kState.IsKeyDown(Keys.S)) { playerOnePosition.Y += playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; if (playerOnePosition.Y + playerOneTexture.Height > _graphics.PreferredBackBufferHeight) { playerOnePosition.Y = _graphics.PreferredBackBufferHeight - playerOneTexture.Height; } } //Player two movement if (kState.IsKeyDown(Keys.Up)) { playerTwoPosition.Y -= playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; if (playerTwoPosition.Y < 0) { playerTwoPosition.Y = 0; } } if (kState.IsKeyDown(Keys.Down)) { playerTwoPosition.Y += playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; if (playerTwoPosition.Y + playerTwoTexture.Height > _graphics.PreferredBackBufferHeight) { playerTwoPosition.Y = _graphics.PreferredBackBufferHeight - playerTwoTexture.Height; } } ballPosition.X += ballDir.X * ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; ballPosition.Y += ballDir.Y * ballSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; //Bouncing if (ballPosition.X > _graphics.PreferredBackBufferWidth - ballTexture.Width / 2) { ballDir.X = -ballDir.X; //_graphics.PreferredBackBufferWidth - ballTexture.Width / 2; } else if (ballPosition.X < ballTexture.Width / 2) { ballDir.X = -ballDir.X; //ballPosition.X = ballTexture.Width / 2; } if (ballPosition.Y > _graphics.PreferredBackBufferHeight - ballTexture.Height / 2) { ballDir.Y = -ballDir.Y; //ballPosition.Y = _graphics.PreferredBackBufferHeight - ballTexture.Height / 2; } else if (ballPosition.Y < ballTexture.Height / 2) { ballDir.Y = -ballDir.Y; //ballPosition.Y = ballTexture.Height / 2; } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); // TODO: Add your drawing code here _spriteBatch.Begin(); _spriteBatch.Draw(ballTexture, ballPosition, null, Color.White, 0f, new Vector2(ballTexture.Width / 2, ballTexture.Height / 2), Vector2.One, SpriteEffects.None, 0f); _spriteBatch.Draw(playerOneTexture, new Rectangle((int)playerOnePosition.X, (int)playerOnePosition.Y, playerOneTexture.Width, playerOneTexture.Height), Color.White); _spriteBatch.Draw(playerTwoTexture, new Rectangle((int)playerTwoPosition.X, (int)playerTwoPosition.Y, playerTwoTexture.Width, playerTwoTexture.Height), Color.White); _spriteBatch.End(); base.Draw(gameTime); } } }