Advertisement
DaveVoyles

Pong Turbo W/ Cooldown 2

Apr 11th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 28.95 KB | None | 0 0
  1. This is for my pong game. I have the turbo working fine, in that I hit spacebar and my speed increases from 7 (standard) to 12. After each point is scored, it increases the speed of the game by 0.5f as well, as you'll notice in the "IncreaseSpeed()" method.
  2.  
  3. The problem however, is that I can't turn this off. I would like for it to turn off after 2 seconds, and then have a brief cooldown of a few seconds seconds. I currently have an variable int for coolDown in there, but am unable to get it to work.
  4.  
  5. I've attached my Bat class, Game1 class, and Input classes.
  6.  
  7. Inside of the Game1 class you will see my if / else statements for moving the bats, in particular the one for turbo.
  8.  
  9. How would you go about resolving this?
  10. */
  11.  
  12. namespace Pong
  13. {
  14.    using System;
  15.    using Microsoft.Xna.Framework;
  16.    using Microsoft.Xna.Framework.Graphics;
  17.    using Microsoft.Xna.Framework.Input;
  18.    using Microsoft.Xna.Framework.Audio;
  19.    using Microsoft.Xna.Framework.Media;
  20.  
  21.  
  22.    /// <summary>
  23.    /// This is the main type for your game
  24.    /// </summary>
  25.    public class Game1 : Microsoft.Xna.Framework.Game
  26.    {
  27.        
  28.        public static GameStates gamestate;
  29.        private GraphicsDeviceManager graphics;
  30.        private Texture2D backgroundTexture;
  31.        private SpriteBatch spriteBatch;
  32.        private Bat rightBat;
  33.        private Bat leftBat;
  34.        private Ball ball;
  35.        private Menu menu;
  36.        private SpriteFont arial;
  37.        private int resetTimer;
  38.        private bool resetTimerInUse;
  39.        private bool lastScored;
  40.  
  41.        private SoundEffect menuButton;
  42.        private SoundEffect menuClose;
  43.        public Song MainMenuSong { get; private set; }
  44.        public Song PlayingSong { get; private set; }
  45. //        public Song mySong;
  46.        
  47.        private Input input;
  48.        private int screenWidth;
  49.        private int screenHeight;
  50.  
  51.        // For resetting the speed burst of the paddle
  52.        int coolDown = 0;
  53.  
  54.        public enum GameStates
  55.        {
  56.            Menu,
  57.            Running,
  58.            End
  59.        }
  60.  
  61.        public Game1()
  62.        {
  63.            graphics = new GraphicsDeviceManager(this);
  64.            Content.RootDirectory = "Content";
  65.        }
  66.      
  67.  
  68.        /// <summary>
  69.        /// Allows the game to perform any initialization it needs to before starting to run.
  70.        /// This is where it can query for any required services and load any non-graphic
  71.        /// related content.  Calling base.Initialize will enumerate through any components
  72.        /// and initialize them as well.
  73.        /// </summary>
  74.        protected override void Initialize()
  75.        {
  76.            screenWidth = 1280;
  77.            screenHeight = 720;
  78.            menu = new Menu();
  79.            gamestate = GameStates.Menu;
  80.            resetTimer = 0;
  81.            resetTimerInUse = true;
  82.            lastScored = false;
  83.            graphics.PreferredBackBufferWidth = screenWidth;
  84.            graphics.PreferredBackBufferHeight = screenHeight;
  85.            graphics.IsFullScreen = false;
  86.            graphics.ApplyChanges();
  87.          
  88.  
  89.  
  90.            // TODO: Add your initialization logic here
  91.            ball = new Ball(Content, new Vector2(screenWidth, screenHeight));
  92.            SetUpMulti();
  93.            input = new Input();
  94.            base.Initialize();
  95.        }
  96.  
  97.  
  98.        /// <summary>
  99.        /// LoadContent will be called once per game and is the place to load
  100.        /// all of your content.
  101.        /// </summary>
  102.        protected override void LoadContent()
  103.        {
  104.            arial = Content.Load<SpriteFont>("Arial");
  105.            // Create a new SpriteBatch, which can be used to draw textures.
  106.            spriteBatch = new SpriteBatch(GraphicsDevice);
  107.            backgroundTexture = Content.Load<Texture2D>(@"gfx/background");
  108.            menuButton = Content.Load<SoundEffect>(@"sfx/menuButton");
  109.            menuClose = Content.Load<SoundEffect>(@"sfx/menuClose");
  110.            MainMenuSong = Content.Load<Song>(@"sfx/getWeapon");
  111.            PlayingSong = Content.Load<Song>(@"sfx/boomer");
  112.            MediaPlayer.IsRepeating = true;
  113.            MediaPlayer.Play(MainMenuSong);
  114.            //            mySong = Content.Load<Song>(@"sfx/getWeapon");
  115.            //            MediaPlayer.Play(mySong);
  116.  
  117.            
  118.        }
  119.  
  120.        /// <summary>
  121.        /// UnloadContent will be called once per game and is the place to unload
  122.        /// all content.
  123.        /// </summary>
  124.        protected override void UnloadContent()
  125.        {
  126.            // TODO: Unload any non ContentManager content here
  127.        }
  128.  
  129.        private void SetUpSingle()
  130.        {
  131.            rightBat = new AIBat(Content, new Vector2(screenWidth, screenHeight), false);
  132.            leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  133.        }
  134.  
  135.        private void SetUpMulti()
  136.        {
  137.            rightBat = new Bat(Content, new Vector2(screenWidth, screenHeight), false);
  138.            leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  139.        }
  140.  
  141.        private void IncreaseSpeed()
  142.        {
  143.            ball.IncreaseSpeed();
  144.            leftBat.IncreaseSpeed();
  145.            rightBat.IncreaseSpeed();
  146.        }
  147.  
  148.  
  149.        /// <param name="gameTime">Provides a snapshot of timing values.</param>
  150.        ///
  151.        protected override void Update(GameTime gameTime)
  152.        {
  153.  
  154.            input.Update();
  155.  
  156.            if (gamestate == GameStates.Running)
  157.            {
  158.                if (leftBat.GetPoints() > 5)
  159.                {
  160.                    menu.InfoText = "Game, blouses.";
  161.                    gamestate = GameStates.End;
  162.                }
  163.                else if (rightBat.GetPoints() > 5)
  164.                {
  165.                    menu.InfoText = "You just let the AI beat you.";
  166.                    gamestate = GameStates.End;
  167.                }
  168.                if (resetTimerInUse)
  169.                {
  170.                    resetTimer++;
  171.                    ball.Stop();                
  172.                }
  173.  
  174.                if (resetTimer == 120)
  175.                {
  176.                    resetTimerInUse = false;
  177.                    ball.Reset(lastScored);
  178.                    resetTimer = 0;
  179.                }
  180.  
  181.                              
  182.                if (rightBat.GetType() != typeof(Pong.AIBat))
  183.                {
  184.                    if (input.LeftDown) leftBat.MoveDown();
  185.                    else if ((input.LeftUp)) leftBat.MoveUp();
  186.                    if (input.RightDown) rightBat.MoveDown();
  187.                    else if (input.RightUp) rightBat.MoveUp();
  188.                }
  189.        
  190.                else if (rightBat.GetType() == typeof(Pong.AIBat))
  191.                {
  192.                    if (input.LeftDown) leftBat.MoveDown();
  193.                    else if ((input.LeftUp)) leftBat.MoveUp();
  194.                    if (input.RightDown) leftBat.MoveDown();
  195.                    else if (input.RightUp) leftBat.MoveUp();
  196.                 }
  197.  
  198.              
  199.                leftBat.UpdatePosition(ball);
  200.                rightBat.UpdatePosition(ball);
  201.                ball.UpdatePosition();
  202.  
  203.  
  204.                if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
  205.                {
  206.                    if (rightBat.GetSize().Intersects(ball.GetSize()))
  207.                    {
  208.                        ball.BatHit(CheckHitLocation(rightBat));
  209.                    }
  210.                }
  211.                else if (leftBat.GetSize().Intersects(ball.GetSize()))
  212.                {
  213.                    ball.BatHit(CheckHitLocation(leftBat));
  214.                }
  215.  
  216.  
  217.            // Triggers the turbo button and cooldown
  218.            if  (input.SpaceDown)
  219.            {
  220.                if (coolDown <= 0)
  221.                {
  222.                leftBat.isTurbo = true;
  223.                coolDown = 5000;
  224.                leftBat.moveSpeed = 40.0f;
  225.                }
  226.            }
  227.                else if (!input.SpaceDown)
  228.  
  229.                     {
  230.                         leftBat.isTurbo = false;
  231.                         coolDown -= gameTime.ElapsedGameTime.Milliseconds;
  232.                     }
  233.            
  234.  
  235.            
  236.                if (!resetTimerInUse)
  237.                {
  238.                    if (ball.GetPosition().X > screenWidth)
  239.                    {
  240.                        resetTimerInUse = true;
  241.                        lastScored = true;
  242.  
  243.                        // Checks to see if ball went out of bounds, and triggers warp sfx
  244.                        ball.OutOfBounds();
  245.                        leftBat.IncrementPoints();
  246.                        IncreaseSpeed();
  247.                    }
  248.                    else if (ball.GetPosition().X < 0)
  249.                    {
  250.  
  251.                        resetTimerInUse = true;
  252.                        lastScored = false;
  253.  
  254.                        // Checks to see if ball went out of bounds, and triggers warp sfx
  255.                        ball.OutOfBounds();
  256.                        rightBat.IncrementPoints();
  257.                        IncreaseSpeed();
  258.                    }
  259.                }
  260.            }
  261.            else if (gamestate == GameStates.Menu)
  262.                
  263.            {
  264.  
  265.  
  266.                if (input.RightDown || input.LeftDown)
  267.                {
  268.                    menu.Iterator++;
  269.                    menuButton.Play();
  270.                }
  271.                else if (input.RightUp || input.LeftUp)
  272.                {
  273.                    menu.Iterator--;
  274.                    menuButton.Play();
  275.                }
  276.  
  277.                if (input.MenuSelect)
  278.                {
  279.  
  280.                    if (menu.Iterator == 0)
  281.                    {
  282.                        gamestate = GameStates.Running;
  283.                        SetUpSingle();
  284.                        menuClose.Play();
  285.                    }
  286.                    else if (menu.Iterator == 1)
  287.                    {
  288.                        gamestate = GameStates.Running;
  289.                        SetUpMulti();
  290.                        menuClose.Play();
  291.                    }
  292.                    else if (menu.Iterator == 2)
  293.                    {
  294.                        this.Exit();
  295.                        menuClose.Play();
  296.                    }
  297.                    menu.Iterator = 0;
  298.  
  299.                }
  300.  
  301.            }
  302.  
  303.            else if (gamestate == GameStates.End)
  304.            {
  305.                if (input.MenuSelect)
  306.                {
  307.                    gamestate = GameStates.Menu;
  308.  
  309.                }
  310.            }
  311.  
  312.  
  313.            base.Update(gameTime);
  314.        }
  315.  
  316.        private int CheckHitLocation(Bat bat)
  317.        {
  318.            int block = 0;
  319.            if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20) block = 1;
  320.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2) block = 2;
  321.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3) block = 3;
  322.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4) block = 4;
  323.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5) block = 5;
  324.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6) block = 6;
  325.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7) block = 7;
  326.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8) block = 8;
  327.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19) block = 9;
  328.            else block = 10;
  329.            return block;
  330.        }
  331.  
  332.  
  333.        protected override void Draw(GameTime gameTime)
  334.        {
  335.            GraphicsDevice.Clear(Color.Black);
  336.  
  337.            // TODO: Add your drawing code here
  338.            spriteBatch.Begin();
  339.            if (gamestate == GameStates.Running)
  340.            {
  341.                spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  342.                leftBat.Draw(spriteBatch);
  343.                rightBat.Draw(spriteBatch);
  344.                ball.Draw(spriteBatch);
  345.                spriteBatch.DrawString(arial, leftBat.GetPoints().ToString(), new Vector2(screenWidth / 4 - arial.MeasureString
  346.                    (rightBat.GetPoints().ToString()).X, 20), Color.White);
  347.                spriteBatch.DrawString(arial, rightBat.GetPoints().ToString(), new Vector2(screenWidth / 4 * 3 - arial.MeasureString
  348.                    (rightBat.GetPoints().ToString()).X, 20), Color.White);
  349.                                
  350.            }
  351.            else if (gamestate == GameStates.Menu)
  352.            {
  353.                spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  354.                menu.DrawMenu(spriteBatch, screenWidth, arial);
  355.            }
  356.            else if (gamestate == GameStates.End)
  357.            {
  358.                spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  359.                menu.DrawEndScreen(spriteBatch, screenWidth, arial);
  360.            }
  361.            spriteBatch.End();
  362.  
  363.            base.Draw(gameTime);
  364.        }
  365.  
  366.    }
  367. }
  368.  
  369. /////////////////////////////////////////////////////////////////////////////////////////
  370.  
  371. namespace Pong
  372. {
  373.    using System;
  374.    using Microsoft.Xna.Framework;
  375.    using Microsoft.Xna.Framework.Graphics;
  376.    using Microsoft.Xna.Framework.Input;
  377.    using Microsoft.Xna.Framework.Audio;
  378.    using Microsoft.Xna.Framework.Media;
  379.  
  380.  
  381.    /// <summary>
  382.    /// This is the main type for your game
  383.    /// </summary>
  384.    public class Game1 : Microsoft.Xna.Framework.Game
  385.    {
  386.        
  387.        public static GameStates gamestate;
  388.        private GraphicsDeviceManager graphics;
  389.        private Texture2D backgroundTexture;
  390.        private SpriteBatch spriteBatch;
  391.        private Bat rightBat;
  392.        private Bat leftBat;
  393.        private Ball ball;
  394.        private Menu menu;
  395.        private SpriteFont arial;
  396.        private int resetTimer;
  397.        private bool resetTimerInUse;
  398.        private bool lastScored;
  399.  
  400.        private SoundEffect menuButton;
  401.        private SoundEffect menuClose;
  402.        public Song MainMenuSong { get; private set; }
  403.        public Song PlayingSong { get; private set; }
  404. //        public Song mySong;
  405.        
  406.        private Input input;
  407.        private int screenWidth;
  408.        private int screenHeight;
  409.  
  410.        // For resetting the speed burst of the paddle
  411.        int coolDown = 0;
  412.  
  413.        public enum GameStates
  414.        {
  415.            Menu,
  416.            Running,
  417.            End
  418.        }
  419.  
  420.        public Game1()
  421.        {
  422.            graphics = new GraphicsDeviceManager(this);
  423.            Content.RootDirectory = "Content";
  424.        }
  425.      
  426.  
  427.        /// <summary>
  428.        /// Allows the game to perform any initialization it needs to before starting to run.
  429.        /// This is where it can query for any required services and load any non-graphic
  430.        /// related content.  Calling base.Initialize will enumerate through any components
  431.        /// and initialize them as well.
  432.        /// </summary>
  433.        protected override void Initialize()
  434.        {
  435.            screenWidth = 1280;
  436.            screenHeight = 720;
  437.            menu = new Menu();
  438.            gamestate = GameStates.Menu;
  439.            resetTimer = 0;
  440.            resetTimerInUse = true;
  441.            lastScored = false;
  442.            graphics.PreferredBackBufferWidth = screenWidth;
  443.            graphics.PreferredBackBufferHeight = screenHeight;
  444.            graphics.IsFullScreen = false;
  445.            graphics.ApplyChanges();
  446.          
  447.  
  448.  
  449.            // TODO: Add your initialization logic here
  450.            ball = new Ball(Content, new Vector2(screenWidth, screenHeight));
  451.            SetUpMulti();
  452.            input = new Input();
  453.            base.Initialize();
  454.        }
  455.  
  456.  
  457.        /// <summary>
  458.        /// LoadContent will be called once per game and is the place to load
  459.        /// all of your content.
  460.        /// </summary>
  461.        protected override void LoadContent()
  462.        {
  463.            arial = Content.Load<SpriteFont>("Arial");
  464.            // Create a new SpriteBatch, which can be used to draw textures.
  465.            spriteBatch = new SpriteBatch(GraphicsDevice);
  466.            backgroundTexture = Content.Load<Texture2D>(@"gfx/background");
  467.            menuButton = Content.Load<SoundEffect>(@"sfx/menuButton");
  468.            menuClose = Content.Load<SoundEffect>(@"sfx/menuClose");
  469.            MainMenuSong = Content.Load<Song>(@"sfx/getWeapon");
  470.            PlayingSong = Content.Load<Song>(@"sfx/boomer");
  471.            MediaPlayer.IsRepeating = true;
  472.            MediaPlayer.Play(MainMenuSong);
  473.            //            mySong = Content.Load<Song>(@"sfx/getWeapon");
  474.            //            MediaPlayer.Play(mySong);
  475.  
  476.            
  477.        }
  478.  
  479.        /// <summary>
  480.        /// UnloadContent will be called once per game and is the place to unload
  481.        /// all content.
  482.        /// </summary>
  483.        protected override void UnloadContent()
  484.        {
  485.            // TODO: Unload any non ContentManager content here
  486.        }
  487.  
  488.        private void SetUpSingle()
  489.        {
  490.            rightBat = new AIBat(Content, new Vector2(screenWidth, screenHeight), false);
  491.            leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  492.        }
  493.  
  494.        private void SetUpMulti()
  495.        {
  496.            rightBat = new Bat(Content, new Vector2(screenWidth, screenHeight), false);
  497.            leftBat = new Bat(Content, new Vector2(screenWidth, screenHeight), true);
  498.        }
  499.  
  500.        private void IncreaseSpeed()
  501.        {
  502.            ball.IncreaseSpeed();
  503.            leftBat.IncreaseSpeed();
  504.            rightBat.IncreaseSpeed();
  505.        }
  506.  
  507.  
  508.        /// <param name="gameTime">Provides a snapshot of timing values.</param>
  509.        ///
  510.        protected override void Update(GameTime gameTime)
  511.        {
  512.  
  513.            input.Update();
  514.  
  515.            if (gamestate == GameStates.Running)
  516.            {
  517.                if (leftBat.GetPoints() > 5)
  518.                {
  519.                    menu.InfoText = "Game, blouses.";
  520.                    gamestate = GameStates.End;
  521.                }
  522.                else if (rightBat.GetPoints() > 5)
  523.                {
  524.                    menu.InfoText = "You just let the AI beat you.";
  525.                    gamestate = GameStates.End;
  526.                }
  527.                if (resetTimerInUse)
  528.                {
  529.                    resetTimer++;
  530.                    ball.Stop();                
  531.                }
  532.  
  533.                if (resetTimer == 120)
  534.                {
  535.                    resetTimerInUse = false;
  536.                    ball.Reset(lastScored);
  537.                    resetTimer = 0;
  538.                }
  539.  
  540.                              
  541.                if (rightBat.GetType() != typeof(Pong.AIBat))
  542.                {
  543.                    if (input.LeftDown) leftBat.MoveDown();
  544.                    else if ((input.LeftUp)) leftBat.MoveUp();
  545.                    if (input.RightDown) rightBat.MoveDown();
  546.                    else if (input.RightUp) rightBat.MoveUp();
  547.                }
  548.        
  549.                else if (rightBat.GetType() == typeof(Pong.AIBat))
  550.                {
  551.                    if (input.LeftDown) leftBat.MoveDown();
  552.                    else if ((input.LeftUp)) leftBat.MoveUp();
  553.                    if (input.RightDown) leftBat.MoveDown();
  554.                    else if (input.RightUp) leftBat.MoveUp();
  555.                 }
  556.  
  557.              
  558.                leftBat.UpdatePosition(ball);
  559.                rightBat.UpdatePosition(ball);
  560.                ball.UpdatePosition();
  561.  
  562.  
  563.                if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
  564.                {
  565.                    if (rightBat.GetSize().Intersects(ball.GetSize()))
  566.                    {
  567.                        ball.BatHit(CheckHitLocation(rightBat));
  568.                    }
  569.                }
  570.                else if (leftBat.GetSize().Intersects(ball.GetSize()))
  571.                {
  572.                    ball.BatHit(CheckHitLocation(leftBat));
  573.                }
  574.  
  575.  
  576.            // Triggers the turbo button and cooldown
  577.            if  (input.SpaceDown)
  578.            {
  579.                if (coolDown <= 0)
  580.                {
  581.                leftBat.isTurbo = true;
  582.                coolDown = 5000;
  583.                leftBat.moveSpeed = 40.0f;
  584.                }
  585.            }
  586.                else if (!input.SpaceDown)
  587.  
  588.                     {
  589.                         leftBat.isTurbo = false;
  590.                         coolDown -= gameTime.ElapsedGameTime.Milliseconds;
  591.                     }
  592.            
  593.  
  594.            
  595.                if (!resetTimerInUse)
  596.                {
  597.                    if (ball.GetPosition().X > screenWidth)
  598.                    {
  599.                        resetTimerInUse = true;
  600.                        lastScored = true;
  601.  
  602.                        // Checks to see if ball went out of bounds, and triggers warp sfx
  603.                        ball.OutOfBounds();
  604.                        leftBat.IncrementPoints();
  605.                        IncreaseSpeed();
  606.                    }
  607.                    else if (ball.GetPosition().X < 0)
  608.                    {
  609.  
  610.                        resetTimerInUse = true;
  611.                        lastScored = false;
  612.  
  613.                        // Checks to see if ball went out of bounds, and triggers warp sfx
  614.                        ball.OutOfBounds();
  615.                        rightBat.IncrementPoints();
  616.                        IncreaseSpeed();
  617.                    }
  618.                }
  619.            }
  620.            else if (gamestate == GameStates.Menu)
  621.                
  622.            {
  623.  
  624.  
  625.                if (input.RightDown || input.LeftDown)
  626.                {
  627.                    menu.Iterator++;
  628.                    menuButton.Play();
  629.                }
  630.                else if (input.RightUp || input.LeftUp)
  631.                {
  632.                    menu.Iterator--;
  633.                    menuButton.Play();
  634.                }
  635.  
  636.                if (input.MenuSelect)
  637.                {
  638.  
  639.                    if (menu.Iterator == 0)
  640.                    {
  641.                        gamestate = GameStates.Running;
  642.                        SetUpSingle();
  643.                        menuClose.Play();
  644.                    }
  645.                    else if (menu.Iterator == 1)
  646.                    {
  647.                        gamestate = GameStates.Running;
  648.                        SetUpMulti();
  649.                        menuClose.Play();
  650.                    }
  651.                    else if (menu.Iterator == 2)
  652.                    {
  653.                        this.Exit();
  654.                        menuClose.Play();
  655.                    }
  656.                    menu.Iterator = 0;
  657.  
  658.                }
  659.  
  660.            }
  661.  
  662.            else if (gamestate == GameStates.End)
  663.            {
  664.                if (input.MenuSelect)
  665.                {
  666.                    gamestate = GameStates.Menu;
  667.  
  668.                }
  669.            }
  670.  
  671.  
  672.            base.Update(gameTime);
  673.        }
  674.  
  675.        private int CheckHitLocation(Bat bat)
  676.        {
  677.            int block = 0;
  678.            if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20) block = 1;
  679.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2) block = 2;
  680.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3) block = 3;
  681.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4) block = 4;
  682.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5) block = 5;
  683.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6) block = 6;
  684.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7) block = 7;
  685.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8) block = 8;
  686.            else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19) block = 9;
  687.            else block = 10;
  688.            return block;
  689.        }
  690.  
  691.  
  692.        protected override void Draw(GameTime gameTime)
  693.        {
  694.            GraphicsDevice.Clear(Color.Black);
  695.  
  696.            // TODO: Add your drawing code here
  697.            spriteBatch.Begin();
  698.            if (gamestate == GameStates.Running)
  699.            {
  700.                spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  701.                leftBat.Draw(spriteBatch);
  702.                rightBat.Draw(spriteBatch);
  703.                ball.Draw(spriteBatch);
  704.                spriteBatch.DrawString(arial, leftBat.GetPoints().ToString(), new Vector2(screenWidth / 4 - arial.MeasureString
  705.                    (rightBat.GetPoints().ToString()).X, 20), Color.White);
  706.                spriteBatch.DrawString(arial, rightBat.GetPoints().ToString(), new Vector2(screenWidth / 4 * 3 - arial.MeasureString
  707.                    (rightBat.GetPoints().ToString()).X, 20), Color.White);
  708.                                
  709.            }
  710.            else if (gamestate == GameStates.Menu)
  711.            {
  712.                spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  713.                menu.DrawMenu(spriteBatch, screenWidth, arial);
  714.            }
  715.            else if (gamestate == GameStates.End)
  716.            {
  717.                spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  718.                menu.DrawEndScreen(spriteBatch, screenWidth, arial);
  719.            }
  720.            spriteBatch.End();
  721.  
  722.            base.Draw(gameTime);
  723.        }
  724.  
  725.    }
  726. }
  727.  
  728. /////////////////////////////////////////////////////////////////////////////////////////////////
  729.  
  730. using System;
  731. using System.Collections.Generic;
  732. using System.Linq;
  733. using System.Text;
  734. using Microsoft.Xna.Framework.Input;
  735.  
  736. namespace Pong
  737. {
  738.    public class Input
  739.    {
  740.      
  741.        public KeyboardState keyboardState;
  742.        public KeyboardState lastState;
  743.        public bool isTurbo;
  744.  
  745.        public Input()
  746.        {
  747.            keyboardState = Keyboard.GetState();
  748.            lastState = keyboardState;
  749.        }
  750.  
  751.        public void Update()
  752.        {
  753.            lastState = keyboardState;
  754.            keyboardState = Keyboard.GetState();
  755.        }
  756.  
  757.  
  758.  
  759.        public bool RightUp
  760.        {
  761.            get
  762.            {
  763.                if (Game1.gamestate == Game1.GameStates.Menu)
  764.                {
  765.                    return keyboardState.IsKeyDown(Keys.Up) && lastState.IsKeyUp(Keys.Up);
  766.                }
  767.                else
  768.                {
  769.                    return keyboardState.IsKeyDown(Keys.Up);
  770.                }
  771.            }
  772.        }
  773.  
  774.        public bool RightDown
  775.        {
  776.            get
  777.            {
  778.                if (Game1.gamestate == Game1.GameStates.Menu)
  779.                {
  780.                    return keyboardState.IsKeyDown(Keys.Down) && lastState.IsKeyUp(Keys.Down);
  781.                }
  782.                else
  783.                {
  784.                    return keyboardState.IsKeyDown(Keys.Down);
  785.                }
  786.            }
  787.        }
  788.  
  789.        public bool LeftUp
  790.        {
  791.            get
  792.            {
  793.                if (Game1.gamestate == Game1.GameStates.Menu)
  794.                {
  795.                    return keyboardState.IsKeyDown(Keys.W) && lastState.IsKeyUp(Keys.W);
  796.                }
  797.                else
  798.                {
  799.                    return keyboardState.IsKeyDown(Keys.W);
  800.                }
  801.            }
  802.        }
  803.  
  804.        public bool LeftDown
  805.        {
  806.            get
  807.            {
  808.                if (Game1.gamestate == Game1.GameStates.Menu)
  809.                {
  810.                    return keyboardState.IsKeyDown(Keys.S) && lastState.IsKeyUp(Keys.S);
  811.                }
  812.                else
  813.                {
  814.                    return keyboardState.IsKeyDown(Keys.S);
  815.                }
  816.            }
  817.        }
  818.  
  819.        public bool SpaceDown
  820.        {
  821.            get
  822.            {
  823.                if (Game1.gamestate == Game1.GameStates.Menu)
  824.                {
  825.                    return keyboardState.IsKeyDown(Keys.Space) && lastState.IsKeyUp(Keys.Space);
  826.                }
  827.                else
  828.                {
  829.                    return keyboardState.IsKeyDown(Keys.Space);
  830.                                    
  831.                }
  832.               }
  833.        }
  834.  
  835.  
  836.  
  837.        public bool MenuSelect
  838.        {
  839.            get
  840.            {
  841.                return keyboardState.IsKeyDown(Keys.Enter) && lastState.IsKeyUp(Keys.Enter);
  842.            }
  843.        }
  844.  
  845.       public bool exitKeyPressed
  846.        {
  847.           get
  848.           {
  849.               return keyboardState.IsKeyDown(Keys.Escape);
  850.           }
  851.        }
  852.  
  853.    }
  854. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement