Advertisement
Guest User

Untitled

a guest
Apr 28th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace Pong
  13. {
  14.     public class Game1 : Microsoft.Xna.Framework.Game
  15.     {
  16.         GraphicsDeviceManager graphics;
  17.         SpriteBatch spriteBatch;
  18.  
  19.         Texture2D pongPaddle1;
  20.         Texture2D pongPaddle2;
  21.         Texture2D pongBall;
  22.  
  23.         Vector2 PongPaddle1;
  24.         Vector2 PongPaddle2;
  25.         Vector2 Ball;
  26.         Vector2 BallStartPosition;
  27.         Vector2 BallVelocity;
  28.         Vector2 BallRebound;
  29.         Vector2 BallInboundL;
  30.         Vector2 BallInboundR;
  31.  
  32.         Random r = new Random();
  33.  
  34.         const int paddleSpeed = 10;
  35.  
  36.         float SafeAreaZone = 0.05f;
  37.  
  38.         Rectangle safeBounds;
  39.  
  40.         public Game1()
  41.         {
  42.             graphics = new GraphicsDeviceManager(this);
  43.             Content.RootDirectory = "Content";
  44.         }
  45.  
  46.         protected override void Initialize()
  47.         {
  48.             graphics.PreferredBackBufferWidth = 1280;
  49.             graphics.PreferredBackBufferHeight = 720;
  50.             graphics.ApplyChanges();
  51.  
  52.             base.Initialize();
  53.         }
  54.  
  55.         protected override void LoadContent()
  56.         {
  57.             // Create a new SpriteBatch, which can be used to draw textures.
  58.             spriteBatch = new SpriteBatch(GraphicsDevice);
  59.  
  60.             Viewport viewport = graphics.GraphicsDevice.Viewport;
  61.             safeBounds = new Rectangle(
  62.                (int)(viewport.Width * SafeAreaZone),
  63.                (int)(viewport.Height * SafeAreaZone),
  64.                (int)(viewport.Width * (1 - 2 * SafeAreaZone)),
  65.                (int)(viewport.Height * (1 - 2 * SafeAreaZone)));
  66.  
  67.             pongPaddle1 = Content.Load<Texture2D>("pongpaddle1");
  68.             pongPaddle2 = Content.Load<Texture2D>("pongpaddle2");
  69.             pongBall = Content.Load<Texture2D>("PongBall");
  70.  
  71.             PongPaddle1 = new Vector2();
  72.             PongPaddle1.X = graphics.GraphicsDevice.Viewport.Width / 100 * 5;
  73.             PongPaddle1.Y = graphics.GraphicsDevice.Viewport.Height / 2 - pongPaddle1.Height / 2;
  74.  
  75.             PongPaddle2 = new Vector2();
  76.             PongPaddle2.X = graphics.GraphicsDevice.Viewport.Width / 100 * 97;
  77.             PongPaddle2.Y = graphics.GraphicsDevice.Viewport.Height / 2 - pongPaddle2.Height / 2;
  78.  
  79.             BallInboundL = new Vector2();
  80.             BallInboundL.X = r.Next(-14, -10);
  81.             BallInboundL.Y = 0;
  82.  
  83.             BallInboundR = new Vector2();
  84.             BallInboundR.X = r.Next(10, 14);
  85.             BallInboundR.Y = 0;
  86.  
  87.             Ball = new Vector2();
  88.             Ball.X = graphics.GraphicsDevice.Viewport.Width / 2 - (pongBall.Width / 2);
  89.             Ball.Y = graphics.GraphicsDevice.Viewport.Height / 2 - (pongBall.Height / 2);
  90.  
  91.             BallStartPosition = new Vector2();
  92.             BallStartPosition.X = graphics.GraphicsDevice.Viewport.Width / 2 - (pongBall.Width / 2);
  93.             BallStartPosition.Y = graphics.GraphicsDevice.Viewport.Height / 2 - (pongBall.Height / 2);
  94.  
  95.             BallVelocity = new Vector2();
  96.             BallVelocity.X = r.Next(11, 16);
  97.             BallVelocity.Y = 0;
  98.  
  99.             BallRebound = new Vector2();
  100.             BallRebound.X = r.Next(12, 16);
  101.             BallRebound.Y = r.Next(12, 16);
  102.         }
  103.  
  104.         protected override void UnloadContent()
  105.         {
  106.             // TODO: Unload any non ContentManager content here
  107.         }
  108.  
  109.         protected override void Update(GameTime gameTime)
  110.         {
  111.             // Allows the game to exit
  112.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  113.                 this.Exit();
  114.  
  115.             PongPaddle1.Y = MathHelper.Clamp(PongPaddle1.Y,
  116.                 safeBounds.Top - 30, safeBounds.Bottom - 70);
  117.  
  118.             PongPaddle2.Y = MathHelper.Clamp(PongPaddle2.Y,
  119.                 safeBounds.Top - 30, safeBounds.Bottom - 70);
  120.  
  121.             if (Keyboard.GetState().IsKeyDown(Keys.Up) == true)
  122.             {
  123.                 PongPaddle1.Y -= paddleSpeed;
  124.             }
  125.  
  126.             if (Keyboard.GetState().IsKeyDown(Keys.Down) == true)
  127.             {
  128.                 PongPaddle1.Y += paddleSpeed;
  129.             }
  130.  
  131.             base.Update(gameTime);
  132.         }
  133.  
  134.         protected override void Draw(GameTime gameTime)
  135.         {
  136.             GraphicsDevice.Clear(Color.Black);
  137.  
  138.             spriteBatch.Begin();
  139.             spriteBatch.Draw(pongPaddle1, PongPaddle1, Color.White);
  140.             spriteBatch.Draw(pongPaddle2, PongPaddle2, Color.White);
  141.             spriteBatch.Draw(pongBall, Ball, Color.White);
  142.             spriteBatch.End();
  143.  
  144.             base.Draw(gameTime);
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement