Example #1
0
        /// <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);
        }
Example #2
0
        /// <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;
                }
            }
        }