Click here to Skip to main content
15,892,537 members
Articles / Mobile Apps / Windows Phone 7

BounceBall - XNA Farseer Magic

Rate me:
Please Sign up or sign in to vote.
4.97/5 (72 votes)
12 Apr 2011CPOL62 min read 194.6K   19K   119  
In this article we are going to develop a game using Farseer Physics Engine and XNA for Windows Phone 7. This article provides you base for your games to make game development easy and fast.

namespace FarseerXNADemo3.Screens
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using FarseerXNABase.ScreenSystem;
using FarseerXNABase.Controls;

    public class StartScreen : GameScreen
    {
        #region Properties & Variables

        private ContentManager _content;
        private Rectangle _viewport;

        private Texture2D _background, _instructionsNormal, _instructionsClicked, _highScoreNormal, _highScoreClicked;
        private PanelControl pnlMenu;

        #endregion

        #region Initialization and Load

        public StartScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            EnabledGestures = Microsoft.Xna.Framework.Input.Touch.GestureType.Tap;
        }

        public override void LoadContent()
        {
            if (_content == null)
                _content = new ContentManager(ScreenManager.Game.Services, "Content");

            _background = ScreenManager.ContentManager.Load<Texture2D>("texture");
            _instructionsNormal = ScreenManager.ContentManager.Load<Texture2D>("instructions_off");
            _instructionsClicked = ScreenManager.ContentManager.Load<Texture2D>("instructions_on");
            _highScoreNormal = ScreenManager.ContentManager.Load<Texture2D>("highscore_off");
            _highScoreClicked = ScreenManager.ContentManager.Load<Texture2D>("highscore_on");

            UpdateScreen();
            ScreenManager.Camera.ProjectionUpdated += UpdateScreen;

            CreateMenu();

            base.LoadContent();
        }
        public override void UnloadContent()
        {
            _content.Unload();
        }

        private void UpdateScreen()
        {
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            _viewport = new Rectangle(0, 0, viewport.Width, viewport.Height);
        }

        #endregion

        #region Game Methods

        public override void HandleInput(InputHelper input)
        {
            pnlMenu.HandleInput(input);
            base.HandleInput(input);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            //Update Menu states
            pnlMenu.Update(gameTime);

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();
            ScreenManager.SpriteBatch.Draw(_background, _viewport, Color.White);
            ScreenManager.SpriteBatch.End();

            Control.BatchDraw(pnlMenu, ScreenManager.GraphicsDevice, ScreenManager.SpriteBatch, Vector2.Zero, gameTime);
        }
        #endregion

        #region Menu

        private void CreateMenu()
        {
            //Initialize MenuPanel
            pnlMenu = new PanelControl();
            pnlMenu.Position = new Vector2(_viewport.Width / 2 - 160, _viewport.Height/2 - 100);

            //Add MenuItems
            //Instructions
            Button miInstructions = new Button(ScreenManager.Game)
            {
                Width = 320,
                Height = 100,
                NormalButtonTexture = _instructionsNormal,
                ClickedButtonTexture = _instructionsClicked,
                Position = new Vector2(0, 0)
            };

            //Highscore
            Button miHighScore = new Button(ScreenManager.Game)
            {
                Width = 320,
                Height = 100,
                NormalButtonTexture = _highScoreNormal,
                ClickedButtonTexture = _highScoreClicked,
                Position = new Vector2(0, 100)
            };

            //Event Handlers
            miInstructions.OnClicked += new Button.ClickHandler(miInstructions_OnClicked);
            miHighScore.OnClicked += new Button.ClickHandler(miHighScore_OnClicked);

            //Add MenuItems to Menupanel
            pnlMenu.AddChild(miInstructions);
            pnlMenu.AddChild(miHighScore);
        }

       
        void miInstructions_OnClicked(Button sender)
        {
            ScreenManager.AddScreen(new InstructionsScreen(), null);
        }
        void miHighScore_OnClicked(Button sender)
        {
           ScreenManager.AddScreen(new HighScoreScreen(), null);
        }

        #endregion
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Veloxcore
India India
Love Programming... and games. You can find latest about me over http://www.veloxcore.com/

Comments and Discussions