Advertisement
rohits134

Game1.cs

Jul 5th, 2012
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.99 KB | None | 0 0
  1. namespace Pong_Clone
  2. {
  3.     using System;
  4.     using Microsoft.Xna.Framework;
  5.     using Microsoft.Xna.Framework.Graphics;
  6.     using Microsoft.Xna.Framework.Input;
  7.  
  8.     public class Game1 : Microsoft.Xna.Framework.Game
  9.     {
  10.         public static GameStates gamestate;
  11.         private GraphicsDeviceManager graphics;
  12.         private SpriteBatch spriteBatch;
  13.         private Bat rightBat;
  14.         private Bat leftBat;
  15.         private Ball ball;
  16.         private Menu menu;
  17.         private SpriteFont arial;
  18.         private int resetTimer;
  19.         private bool resetTimerInUse;
  20.         private bool lastScored;
  21.  
  22.         private Input input;
  23.         private int screenWidth;
  24.         private int screenHeight;
  25.  
  26.         public enum GameStates
  27.         {
  28.             Menu,
  29.             Running,
  30.             End
  31.         }
  32.  
  33.         public Game1()
  34.         {
  35.             graphics = new GraphicsDeviceManager(this);
  36.             Content.RootDirectory = "Content";
  37.         }
  38.  
  39.         protected override void Initialize()
  40.         {
  41.             screenHeight = 600;
  42.             screenWidth = 800;
  43.             menu = new Menu();
  44.             gamestate = GameStates.Menu;
  45.             resetTimer = 0;
  46.             resetTimerInUse = true;
  47.             lastScored = false;
  48.             graphics.PreferredBackBufferWidth = screenWidth;
  49.             graphics.PreferredBackBufferHeight = screenHeight;
  50.             graphics.IsFullScreen = false;
  51.             graphics.ApplyChanges();
  52.             ball = new Ball(Content, new Vector2(screenWidth, screenHeight));
  53.             SetUpMulti();
  54.             input = new Input();
  55.             base.Initialize();
  56.         }
  57.  
  58.         protected override void LoadContent()
  59.         {
  60.             arial = Content.Load<SpriteFont>("Arial");
  61.             spriteBatch = new SpriteBatch(GraphicsDevice);
  62.  
  63.            
  64.         }
  65.  
  66.         protected override void UnloadContent()
  67.         {
  68.             // TODO: Unload any non ContentManager content here
  69.         }
  70.  
  71.         private void SetUpSingle()
  72.         {
  73.             rightBat = new AIBat(Content, new Vector2(screenWidth, screenHeight), false);
  74.             leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  75.         }
  76.  
  77.         private void SetUpMulti()
  78.         {
  79.             rightBat = new Bat(Content, new Vector2(screenWidth, screenHeight), false);
  80.             leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  81.         }
  82.  
  83.         private void IncreaseSpeed()
  84.         {
  85.             ball.IncreaseSpeed();
  86.             leftBat.IncreaseSpeed();
  87.             rightBat.IncreaseSpeed();
  88.         }
  89.  
  90.         protected override void Update(GameTime gameTime)
  91.         {
  92.             input.Update();
  93.  
  94.             if (gamestate == GameStates.Running)
  95.             {
  96.                 if (leftBat.GetPoints() > 9)
  97.                 {
  98.                     menu.InfoText = "Left Player Wins";
  99.                     gamestate = GameStates.End;
  100.                 }
  101.                 else if (rightBat.GetPoints() > 9)
  102.                 {
  103.                     menu.InfoText = "Right Player Wins";
  104.                     gamestate = GameStates.End;
  105.                 }
  106.                 if (resetTimerInUse)
  107.                 {
  108.                     resetTimer++;
  109.                     ball.Stop();
  110.                 }
  111.  
  112.                 if (resetTimer == 120)
  113.                 {
  114.                     resetTimerInUse = false;
  115.                     ball.Reset(lastScored);
  116.                     resetTimer = 0;
  117.                 }
  118.  
  119.                 if (rightBat.GetType() != typeof(Pong_Clone.AIBat))
  120.                 {
  121.                     if (input.LeftDown) leftBat.MoveDown();
  122.                     else if ((input.LeftUp)) leftBat.MoveUp();
  123.                     if (input.RightDown) rightBat.MoveDown();
  124.                     else if (input.RightUp) rightBat.MoveUp();
  125.                 }
  126.                 else if (rightBat.GetType() == typeof(Pong_Clone.AIBat))
  127.                 {
  128.                     if (input.LeftDown) leftBat.MoveDown();
  129.                     else if ((input.LeftUp)) leftBat.MoveUp();
  130.                     if (input.RightDown) leftBat.MoveDown();
  131.                     else if (input.RightUp) leftBat.MoveUp();
  132.                 }
  133.  
  134.                 leftBat.UpdatePosition(ball);
  135.                 rightBat.UpdatePosition(ball);
  136.                 ball.UpdatePosition();
  137.                 if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
  138.                 {
  139.                     if (rightBat.GetSize().Intersects(ball.GetSize()))
  140.                     {
  141.                         ball.BatHit(CheckHitLocation(rightBat));
  142.                     }
  143.                 }
  144.                 else if (leftBat.GetSize().Intersects(ball.GetSize()))
  145.                 {
  146.                     ball.BatHit(CheckHitLocation(leftBat));
  147.                 }
  148.  
  149.  
  150.  
  151.                 if (!resetTimerInUse)
  152.                 {
  153.                     if (ball.GetPosition().X > screenWidth)
  154.                     {
  155.                         resetTimerInUse = true;
  156.                         lastScored = true;
  157.                         leftBat.IncrementPoints();
  158.                         IncreaseSpeed();
  159.                     }
  160.                     else if (ball.GetPosition().X < 0)
  161.                     {
  162.  
  163.                         resetTimerInUse = true;
  164.                         lastScored = false;
  165.  
  166.                         rightBat.IncrementPoints();
  167.                         IncreaseSpeed();
  168.                     }
  169.                 }
  170.             }
  171.             else if (gamestate == GameStates.Menu)
  172.             {
  173.                 if (input.RightDown || input.LeftDown)
  174.                 {
  175.                     menu.Iterator++;
  176.                 }
  177.                 else if (input.RightUp || input.LeftUp)
  178.                 {
  179.                     menu.Iterator--;
  180.                 }
  181.  
  182.                 if (input.MenuSelect)
  183.                 {
  184.                     if (menu.Iterator == 0)
  185.                     {
  186.                         gamestate = GameStates.Running;
  187.                         SetUpSingle();
  188.                     }
  189.                     else if (menu.Iterator == 1)
  190.                     {
  191.                         gamestate = GameStates.Running;
  192.                         SetUpMulti();
  193.                     }
  194.                     else if (menu.Iterator == 2)
  195.                     {
  196.                         this.Exit();
  197.                     }
  198.                     menu.Iterator = 0;
  199.                 }
  200.             }
  201.             else if (gamestate == GameStates.End)
  202.             {
  203.                 if (input.MenuSelect)
  204.                 {
  205.                     gamestate = GameStates.Menu;
  206.                 }
  207.             }
  208.  
  209.             // TODO: Add your update logic here
  210.             base.Update(gameTime);
  211.         }
  212.  
  213.         private int CheckHitLocation(Bat bat)
  214.         {
  215.             int block = 0;
  216.             if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20) block = 1;
  217.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2) block = 2;
  218.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3) block = 3;
  219.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4) block = 4;
  220.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5) block = 5;
  221.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6) block = 6;
  222.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7) block = 7;
  223.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8) block = 8;
  224.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19) block = 9;
  225.             else block = 10;
  226.             return block;
  227.         }
  228.  
  229.         protected override void Draw(GameTime gameTime)
  230.         {
  231.             GraphicsDevice.Clear(Color.Black);
  232.  
  233.             // TODO: Add your drawing code here
  234.             spriteBatch.Begin();
  235.             if (gamestate == GameStates.Running)
  236.             {
  237.                 leftBat.Draw(spriteBatch);
  238.                 rightBat.Draw(spriteBatch);
  239.                 ball.Draw(spriteBatch);
  240.                 spriteBatch.DrawString(arial, leftBat.GetPoints().ToString(), new Vector2(screenWidth / 4 - arial.MeasureString(rightBat.GetPoints().ToString()).X, 20), Color.White);
  241.                 spriteBatch.DrawString(arial, rightBat.GetPoints().ToString(), new Vector2(screenWidth / 4 * 3 - arial.MeasureString(rightBat.GetPoints().ToString()).X, 20), Color.White);
  242.             }
  243.             else if (gamestate == GameStates.Menu)
  244.             {
  245.                 menu.DrawMenu(spriteBatch, screenWidth, arial);
  246.             }
  247.             else if (gamestate == GameStates.End)
  248.             {
  249.                 menu.DrawEndScreen(spriteBatch, screenWidth, arial);
  250.             }
  251.             spriteBatch.End();
  252.  
  253.             base.Draw(gameTime);
  254.         }
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement