/// <summary> /// Move slider left on keyboard left. Also handles explicit collision detection because of a bug with slider moving /// </summary> /// <param name="x">The value by which slider will move left in a single frame</param> /// <param name="ball">To detect collision with ball</param> /// <param name="brickManager">To detect collision with brick</param> /// <param name="gameFrame">Detect collision with screen edges</param> /// <param name="lifelost">Boolean variable to store if ball went below slider and a life was lost</param> /// <param name="hit">Sound when collision occurs</param> public void MoveLeft(int x, ref Ball ball, ref BricksManager brickManager, ref Rectangle gameFrame, ref bool lifelost, SoundEffectInstance hit) { //for every unit movement of the slider to the left for (int i = 0; i < x; i++) { boundingBox.X--; ball.UpdatePosition(brickManager, gameFrame, this, ref lifelost, hit, x); //note slow down factor, since UpdatePosition is called 'x' times //this was an explicit bug fix. Slider used to cross over the ball if (checkCollision(ball.getBoundingBox())) { boundingBox.X+=3; } } }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { gameState = GameState.TitleScreen; lives = 3; lifelost = false; font = Content.Load<SpriteFont>("SpriteFont"); //sounds/music fantomenk = Content.Load<SoundEffect>("FantomenK"); backgroundMusic = fantomenk.CreateInstance(); hit = Content.Load<SoundEffect>("hit"); // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); //boundingBox for collision detection with screen edges gameFrame = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height); //bricks brickTexture = Content.Load<Texture2D>("brick"); brickManager = new BricksManager(ref spriteBatch,ref brickTexture,ref graphics); brickManager.generateBricks(); //ball ballTexture = Content.Load<Texture2D>("ball"); ballPos = new Vector2( ( graphics.GraphicsDevice.Viewport.Width - ballTexture.Width) / 2, //centre of the screen graphics.GraphicsDevice.Viewport.Height * 3 / 4 - (ballTexture.Height + 10) ); ball = new Ball(ref spriteBatch, ref ballTexture, ballPos, new Vector2(0, 0), //velocity vector timeDivideFactor); //slider sliderTexture = Content.Load<Texture2D>("slider"); sliderPos = new Vector2( ( graphics.GraphicsDevice.Viewport.Width - sliderTexture.Width) / 2, //centre of the screen graphics.GraphicsDevice.Viewport.Height*3 / 4 ); slider = new Slider(ref sliderTexture,ref spriteBatch, sliderPos); }
/// <summary> /// Function to move the ball, while checking for collisions with bricks, slider and screen edges(gameFrame) /// </summary> /// <param name="brickManager">BrickManager to check collision with all bricks</param> /// <param name="gameFrame">BoundingBox of the gamescreen to detect collision with screen edges</param> /// <param name="slider">Slider to detect collission</param> /// <param name="lifelost">Boolean variable to see if the ball goes below the slider</param> /// <param name="hit">SoundEffect when the ball collides</param> /// <param name="extraTimeDivide">Used ONLY when we need to slow down movement of ball. Used in this game when the function is called by Slider</param> public void UpdatePosition(BricksManager brickManager, Rectangle gameFrame, Slider slider, ref bool lifelost, SoundEffectInstance hit, int extraTimeDivide = 1) { //check for collision with every unit of movement for X, then for Y for (int i = 0; i < Math.Abs(velocity.get().X); i++) { xMoveAheadByUnit(extraTimeDivide); if (slider.checkCollision(this.boundingBox) || brickManager.DetectCollision(this.boundingBox) || !gameFrame.Contains(this.boundingBox)) { hit.Play(); collideX(extraTimeDivide); } } for (int i = 0; i < Math.Abs(velocity.get().Y); i++) { yMoveAheadByUnit(extraTimeDivide); if (slider.checkCollision(this.boundingBox) || brickManager.DetectCollision(this.boundingBox) || !gameFrame.Contains(this.boundingBox)) { hit.Play(); collideY(extraTimeDivide); } //when ball goes below the slider if (gameFrame.Bottom - this.getPosition().Y < 50) { lifelost = true; } } }