Guest User

Dave XNA Pong

a guest
Mar 26th, 2012
26
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #region Using Statements
  2. using System;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Audio;
  8. using Microsoft.Xna.Framework.Media;
  9. using Microsoft.Xna.Framework.Input;
  10. #endregion
  11.  
  12. namespace MyXnaGame
  13. {
  14.     /// <summary>
  15.     /// Sample showing how to manage different game states, with transitions
  16.     /// between menu screens, a loading screen, the game itself, and a pause
  17.     /// menu. This main game class is extremely simple: all the interesting
  18.     /// stuff happens in the ScreenManager component.
  19.     /// </summary>
  20.     public class GameStateManagementGame : Microsoft.Xna.Framework.Game
  21.     {
  22.         #region Fields
  23.  
  24.         GraphicsDeviceManager graphics;
  25.         ScreenManager screenManager;
  26.         public Song PlayingSong { get; private set; }
  27.  
  28.         private int screenWidth;
  29.         private int screenHeight;
  30.         private Input input;
  31.         private Bat rightBat;
  32.         private Bat leftBat;
  33.         private Ball ball;
  34.         SpriteBatch spriteBatch;
  35.         private int resetTimer;
  36.         private bool resetTimerInUse;
  37.         private bool lastScored;
  38.         SpriteFont arial;
  39.  
  40.        
  41.        
  42.         // By preloading any assets used by UI rendering, we avoid framerate glitches
  43.         // when they suddenly need to be loaded in the middle of a menu transition.
  44.         static readonly string[] preloadAssets =
  45.         {
  46.             "gradient",
  47.         };
  48.  
  49.  
  50.         #endregion
  51.  
  52.         #region Initialization
  53.  
  54.         protected override void Initialize()
  55.         {
  56.             // TODO: Add your initialization logic here
  57.             screenHeight = 600;
  58.             screenWidth = 800;
  59.             graphics.PreferredBackBufferHeight = screenHeight;
  60.             graphics.PreferredBackBufferWidth = screenWidth;
  61.             graphics.ApplyChanges();
  62.             input = new Input();
  63.             rightBat = new Bat(Content, new Vector2(screenWidth, screenHeight), false);
  64.             leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  65.             ball = new Ball(Content, new Vector2(screenWidth, screenHeight));
  66.             ball.Reset(true);
  67.             resetTimer = 0;
  68.             resetTimerInUse = true;
  69.             lastScored = false;  
  70.  
  71.             base.Initialize();
  72.         }
  73.  
  74.         /// <summary>
  75.         /// The main game constructor.
  76.         /// </summary>
  77.         public GameStateManagementGame()
  78.         {
  79.             Content.RootDirectory = "Content";
  80.  
  81.             graphics = new GraphicsDeviceManager(this);
  82.  //           graphics.PreferredBackBufferWidth = screenWidth;
  83. //            graphics.PreferredBackBufferHeight = screenHeight;
  84.  
  85.             // Create the screen manager component.
  86.             screenManager = new ScreenManager(this);
  87.  
  88.             Components.Add(screenManager);
  89.  
  90.             // Activate the first screens.
  91.             screenManager.AddScreen(new BackgroundScreen(), null);
  92.             //screenManager.AddScreen(new MainMenuScreen(), null);
  93.             screenManager.AddScreen(new PressStartScreen(), null);
  94.         }
  95.  
  96.  
  97.         /// <summary>
  98.         /// Loads graphics content.
  99.         /// </summary>
  100.         protected override void LoadContent()
  101.         {
  102.            
  103.             // Create a new SpriteBatch, which can be used to draw textures.
  104.             spriteBatch = new SpriteBatch(GraphicsDevice);
  105.             PlayingSong = Content.Load<Song>(@"sfx/boomer");
  106.             arial = Content.Load<SpriteFont>("Arial");
  107.  
  108.             foreach (string asset in preloadAssets)
  109.             {
  110.                 Content.Load<object>(asset);
  111.             }
  112.         }
  113.        
  114.         #endregion
  115.  
  116.         /// <summary>
  117.         /// Allows the game to run logic such as updating the world,
  118.         /// checking for collisions, gathering input, and playing audio.
  119.         /// </summary>
  120.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  121.         protected override void Update(GameTime gameTime)
  122.         {
  123.             input.Update();
  124.             if (resetTimerInUse)
  125.             {
  126.                 resetTimer++;
  127.                 ball.Stop();
  128.             }
  129.             if (resetTimer == 120)
  130.             {
  131.                 resetTimerInUse = false;
  132.                 ball.Reset(lastScored);
  133.                 resetTimer = 0;
  134.             }
  135.             if (input.LeftDown) leftBat.MoveDown();
  136.             else if ((input.LeftUp)) leftBat.MoveUp();
  137.             if (input.RightDown) leftBat.MoveDown();
  138.             else if (input.RightUp) leftBat.MoveUp();
  139.             leftBat.UpdatePosition(ball);
  140.             rightBat.UpdatePosition(ball);
  141.             ball.UpdatePosition();
  142.             if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
  143.             {
  144.                 if (rightBat.GetSize().Intersects(ball.GetSize()))
  145.                 {
  146.                     ball.BatHit(CheckHitLocation(rightBat));
  147.                 }
  148.             }
  149.             else if (leftBat.GetSize().Intersects(ball.GetSize()))
  150.             {
  151.                 ball.BatHit(CheckHitLocation(leftBat));
  152.             }
  153.  
  154.             if (!resetTimerInUse)
  155.             {
  156.                 if (ball.GetPosition().X > screenWidth)
  157.                 {
  158.                     resetTimerInUse = true;
  159.                     lastScored = true;
  160.                     leftBat.IncrementPoints();
  161.                 }
  162.                 else if (ball.GetPosition().X < 0)
  163.                 {
  164.                     resetTimerInUse = true;
  165.                     lastScored = false;
  166.                     rightBat.IncrementPoints();
  167.                 }
  168.             }
  169.             base.Update(gameTime);
  170.         }
  171.  
  172.         private int CheckHitLocation(Bat bat)
  173.         {
  174.             int block = 0;
  175.             if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20) block = 1;
  176.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2) block = 2;
  177.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3) block = 3;
  178.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4) block = 4;
  179.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5) block = 5;
  180.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6) block = 6;
  181.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7) block = 7;
  182.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8) block = 8;
  183.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19) block = 9;
  184.             else block = 10;
  185.             return block;
  186.         }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.         #region Draw
  194.  
  195.  
  196.         /// <summary>
  197.         /// This is called when the game should draw itself.
  198.         /// </summary>
  199.         protected override void Draw(GameTime gameTime)
  200.         {
  201.             graphics.GraphicsDevice.Clear(Color.Black);
  202.  
  203.    
  204.  
  205.  
  206.             // The real drawing happens inside the screen manager component.
  207.             base.Draw(gameTime);
  208.         }
  209.  
  210.  
  211.         #endregion
  212.     }
  213.  
  214.  
  215.     #region Entry Point
  216.  
  217.     /// <summary>
  218.     /// The main entry point for the application.
  219.     /// </summary>
  220.     static class Program
  221.     {
  222.         static void Main()
  223.         {
  224.             using (GameStateManagementGame game = new GameStateManagementGame())
  225.             {
  226.                 game.Run();
  227.             }
  228.         }
  229.     }
  230.  
  231.     #endregion
  232. }
RAW Paste Data