Beispiel #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            KeyboardState Keystate          = Keyboard.GetState();
            MouseState    CurrentMouseState = Mouse.GetState();
            MouseState    PrevMouseState    = CurrentMouseState;

            switch (CurrentGameState)
            {
            case Gamestate.MainMenu:
                m_Lives1.Reset();
                m_Lives2.Reset();
                Back.IsClicked = false;
                if (PlayButton.IsClicked == true && PrevMouseState.LeftButton == ButtonState.Released)
                {
                    CurrentGameState = Gamestate.Playing;
                }
                PlayButton.Update(CurrentMouseState);
                //if (Options.IsClicked == true && PrevMouseState.LeftButton == ButtonState.Released) { CurrentGameState = Gamestate.Options; }
                //Options.Update(CurrentMouseState);
                break;

            /*case Gamestate.Options:
             *  if (Full.IsClicked == true && PrevMouseState.LeftButton == ButtonState.Released && CurrentMouseState.LeftButton == ButtonState.Released)
             *  {
             *      graphics.ToggleFullScreen();
             *      Full.IsClicked = false;
             *  }
             *  Full.Update(CurrentMouseState);
             *  if (Back.IsClicked == true && PrevMouseState.LeftButton == ButtonState.Released)
             *      CurrentGameState = Gamestate.MainMenu;
             *  Back.Update(CurrentMouseState);
             *  break;*/
            case Gamestate.Playing:
                if (Keyboard.GetState().IsKeyDown(Keys.S))
                {
                    m_Bar1.SetVel(m_Bar1.GetMaxVel());
                }
                if (Keyboard.GetState().IsKeyDown(Keys.W))
                {
                    m_Bar1.SetVel(-m_Bar1.GetMaxVel());
                }
                if (Keystate.IsKeyDown(Keys.Down))
                {
                    m_Bar2.SetVel(m_Bar2.GetMaxVel());
                }
                if (Keystate.IsKeyDown(Keys.Up))
                {
                    m_Bar2.SetVel(-m_Bar2.GetMaxVel());
                }
                if (m_Bar1.GetPosY() <= 0 && m_Bar1.GetVel() < 0 || Keyboard.GetState().IsKeyUp(Keys.S) && m_Bar1.GetVel() > 0)
                {
                    m_Bar1.SetVel(0);
                }
                if (m_Bar1.GetPosY() + m_Bar2.GetHeight() >= graphics.GraphicsDevice.Viewport.Height && m_Bar1.GetVel() > 0 || Keyboard.GetState().IsKeyUp(Keys.W) && m_Bar1.GetVel() < 0)
                {
                    m_Bar1.SetVel(0);
                }
                if (m_Bar2.GetPosY() <= 0 && m_Bar2.GetVel() < 0 || Keyboard.GetState().IsKeyUp(Keys.Down) && m_Bar2.GetVel() > 0)
                {
                    m_Bar2.SetVel(0);
                }
                if (m_Bar2.GetPosY() + m_Bar2.GetHeight() >= graphics.GraphicsDevice.Viewport.Height && m_Bar2.GetVel() > 0 || Keyboard.GetState().IsKeyUp(Keys.Up) && m_Bar2.GetVel() < 0)
                {
                    m_Bar2.SetVel(0);
                }
                if (m_Ball.GetPosX() <= m_Bar1.GetWidth())
                {
                    if (m_Ball.GetMidPos() <= m_Bar1.GetPosY() + m_Bar1.GetHeight() && m_Ball.GetMidPos() >= m_Bar1.GetPosY())
                    {
                        m_Ball.InverseVelX();
                        m_Ball.SetPosX(m_Bar1.GetWidth() + 1);
                        m_Ball.IncreaseVel();
                        float DTM = (m_Bar1.GetMiddlePos() - m_Ball.GetMidPos()) / (-m_Bar1.GetHeight() / 2);
                        m_Ball.ModVelY(DTM);
                        Ping.Play();
                    }
                    else
                    {
                        if (m_Ball.GetPosX() <= -m_Ball.GetSize())
                        {
                            m_Ball.SetPos(new Vector2((graphics.GraphicsDevice.Viewport.Width / 2), (graphics.GraphicsDevice.Viewport.Height / 2)));
                            m_Ball.SetVelX(-m_Ball.GetStartVelX());
                            m_Ball.SetMaxVelY(m_Ball.GetStartVelY());
                            m_Ball.SetVelY((float)(m_Bar1.GetMiddlePos() - (m_Ball.GetMidPos())) / ((graphics.GraphicsDevice.Viewport.Width / 2) / m_Ball.GetStartVelX()));
                            m_Lives1.RemoveOne();
                            if (m_Lives1.GetLivesInt() == 0)
                            {
                                CurrentGameState = Gamestate.GameOver;
                            }
                        }
                    }
                }
                if (m_Ball.GetPosX() + m_Ball.GetSize() >= graphics.GraphicsDevice.Viewport.Width - m_Bar2.GetWidth())
                {
                    if (m_Ball.GetMidPos() <= m_Bar2.GetPosY() + m_Bar2.GetHeight() && m_Ball.GetMidPos() >= m_Bar2.GetPosY())
                    {
                        m_Ball.InverseVelX();
                        m_Ball.SetPosX(graphics.GraphicsDevice.Viewport.Width - m_Bar2.GetWidth() - m_Ball.GetSize() - 1);
                        m_Ball.IncreaseVel();
                        float DTM = (m_Bar2.GetMiddlePos() - m_Ball.GetMidPos()) / (-m_Bar2.GetHeight() / 2);
                        m_Ball.ModVelY(DTM);
                        Pong.Play();
                    }
                    else
                    {
                        if (m_Ball.GetPosX() >= graphics.GraphicsDevice.Viewport.Width)
                        {
                            m_Ball.SetPos(new Vector2((graphics.GraphicsDevice.Viewport.Width / 2), (graphics.GraphicsDevice.Viewport.Height / 2)));
                            m_Ball.SetVelX(m_Ball.GetStartVelX());
                            m_Ball.SetMaxVelY(m_Ball.GetStartVelY());
                            m_Ball.SetVelY((float)(m_Bar2.GetMiddlePos() - (m_Ball.GetMidPos())) / ((graphics.GraphicsDevice.Viewport.Width / 2) / m_Ball.GetStartVelX()));
                            m_Lives2.RemoveOne();
                            if (m_Lives2.GetLivesInt() == 0)
                            {
                                CurrentGameState = Gamestate.GameOver;
                            }
                        }
                    }
                }

                float MovedPos1     = m_Bar1.GetPosY() + m_Bar1.GetVel() * (float)gameTime.ElapsedGameTime.TotalSeconds;
                float MovedPos2     = m_Bar2.GetPosY() + m_Bar2.GetVel() * (float)gameTime.ElapsedGameTime.TotalSeconds;
                float MovedBallPosX = m_Ball.GetPosX() + m_Ball.GetVelX() * (float)gameTime.ElapsedGameTime.TotalSeconds;
                float MovedBallPosY = m_Ball.GetPosY() + m_Ball.GetVelY() * (float)gameTime.ElapsedGameTime.TotalSeconds;

                m_Bar1.SetPos(MovedPos1);
                m_Bar2.SetPos(MovedPos2);
                m_Ball.SetPosX(MovedBallPosX);
                m_Ball.SetPosY(MovedBallPosY);

                if (m_Ball.GetPosY() + m_Ball.GetSize() >= graphics.GraphicsDevice.Viewport.Height)
                {
                    m_Ball.InverseVelY();
                    Pang2.Play();
                }
                else if (m_Ball.GetPosY() < 0 && m_Ball.GetVelY() < 0)
                {
                    m_Ball.InverseVelY();
                    Pang2.Play();
                }
                break;

            case Gamestate.GameOver:
                PlayButton.IsClicked = false;
                MouseState PrevMouseState3 = Mouse.GetState();
                if (Back.IsClicked == true && PrevMouseState3.LeftButton == ButtonState.Released)
                {
                    CurrentGameState = Gamestate.MainMenu;
                }
                Back.Update(CurrentMouseState);
                break;
            }

            base.Update(gameTime);
        }