/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here m_Bar1 = new Bar(new Vector2(0, (graphics.GraphicsDevice.Viewport.Height / 2))); m_Bar2 = new Bar(new Vector2(0, (graphics.GraphicsDevice.Viewport.Height / 2))); m_Ball = new Ball(new Vector2((graphics.GraphicsDevice.Viewport.Width / 2), (graphics.GraphicsDevice.Viewport.Height / 2)), 10, new Vector2(0, 0)); m_Lives1 = new Lives(); m_Lives2 = new Lives(); m_Bar1.MoveVertical(-(m_Bar1.GetHeight()) / 2); m_Bar2.MoveVertical(-(m_Bar2.GetHeight()) / 2); m_Ball.MoveVertical(-m_Ball.GetSize() / 2); m_Bar2.MoveHorizontal(graphics.GraphicsDevice.Viewport.Width - m_Bar2.GetWidth()); m_Ball.SetVel(new Vector2(m_Ball.GetStartVelX(), m_Ball.GetStartVelY())); Font = Content.Load <SpriteFont>("Score"); m_Ball.MoveHorizontal(-m_Ball.GetSize() / 2); //SpriteFont font = Content.Load<SpriteFont>("Score.spritefont"); m_BarShape1 = new Texture2D(graphics.GraphicsDevice, m_Bar1.GetWidth(), m_Bar1.GetHeight()); m_BarShape2 = new Texture2D(graphics.GraphicsDevice, m_Bar2.GetWidth(), m_Bar2.GetHeight()); m_BallShape = new Texture2D(graphics.GraphicsDevice, m_Ball.GetSize(), m_Ball.GetSize()); Color[] data = new Color[80 * 30]; for (int i = 0; i < data.Length; ++i) { data[i] = Color.White; } m_BarShape1.SetData(data); m_BarShape2.SetData(data); m_BallShape.SetData(data); Music = Content.Load <Song>("BeepBox-Song loop"); MediaPlayer.Play(Music); MediaPlayer.IsRepeating = true; IsMouseVisible = true; PlayButton = new Button(Content.Load <Texture2D>("Play"), graphics.GraphicsDevice); PlayButton.SetPostion(new Vector2(GraphicsDevice.Viewport.Width / 2 - GraphicsDevice.Viewport.Width / 16, GraphicsDevice.Viewport.Height / 2)); Back = new Button(Content.Load <Texture2D>("Back"), graphics.GraphicsDevice); Back.Size = new Vector2(graphics.GraphicsDevice.Viewport.Width / 9, graphics.GraphicsDevice.Viewport.Height / 18); Back.SetPostion(new Vector2(GraphicsDevice.Viewport.Width / 2 - GraphicsDevice.Viewport.Width / 16, GraphicsDevice.Viewport.Height / 2 + 100)); //Options = new Button(Content.Load<Texture2D>("Options"), graphics.GraphicsDevice); //Options.SetPostion(new Vector2(GraphicsDevice.Viewport.Width / 2 - GraphicsDevice.Viewport.Width / 16, (GraphicsDevice.Viewport.Height / 2 + 20) + 10)); //Full = new Button(Content.Load<Texture2D>("Fullscreen"), graphics.GraphicsDevice); //Full.SetPostion(new Vector2(GraphicsDevice.Viewport.Width / 2 - GraphicsDevice.Viewport.Width / 16, GraphicsDevice.Viewport.Height / 2)); Ping = Content.Load <SoundEffect>("ping"); Pong = Content.Load <SoundEffect>("pong"); Pang2 = Content.Load <SoundEffect>("pang2"); }
/// <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); }