Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- namespace Pong
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D pongPaddle1;
- Texture2D pongPaddle2;
- Texture2D pongBall;
- Vector2 PongPaddle1;
- Vector2 PongPaddle2;
- Vector2 Ball;
- Vector2 BallStartPosition;
- Vector2 BallVelocity;
- Vector2 BallRebound;
- Vector2 BallInboundL;
- Vector2 BallInboundR;
- Random r = new Random();
- const int paddleSpeed = 10;
- float SafeAreaZone = 0.05f;
- Rectangle safeBounds;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- graphics.PreferredBackBufferWidth = 1280;
- graphics.PreferredBackBufferHeight = 720;
- graphics.ApplyChanges();
- base.Initialize();
- }
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- Viewport viewport = graphics.GraphicsDevice.Viewport;
- safeBounds = new Rectangle(
- (int)(viewport.Width * SafeAreaZone),
- (int)(viewport.Height * SafeAreaZone),
- (int)(viewport.Width * (1 - 2 * SafeAreaZone)),
- (int)(viewport.Height * (1 - 2 * SafeAreaZone)));
- pongPaddle1 = Content.Load<Texture2D>("pongpaddle1");
- pongPaddle2 = Content.Load<Texture2D>("pongpaddle2");
- pongBall = Content.Load<Texture2D>("PongBall");
- PongPaddle1 = new Vector2();
- PongPaddle1.X = graphics.GraphicsDevice.Viewport.Width / 100 * 5;
- PongPaddle1.Y = graphics.GraphicsDevice.Viewport.Height / 2 - pongPaddle1.Height / 2;
- PongPaddle2 = new Vector2();
- PongPaddle2.X = graphics.GraphicsDevice.Viewport.Width / 100 * 97;
- PongPaddle2.Y = graphics.GraphicsDevice.Viewport.Height / 2 - pongPaddle2.Height / 2;
- BallInboundL = new Vector2();
- BallInboundL.X = r.Next(-14, -10);
- BallInboundL.Y = 0;
- BallInboundR = new Vector2();
- BallInboundR.X = r.Next(10, 14);
- BallInboundR.Y = 0;
- Ball = new Vector2();
- Ball.X = graphics.GraphicsDevice.Viewport.Width / 2 - (pongBall.Width / 2);
- Ball.Y = graphics.GraphicsDevice.Viewport.Height / 2 - (pongBall.Height / 2);
- BallStartPosition = new Vector2();
- BallStartPosition.X = graphics.GraphicsDevice.Viewport.Width / 2 - (pongBall.Width / 2);
- BallStartPosition.Y = graphics.GraphicsDevice.Viewport.Height / 2 - (pongBall.Height / 2);
- BallVelocity = new Vector2();
- BallVelocity.X = r.Next(11, 16);
- BallVelocity.Y = 0;
- BallRebound = new Vector2();
- BallRebound.X = r.Next(12, 16);
- BallRebound.Y = r.Next(12, 16);
- }
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- protected override void Update(GameTime gameTime)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- PongPaddle1.Y = MathHelper.Clamp(PongPaddle1.Y,
- safeBounds.Top - 30, safeBounds.Bottom - 70);
- PongPaddle2.Y = MathHelper.Clamp(PongPaddle2.Y,
- safeBounds.Top - 30, safeBounds.Bottom - 70);
- if (Keyboard.GetState().IsKeyDown(Keys.Up) == true)
- {
- PongPaddle1.Y -= paddleSpeed;
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Down) == true)
- {
- PongPaddle1.Y += paddleSpeed;
- }
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.Black);
- spriteBatch.Begin();
- spriteBatch.Draw(pongPaddle1, PongPaddle1, Color.White);
- spriteBatch.Draw(pongPaddle2, PongPaddle2, Color.White);
- spriteBatch.Draw(pongBall, Ball, Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement