5 Replies - 1427 Views - Last Post: 12 March 2012 - 08:09 AM Rate Topic: -----

#1 MacNeil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-March 12

How can I run a game from a different class?

Posted 11 March 2012 - 12:55 PM

Hi, I'm using the Farseer engine and the entry point to the program is in a class called program below
namespace FarseerPhysics.HelloWorld
{
#if WINDOWS || XBOX
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            using (Sum game = new Sum())
            {
                game.Run();
            }
        }
    }
#endif
}


When this executes, an instance of the class called Sum starts which contains my code for the main menu.

What I would like is to have code which would run the game when the start button is pressed as shown below

if (startScreen.SelectedIndex == 0)
                {
                    //I need my code instruction here
                }


Basically the code I need has to instantiate an instance of the class which contains the code that runs the game.

I have tried doing the following method in the Sum class with no success

Declare the class which contains the game code

StateManagement gameScreen1;
gameScreen1 = new StateManagement();


Then I've tried to run gameScreen1 when start button is pressed

 if (CheckKey(Keys.Enter))
            {
                if (startScreen.SelectedIndex == 0)
                {
                    gameScreen1.Run();
                }


The code will not run, however, when I declare the gamescreen1 and run it in the entry point to the program the game works fine.

namespace FarseerPhysics.HelloWorld
{
#if WINDOWS || XBOX
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            using (StateManagement gameScreen1 = new StateManagement())
            {
                gameScreen1.Run();
            }
        }
    }
#endif
}


I have no idea why this is happening, surely it should run in the other class fine??!

Also I used SixOfEleven's game menu tutorial to create my menu system, http://www.dreaminco...ith-xna-part-2/

Is This A Good Question/Topic? 0
  • +

Replies To: How can I run a game from a different class?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: How can I run a game from a different class?

Posted 11 March 2012 - 02:19 PM

Since this is specific to C# and not general (non-language-specific) game theory, in the future please post things like this in the C# section.

So you're just running another application from your menu?
MSDN page for Process.Start

Otherwise, if you're really talking about class to class communication:

See FAQ # 3. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2


FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 MacNeil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-March 12

Re: How can I run a game from a different class?

Posted 12 March 2012 - 05:52 AM

Hey tlhIn`toq, sorry I thought game development was the best place to post this but I guess it is more of a problem with the C# code. My bad.

I'm not trying to run another application, basically the code for the game play is contained in one class and I require a way to instantiate the class so that the player can play the game after they press the start button. Having a bit of a nightmare with it to be honest!
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: How can I run a game from a different class?

Posted 12 March 2012 - 06:38 AM

What's odd to me is when you say the game is contained in another class.
Any game I've ever dealt with contains several classes; not just one.

If it's just one, then it shouldn't be a problem to make a new instance of the class and call whatever is your .Start() method. Pretty much what you have in lines 11-13 of your top code block.

If what you have in the first blocks works... I don't understand why you shifted direction into this StateManagement stuff later on.
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,175
  • Joined: 14-February 08

Re: How can I run a game from a different class?

Posted 12 March 2012 - 07:40 AM

*Moved to C#*
Was This Post Helpful? 0
  • +
  • -

#6 MacNeil88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-March 12

Re: How can I run a game from a different class?

Posted 12 March 2012 - 08:09 AM

Basically because I want the player to begin at the main menu I choose to run the main menu class in the entry point to the program. Then when the player triggers the game to start I need to run the code for the game, which I've shown below if that makes things easier

using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using FarseerPhysics.DebugViews;
using FarseerPhysics.HelloWorld.Physics;
using System.Collections.Generic;
using FarseerPhysics.HelloWorld.GameObjects;
using TiledLib;
using System;

namespace FarseerPhysics.HelloWorld
{
    /// <summary>
    /// This is the main class for our game, most of the work ealier on in the project will
    /// be done in this class but we should move some of the work into level classes.
    /// </summary>
    public class StateManagement : Game
    {
        //Graphics
        private GraphicsDeviceManager graphics;
        //Sprite Batch- used to draw texture2D to the screen
        private SpriteBatch batch;
        //Holds the previous input state
        private KeyboardState oldKeyState;
        private GamePadState oldPadState;
        //For drawing text to the screen, mainly debug info
        private SpriteFont gameFont;
        public float playerSpeed;
        public float jumpHeight;
        public int jumpTimes;
        public float gravity;
        private int count = 0;
        private int count2 = 0;
        public float friction;
        public float bounce;
        public float velocity;
        public float platformSpeed;

        public Body b;
        public int xindex;
        public int yindex;
        public int x;
        public int y;
        private float xpos;
        private float ypos;
        private bool onPlatform;
        private Vector2 distanceToPlatform;
        private Body collidedPlatform;

        //MovingPlatforms mp;



        //World, this is the physics simulation, we will use this to add
        //remove objects from the physics
        private World world;

        // Simple camera controls
        private Matrix view;
        private Vector2 cameraPosition;
        private Vector2 screenCenter;


        //Physics debug overlay, shows what has been added to the simulation
        private DebugViewXNA debugPhysicsView;
        private Matrix debugProjection;
        private Matrix debugView;

        //Holds every game sprite on the screen, this has been loaded by us and
        //not from the map
        public List<GameSprite> displayList;
        public List<GameSprite> movingPlatforms;

        //The Sprite we are controlling, is there a better way to control something
        public GameSprite playerSprite;
        public GameSprite platformHorizontal;



        //The Tiled Map we are using at the moment
        Map gameMap;


        //If we are not on the XBOX360 platform
#if !XBOX360
        //Some instruction text to show
        const string Text = "Press A or D to rotate the ball\n" +
                            "Press Space to jump\n" +
                            "Press Shift + W/S/A/D to move the camera" +
                            "Press F1 to enable/disable physics debug";
#else
                const string Text = "Use left stick to move\n" +
                                    "Use right stick to move camera\n" +
                                    "Press A to jump\n"+
                                    "Press Back to enable/disable physics debug";
#endif
        /// <summary>
        /// 
        /// </summary>
        public void VariableChanger()
        {
            string[] lines = System.IO.File.ReadAllLines("Content/TextEditor.txt");

            foreach (string line in lines)
            {
                string[] chart = line.Split('=');
                if (chart[0].ToLower() == "playerspeed")
                {
                    playerSpeed = float.Parse(chart[1]);
                }
                if (chart[0] == "platformSpeed")
                {
                    platformSpeed = float.Parse(chart[1]);
                }
                if (chart[0] == "jumpHeight")
                {
                    jumpHeight = float.Parse(chart[1]);
                }
                if (chart[0] == "jumpTimes")
                {
                    jumpTimes = int.Parse(chart[1]);
                }
                if (chart[0] == "gravity")
                {
                    gravity = float.Parse(chart[1]);
                }
                if (chart[0] == "friction")
                {
                    friction = float.Parse(chart[1]);
                }
                if (chart[0] == "bounce")
                {
                    bounce = float.Parse(chart[1]);
                }
                if (chart[0] == "velocity")
                {
                    velocity = float.Parse(chart[1]);
                }
            }
        }




        public StateManagement()
        {
            VariableChanger();
            //Initialise the graphics system
            graphics = new GraphicsDeviceManager(this);
            //Width and Height of the screen
            graphics.PreferredBackBufferWidth = 1200;
            graphics.PreferredBackBufferHeight = 880;

            //The root directory for all assets
            Content.RootDirectory = "Content";
            //This holds sprites that are loaded by us and not from the tiled Map
            displayList = new List<GameSprite>();
            movingPlatforms = new List<GameSprite>();
            //Create Physics Simulation with Gavity of 9.8. We should load this
            //value from a text file
            world = new World(new Vector2(0, gravity));

        }


        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content. We might change this later on when we
        /// need to load a level at a time
        /// </summary>
        protected override void LoadContent()
        {
            VariableChanger();
            // Initialize camera controls
            view = Matrix.Identity;
            cameraPosition = Vector2.Zero;

            //Calculate screen centre
            screenCenter = new Vector2(graphics.GraphicsDevice.Viewport.Width / 2f,
                                                graphics.GraphicsDevice.Viewport.Height / 2f);

            //Create Sprite batch for drawing our sprites later
            batch = new SpriteBatch(GraphicsDevice);
            //load font from drawing text
            gameFont = Content.Load<SpriteFont>("font");


            //Create our debug physics view
            debugPhysicsView = new DebugViewXNA(world);
            debugPhysicsView.Enabled = true;
            debugPhysicsView.LoadContent(GraphicsDevice, Content);
            debugPhysicsView.DebugPanelPosition = new Vector2(600.0f, 0.0f);
            debugPhysicsView.AppendFlags(DebugViewFlags.Shape);
            debugPhysicsView.AppendFlags(DebugViewFlags.Joint);
            debugPhysicsView.AppendFlags(DebugViewFlags.DebugPanel);
            debugPhysicsView.AppendFlags(DebugViewFlags.ContactPoints);
            debugPhysicsView.AppendFlags(DebugViewFlags.AABB);
            debugPhysicsView.AppendFlags(DebugViewFlags.PolygonPoints);
            debugPhysicsView.AppendFlags(DebugViewFlags.CenterOfMass);

            //Load our game map, we should probably load the level name(map) from 
            //a text file
            gameMap = Content.Load<Map>("Test Map");

            //we have loaded the map, we need to add Physics Objects
            //to represent this. We have decided that layer called Game
            //will have physics
            TileLayer tileLayer = gameMap.GetLayer("Main Level Layer") as TileLayer;

            int xindex = 0;
            int yindex = 0;
            //Start position of tiles
            float xPos = 0f;
            float yPos = 0f;
            //Nested for loop to go through every tile in the layer
            for (int y = 0; y < tileLayer.Height; y++)
            {
                for (int x = 0; x < tileLayer.Width; x++)
                {
                    //if we have a tile at position x, y
                    if (tileLayer.Tiles[x, y] != null)
                    {
                        //Retrieve that tile
                        Tile currentTile = tileLayer.Tiles[x, y];
                        //Grab the propery called tile type from the tile, this was
                        //created in Tiled editor
                        int tileType = (int)currentTile.Properties["TileType"];



                        //Retrieve the size of the tile
                        Vector2 size = new Vector2(currentTile.Source.Width,
                            currentTile.Source.Height);

                        //Get the position
                        Vector2 pos = new Vector2(xPos, yPos);
                        //We need to offset the position to the center of the tile
                        pos += size / 2;
                        //create the body for the tile
                        Body b = BodyFactory.CreateRectangle(world, PhysicsUtils.ConvertToPhysicsUnits(size.X),
                            PhysicsUtils.ConvertToPhysicsUnits(size.Y), 1f, PhysicsUtils.ConvertToPhysicsUnits(pos));


                        //Make the body static, pinned in place
                        b.BodyType = BodyType.Static;
                        //set the user type of the body, this allows us to associate some kind of data with
                        //the body and 
                        b.UserData = tileType;
                        if (tileType == 3)
                        {
                            platformHorizontal = new GameSprite();
                            movingPlatforms.Add(platformHorizontal);

                            //Load the texture and asign it to the texture property of the the new sprite
                            platformHorizontal.Texture = platformHorizontal.Texture = Content.Load<Texture2D>("Test Sprite"); ; //  40 x 40 - 1m x 1m

                            //Convert screen center from pixels to meters, we could grab this position
                            //from a tile
                            platformHorizontal.Position = PhysicsUtils.ConvertToPhysicsUnits(screenCenter) + new Vector2(-1.5f, -1.5f);

                            //Create the RigidBody of the player, we could change this to some other kind
                            //rigid body or a combination of rigid bodies
                            platformHorizontal.RigidBody = b;
                            xpos = b.Position.X;
                            ypos = b.Position.Y;
                            //Under simulation control
                            platformHorizontal.RigidBody.BodyType = BodyType.Dynamic;
                            platformHorizontal.RigidBody.IgnoreGravity = true;
                            platformHorizontal.RigidBody.FixedRotation = true;
                            platformHorizontal.RigidBody.Mass = 1000;
                            //movingPlatform.RigidBody.LinearVelocity = new Vector2(-2.0f, 0.0f);
                            //platformHorizontal.RigidBody.Position = new Vector2(platformHorizontal.RigidBody.Position.X - platformSpeed, platformHorizontal.RigidBody.Position.Y);
                            //sprite.RigidBody.LinearVelocity = new Vector2(0,0);

                            //// Give it some bounce and friction
                            platformHorizontal.RigidBody.OnCollision += new OnCollisionEventHandler(platform_OnCollision);
                            //Register a collision handler for the moving sprite
                            //movingPlatfrom.RigidBody.OnCollision += new OnCollisionEventHandler(RigidBody_OnCollision);
                            // sprite.RigidBody.FixedRotation = true;

                            //Add it to the display list
                            displayList.Add(platformHorizontal);
                            xindex = x;
                            yindex = y;
                        }

                    }

                    //Move the new position to the next tile position on the x
                    xPos += PhysicsUtils.MeterInPixels;

                }
                //reset back to the starting x position
                xPos = 0f;
                //Move the new position to the next tile position on the y
                yPos += PhysicsUtils.MeterInPixels;
            }
            tileLayer.Tiles[xindex, yindex] = null;



            //Create Game Sprite
            GameSprite sprite = new GameSprite();

            //Load the texture and asign it to the texture property of the the new sprite
            sprite.Texture = Content.Load<Texture2D>("Test Sprite"); //  40 x 40 - 1m x 1m

            //Convert screen center from pixels to meters, we could grab this position
            //from a tile
            sprite.Position = PhysicsUtils.ConvertToPhysicsUnits(screenCenter) + new Vector2(-1.5f, -1.5f);

            //Create the RigidBody of the player, we could change this to some other kind
            //rigid body or a combination of rigid bodies
            sprite.RigidBody = BodyFactory.CreateCircle(world, PhysicsUtils.MeterInPixels
                / PhysicsUtils.ConvertToGraphicsUnits(2f),
                1f, sprite.Position);
            //Under simulation control
            sprite.RigidBody.BodyType = BodyType.Dynamic;
            //sprite.RigidBody.LinearVelocity = new Vector2(0,0);

            //// Give it some bounce and friction
            sprite.RigidBody.Restitution = bounce;
            sprite.RigidBody.Friction = friction;
            //Register a collision handler for the moving sprite
            sprite.RigidBody.OnCollision += new OnCollisionEventHandler(RigidBody_OnCollision);
            // sprite.RigidBody.FixedRotation = true;

            //Add it to the display list
            displayList.Add(sprite);
            ////this is the sprite we are controlling
            playerSprite = sprite;





        }

        public void movePlatform()
        {
            xpos -= platformSpeed;
            platformHorizontal.RigidBody.Position = new Vector2(xpos, ypos);
            //movingPlatform.RigidBody.ApplyLinearImpulse(new Vector2(-1000.0f, 0.0f));
            //movingPlatform.RigidBody.Position = new Vector2(movingPlatform.RigidBody.Position.X - 0.02f, movingPlatform.RigidBody.Position.Y);
            /*count++;
            if (count > 0 && count < 120)
            {
                movingPlatform.RigidBody.Position = new Vector2(movingPlatform.RigidBody.Position.X - 0.02f, movingPlatform.RigidBody.Position.Y);
            }

            count++;
            if (count > 120 && count < 241)
            {
                movingPlatform.RigidBody.Position = new Vector2(movingPlatform.RigidBody.Position.X + 0.02f, movingPlatform.RigidBody.Position.Y);
            }

            if (count > 241)
            {
                count = 0;
            }*/

            //movingPlatform.BodyType = BodyType.Dynamic;
            //movingPlatform.IgnoreGravity = true;
            //movingPlatform.FixedRotation = true;
            //movingPlatform.ApplyForce(new Vector2(-5f,0f));




        }





        /// <summary>
        /// The Collision handler
        /// </summary>
        /// <param name="fixtureA">The fixture(contains body) which is on the left side of +=</param>
        /// <param name="fixtureB">The fixture(contains body) which has been collided by the above</param>
        /// <param name="contact">Extra contact information</param>
        /// <returns></returns>
        bool RigidBody_OnCollision(Fixture fixtureA, Fixture fixtureB, Dynamics.Contacts.Contact contact)
        {
            //Cast the user data from fixture B's body and check it
            if ((int)fixtureB.Body.UserData == 4)
            {
                this.Exit();


            }

            if ((int)fixtureB.Body.UserData == 3)
            {

                collidedPlatform = fixtureB.Body;
                distanceToPlatform = fixtureB.Body.Position - fixtureA.Body.Position;
                if (playerSprite.RigidBody.Position.Y < collidedPlatform.Position.Y)
                {
                    onPlatform = true;
                }


            }

            if ((int)fixtureB.Body.UserData == 2)
            {
                count2 = 10;
            }

            else
            {
                count2 = 0;
            }

            return true;
        }

        bool platform_OnCollision(Fixture fixtureA, Fixture fixtureB, Dynamics.Contacts.Contact contact)
        {
            if (fixtureB.Body != playerSprite.RigidBody && fixtureB.Body != platformHorizontal.RigidBody)
            {

                platformSpeed *= -1;
                //float xvel = movingPlatform.RigidBody.LinearVelocity.X;
                //xvel *= -1;
                //movingPlatform.RigidBody.Position = new Vector2(xvel, 0.0f);
                return true;
            }
            else
            {
                return false;
            }

        }


        /// <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)
        {
            //Call to deal with game pad controls
            HandleGamePad();
            //Call to deal with keyboard controls
            HandleKeyboard();
            movePlatform();

            if (onPlatform == true)
            {
                playerSprite.RigidBody.Position = collidedPlatform.Position - distanceToPlatform;
                playerSprite.RigidBody.BodyType = BodyType.Static;
            }

            else
            {
                playerSprite.RigidBody.BodyType = BodyType.Dynamic;
            }


            //Update physics first, never lest than 30 FPS
            world.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalSeconds, (1f / 30f)));

            base.Update(gameTime);

        }

        /// <summary>
        /// This is used to update the joypad state
        /// </summary>
        private void HandleGamePad()
        {
            //Get the state of the joypad
            GamePadState padState = GamePad.GetState(0);

            //If it is connected
            if (padState.IsConnected)
            {
                //Had the start button been pressed?
                if (padState.Buttons.Start == ButtonState.Pressed)
                    Exit();

                //Check to see if the A Button has been pressed, apply an impulse
                //this 'jumps' the player
                if (padState.Buttons.A == ButtonState.Pressed && oldPadState.Buttons.A == ButtonState.Released)
                    playerSprite.RigidBody.ApplyLinearImpulse(new Vector2(0, -jumpHeight));

                //Has the back button been pressed, toggle debug view
                if (padState.Buttons.Back == ButtonState.Pressed)
                    debugPhysicsView.Enabled = !debugPhysicsView.Enabled;

                //Apply a force to move the object on the left thumbstick
                playerSprite.RigidBody.ApplyForce(padState.ThumbSticks.Left);
                //Move the camera position with the right thumb stick
                cameraPosition.X -= padState.ThumbSticks.Right.X;
                cameraPosition.Y += padState.ThumbSticks.Right.Y;

                //Recalculate the view matrix of the camera
                view = Matrix.CreateTranslation(new Vector3(cameraPosition - screenCenter, 0f)) * Matrix.CreateTranslation(new Vector3(screenCenter, 0f));

                //Store the last state
                oldPadState = padState;
            }
        }

        /// <summary>
        /// This method is used to retrieve the state of the keyboard
        /// </summary>
        private void HandleKeyboard()
        {
            //Get the current keyboard state
            KeyboardState state = Keyboard.GetState();

            // Switch between player and camera control
            if (state.IsKeyDown(Keys.LeftShift) || state.IsKeyDown(Keys.RightShift))
            {
                // Move camera
                if (state.IsKeyDown(Keys.A))
                    cameraPosition.X += 1.5f;

                if (state.IsKeyDown(Keys.D))
                    cameraPosition.X -= 1.5f;

                if (state.IsKeyDown(Keys.W))
                    cameraPosition.Y += 1.5f;

                if (state.IsKeyDown(Keys.S))
                    cameraPosition.Y -= 1.5f;

                view = Matrix.CreateTranslation(new Vector3(cameraPosition - screenCenter, 0f)) *
                        Matrix.CreateTranslation(new Vector3(screenCenter, 0f));
            }
            else
            {
                // We make it possible to rotate the circle body
                if (state.IsKeyDown(Keys.A))
                {
                    //onPlatform = false;
                    playerSprite.RigidBody.ApplyLinearImpulse(new Vector2(-playerSpeed, 0f));

                    if (onPlatform == true)
                    {
                        onPlatform = false;
                    }
                }


                if (state.IsKeyDown(Keys.D))
                {

                    playerSprite.RigidBody.ApplyLinearImpulse(new Vector2(playerSpeed, 0f));

                    if (onPlatform == true)
                    {
                        onPlatform = false;
                    }

                }




                if (state.IsKeyDown(Keys.Space) && oldKeyState.IsKeyUp(Keys.Space))
                {
                    //onPlatform = false;
                    if (count2 < jumpTimes)
                    {
                        playerSprite.RigidBody.LinearVelocity = new Vector2(playerSprite.RigidBody.LinearVelocity.X, 0f);
                        playerSprite.RigidBody.ApplyForce(new Vector2(0, -jumpHeight * 75));
                        count2++;

                        if (onPlatform == true)
                        {
                            onPlatform = false;
                        }

                    }
                }

                if (!(state.IsKeyDown(Keys.A)) && !(state.IsKeyDown(Keys.D)))
                {
                    playerSprite.RigidBody.LinearVelocity = new Vector2(playerSprite.RigidBody.LinearVelocity.X / 2 * friction, playerSprite.RigidBody.LinearVelocity.Y);
                }

                if (state.IsKeyDown(Keys.F1) && oldKeyState.IsKeyUp(Keys.F1))
                    debugPhysicsView.Enabled = !debugPhysicsView.Enabled;

                if (playerSprite.RigidBody.LinearVelocity.X > playerSpeed)
                {
                    playerSprite.RigidBody.LinearVelocity = new Vector2(playerSpeed, playerSprite.RigidBody.LinearVelocity.Y);
                }
                if (playerSprite.RigidBody.LinearVelocity.X < -playerSpeed)
                {
                    playerSprite.RigidBody.LinearVelocity = new Vector2(-playerSpeed, playerSprite.RigidBody.LinearVelocity.Y);
                }


            }

            if (state.IsKeyDown(Keys.Escape))
                Exit();

            oldKeyState = state;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            //Clear the screen
            GraphicsDevice.Clear(Color.CornflowerBlue);

            //Begin a sprite batch
            batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, view);

            //draw map
            gameMap.Draw(batch);

            //Cycle through game sprites
            foreach (GameSprite s in displayList)
            {
                // Convert physics position (meters) to screen coordinates (pixels)
                Vector2 pos = PhysicsUtils.ConvertToGraphicsUnits(s.RigidBody.Position);
                Vector2 origin = new Vector2(s.Texture.Width / 2f, s.Texture.Height / 2f);
                float rotation = 0;

                //Draw sprite
                batch.Draw(s.Texture, pos, null, Color.White, rotation, origin, 1f, SpriteEffects.None, 0f);
            }

            batch.End();

            //Draw debug display
            debugView = Matrix.CreateTranslation(new Vector3(PhysicsUtils.ConvertToPhysicsUnits(cameraPosition - screenCenter), 0f)) *
                        Matrix.CreateTranslation(new Vector3(PhysicsUtils.ConvertToPhysicsUnits(screenCenter), 0f)); ;
            debugProjection = Matrix.CreateOrthographicOffCenter(0, PhysicsUtils.ConvertToPhysicsUnits(GraphicsDevice.Viewport.Width),
                PhysicsUtils.ConvertToPhysicsUnits(GraphicsDevice.Viewport.Height), 0, 0, 1f);
            debugPhysicsView.RenderDebugData(ref debugProjection, ref debugView);


            //Begin Sprite batch
            batch.Begin();

            // Display instructions
            batch.DrawString(gameFont, Text, new Vector2(14f, 14f), Color.Black);
            batch.DrawString(gameFont, Text, new Vector2(12f, 12f), Color.White);

            batch.End();

            base.Draw(gameTime);
        }
    }
}


I might as well put the code that handles the main menu here as well

using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using FarseerPhysics.DebugViews;
using FarseerPhysics.HelloWorld.Physics;
using System.Collections.Generic;
using FarseerPhysics.HelloWorld.GameObjects;
using TiledLib;
using System;

namespace FarseerPhysics.HelloWorld
{
    /// <summary>
    /// This is the main class for our game, most of the work ealier on in the project will
    /// be done in this class but we should move some of the work into level classes.
    /// </summary>
    public class Sum : Game
    {
        //Graphics
        private GraphicsDeviceManager graphics;
        //Sprite Batch- used to draw texture2D to the screen
        private SpriteBatch spriteBatch;
        //Holds the previous input state
        private KeyboardState oldKeyState;
        private GamePadState oldPadState;
 
        MenuComponent menuComponent;

        KeyboardState keyboardState;
        KeyboardState oldKeyboardState;

        GameScreen activeScreen;
        StartScreen startScreen;
        ActionScreen actionScreen;
        OptionsScreen optionsScreen;
        PopUpScreen quitScreen;
        StateManagement gameScreen1;

        public bool game = false;
        public bool gameEnds = false;
        public bool playerDead = false;
        public bool gameStart = false;



        public Sum()
        {
            
            graphics = new GraphicsDeviceManager(this);
           
            graphics.PreferredBackBufferWidth = 1200;
            graphics.PreferredBackBufferHeight = 880;

           
            Content.RootDirectory = "Content";
          
        }

        public void LoadGame()
        { }

        /// </summary>
        protected override void LoadContent()
        {
            //string[] menuItems = { "Start", "Options", "Quit"};

            spriteBatch = new SpriteBatch(GraphicsDevice);

            startScreen = new StartScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"), Content.Load<Texture2D>("image1"));
            Components.Add(startScreen);
            startScreen.Hide();


            actionScreen = new ActionScreen(this, spriteBatch, Content.Load<Texture2D>("image4"));
            Components.Add(actionScreen);
            actionScreen.Hide();

            optionsScreen = new OptionsScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"), Content.Load<Texture2D>("image2"));
            Components.Add(optionsScreen);
            optionsScreen.Hide();

            quitScreen = new PopUpScreen(this, spriteBatch, Content.Load<SpriteFont>("menufont"), Content.Load<Texture2D>("image3"));
            Components.Add(quitScreen);
            quitScreen.Hide();

            gameScreen1 = new StateManagement();



            activeScreen = startScreen;
            activeScreen.Show();

            //menuComponent = new MenuComponent(this, spriteBatch, Content.Load<SpriteFont>("menufont"), menuItems);
            //Components.Add(menuComponent);

        }


        public void movePlatform()
        {
           
        }


      

        /// <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)
        {
            keyboardState = Keyboard.GetState();

            if (activeScreen == startScreen)
            {
                HandleStartScreen();
            }

            if (activeScreen == optionsScreen)
            {
                HandleOptionsScreen();
            }

            if (activeScreen == quitScreen)
            {
                HandleQuitScreen();
            }
            if (activeScreen == actionScreen)
            {
                HandleActionScreen();
            }
            if (game == true)
            {
                if (CheckKey(Keys.Escape))
                {
                    activeScreen.Enabled = false;
                    activeScreen = quitScreen;
                    activeScreen.Show();
                }
              
                movePlatform();
            }

            if (gameEnds == true)
            {
                game = false;
                activeScreen = actionScreen;
                activeScreen.Show();
            }

            base.Update(gameTime);
            oldKeyboardState = keyboardState;
            //HandleKeyboard();
        }
        private void HandleActionScreen()
        {
            if (CheckKey(Keys.Enter))
            {
                actionScreen.Hide();
                activeScreen = startScreen;
                startScreen.Show();
                gameEnds = false;
            }
        }

        private void HandleStartScreen()
        {
            if (CheckKey(Keys.Enter))
            {
                if (startScreen.SelectedIndex == 0)
                {
                    activeScreen.Hide();
                    gameScreen1.Run();
                }
                if (startScreen.SelectedIndex == 1)
                {
                    activeScreen = optionsScreen;
                    startScreen.Hide();
                    optionsScreen.Show();
                }
                if (startScreen.SelectedIndex == 2)
                {
                    this.Exit();
                }
            }
        }

        private void HandleOptionsScreen()
        {
            if (CheckKey(Keys.Enter))
            {
                if (optionsScreen.SelectedIndex == 0)
                {

                }
                if (optionsScreen.SelectedIndex == 1)
                {

                }
                if (optionsScreen.SelectedIndex == 2)
                {
                    optionsScreen.Hide();
                    startScreen.Show();
                    activeScreen = startScreen;
                    //selected index has to be changed to allow user to change between menus
                    optionsScreen.SelectedIndex = 1;
                }
            }
        }

        private void HandleQuitScreen()
        {
            if (CheckKey(Keys.Enter))
            {
                if (quitScreen.SelectedIndex == 0)
                {
                    activeScreen.Hide();
                    activeScreen = startScreen;
                    activeScreen.Show();
                    game = false;
                }
                if (quitScreen.SelectedIndex == 1)
                {
                    activeScreen.Hide();
                }
            }
        }

        private bool CheckKey(Keys theKey)
        {
            return keyboardState.IsKeyUp(theKey) && oldKeyboardState.IsKeyDown(theKey);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {

            //Clear the screen
            GraphicsDevice.Clear(Color.CornflowerBlue);

            //Begin a sprite batch
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null);
            spriteBatch.End();

            base.Draw(gameTime);


            base.Draw(gameTime);
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1