Difference between revisions of "My Full Example of GeonBit & Classes"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Creating screens in Game1.cs)
(Creating screens in Game1.cs)
 
(8 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
         public enum GameState
 
         public enum GameState
 
         {
 
         {
 +
            Welcome,
 
             Screen1,
 
             Screen1,
 
             Screen2
 
             Screen2
 
         }
 
         }
  
         private GameState state=GameState.Screen1;
+
         private GameState state=GameState.Welcome;
  
 
         public GameState State
 
         public GameState State
Line 55: Line 56:
 
The variables will allow you to set and remember the height & width, the parentGame will be used in any subclass to allow the screen to access and change the enum we have created. I have added a methods to allow you to set the Visible & Draggable setting of the screen. I have also created a constructor which will create the actual window using the settings passed.
 
The variables will allow you to set and remember the height & width, the parentGame will be used in any subclass to allow the screen to access and change the enum we have created. I have added a methods to allow you to set the Visible & Draggable setting of the screen. I have also created a constructor which will create the actual window using the settings passed.
  
=Screen class=
+
=Individual Screen classes=
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
 
class Screen1:UIScreen
 
class Screen1:UIScreen
Line 68: Line 69:
 
             parentGame = game;
 
             parentGame = game;
  
             Header title = new Header("Screen 1");
+
             Header title = new Header("Instructions");
 
             window.AddChild(title);
 
             window.AddChild(title);
  
             Paragraph test = new Paragraph("Welcome to screen 1");
+
             Paragraph test = new Paragraph("How to play the game info");
 
             window.AddChild(test);
 
             window.AddChild(test);
  
             Button button = new Button("Click");
+
            Button button1 = new Button("Back");
 +
            window.AddChild(button1);
 +
 
 +
            button1.OnClick = (Entity btn) =>
 +
            {
 +
                //test.Text = "button clicked";
 +
                base.show(false);
 +
                parentGame.State = Game1.GameState.Welcome;
 +
                window.ClearChildren();
 +
            };
 +
        }
 +
    }
 +
 
 +
class Welcome:UIScreen
 +
    {
 +
        public Welcome(PanelSkin p, Anchor a, int w, int h) : base(p, a, w, h)
 +
        {
 +
 
 +
        }
 +
 
 +
        public void Init(Game1 game)
 +
        {
 +
            parentGame = game;
 +
 
 +
            Header title = new Header("My Game Title");
 +
            window.AddChild(title);
 +
 
 +
            Paragraph test = new Paragraph("Select the options for you game");
 +
            window.AddChild(test);
 +
 
 +
             Button button = new Button("Instructions");
 
             window.AddChild(button);
 
             window.AddChild(button);
  
 
             button.OnClick = (Entity btn) =>
 
             button.OnClick = (Entity btn) =>
 
             {
 
             {
                test.Text = "Screen 1 - button clicked";
+
            //test.Text = "button clicked";
                //base.show(false);
+
            base.show(false);
 +
                parentGame.State = Game1.GameState.Screen1;
 +
                window.ClearChildren();
 
             };
 
             };
 +
 +
            Button button1 = new Button("Play");
 +
            window.AddChild(button1);
 +
 +
            button1.OnClick = (Entity btn) =>
 +
            {
 +
                //test.Text = "button clicked";
 +
                base.show(false);
 +
                parentGame.State = Game1.GameState.Screen2;
 +
                window.ClearChildren();
 +
            };
 +
        }
 +
    }
 +
 +
class Screen2 : UIScreen
 +
    {
 +
        public Screen2(PanelSkin p, Anchor a, int w, int h) : base(p, a, w, h)
 +
        {
 +
 +
        }
 +
 +
        public void Init(Game1 game)
 +
        {
 +
            parentGame = game;
 +
 +
            Header title = new Header("Play Options");
 +
            window.AddChild(title);
 +
 +
            Paragraph test = new Paragraph("Select the options for you game");
 +
            window.AddChild(test);
  
 
             Button button1 = new Button("Close");
 
             Button button1 = new Button("Close");
Line 90: Line 153:
 
                 //test.Text = "button clicked";
 
                 //test.Text = "button clicked";
 
                 base.show(false);
 
                 base.show(false);
                 parentGame.State = Game1.GameState.Screen2;
+
                 parentGame.State = Game1.GameState.Welcome;
 
                 window.ClearChildren();
 
                 window.ClearChildren();
 
             };
 
             };
Line 101: Line 164:
 
The Init method requires a parameter for the game. This means the later button1.OnClick can access the enum to switch screens. This code creates a header, paragraph, a button to carry out an action on the paragraph, and a button to access the enum and switch screens.
 
The Init method requires a parameter for the game. This means the later button1.OnClick can access the enum to switch screens. This code creates a header, paragraph, a button to carry out an action on the paragraph, and a button to access the enum and switch screens.
  
'''Note you need to do this for each screen'''
+
'''Note you need to do this for each screen''' I have created a screen for Welcome which is the game title and buttons to instructions and play. You could also add images etc to make a better title screen. I have created a Screen1 for instructions, and a Screen2 for the play options.
  
 
=Creating screens in Game1.cs=
 
=Creating screens in Game1.cs=
Line 107: Line 170:
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
         Screen1 test;
+
        Welcome title;
         Screen2 test1;
+
         Screen1 instructions;
 +
         Screen2 playOptions;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 117: Line 181:
 
             UserInterface.Initialize(Content, BuiltinThemes.editor);
 
             UserInterface.Initialize(Content, BuiltinThemes.editor);
  
             test = new Screen1(PanelSkin.Fancy, Anchor.Center, 300, 300);
+
             title = new Welcome(PanelSkin.Fancy, Anchor.Center, 300, 300);
             test1 = new Screen2(PanelSkin.Fancy, Anchor.Center, 300, 300);
+
            instructions = new Screen1(PanelSkin.Fancy, Anchor.Center, 300, 300);
             test1.Init(this);
+
             playOptions = new Screen2(PanelSkin.Fancy, Anchor.Center, 300, 300);
             test.Init(this);
+
 
 +
            title.Init(this);
 +
             instructions.Init(this);
 +
             playOptions.Init(this);
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 134: Line 201:
 
UserInterface.Active.Draw(spriteBatch);
 
UserInterface.Active.Draw(spriteBatch);
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Finally create the SwitchScreen method, this is called when the enum is changed:
 +
 +
<syntaxhighlight lang=csharp>
 +
        public void SwitchScreen()
 +
        {
 +
            switch (State)
 +
            {
 +
                case GameState.Welcome:
 +
                    title.show(true);
 +
                    instructions.show(false);
 +
                    playOptions.show(false);
 +
                    break;
 +
                case GameState.Screen1:
 +
                    title.show(false);
 +
                    instructions.show(true);
 +
                    playOptions.show(false);
 +
                    break;
 +
                case GameState.Screen2:
 +
                    title.show(false);
 +
                    instructions.show(false);
 +
                    playOptions.show(true);
 +
                    break;
 +
            }
 +
        }
 +
</syntaxhighlight>
 +
 +
You will need a case for each possible enum value. State is the property created at the begining with the get/set method.

Latest revision as of 10:55, 30 October 2017

Game1.cs enum

I have created the following enum within the Game1 class:

        public enum GameState
        {
            Welcome,
            Screen1,
            Screen2
        }

        private GameState state=GameState.Welcome;

        public GameState State
        {
            get { return state; }
            set
            {
                state = value;
                SwitchScreen();
            }
        }

wheneever the enum is changed it will run my SwitchScreen() method. I will show this method later. Before this we will need to create the classes for our base screen and for the actual screens.

Screen base class

    class UIScreen
    {
        public Panel window;
        public int width, height;
        public Game1 parentGame;
      
        public UIScreen(PanelSkin p, Anchor a, int w, int h)
        {
            width = w;
            height = h;
            window = new Panel(new Vector2(w, h), p,a);
            UserInterface.Active.AddEntity(window);
        }

        public void show(bool b)
        {
            window.Visible = b;
        }

        public void drag(bool b)
        {
            window.Draggable = b;
        }
    }

The variables will allow you to set and remember the height & width, the parentGame will be used in any subclass to allow the screen to access and change the enum we have created. I have added a methods to allow you to set the Visible & Draggable setting of the screen. I have also created a constructor which will create the actual window using the settings passed.

Individual Screen classes

class Screen1:UIScreen
    {
        public Screen1(PanelSkin p, Anchor a, int w, int h):base(p,a,w,h)
        {
          
        }

        public void Init(Game1 game)
        {
            parentGame = game;

            Header title = new Header("Instructions");
            window.AddChild(title);

            Paragraph test = new Paragraph("How to play the game info");
            window.AddChild(test);

            Button button1 = new Button("Back");
            window.AddChild(button1);

            button1.OnClick = (Entity btn) =>
            {
                //test.Text = "button clicked";
                base.show(false);
                parentGame.State = Game1.GameState.Welcome;
                window.ClearChildren();
            };
        }
    }

class Welcome:UIScreen
    {
        public Welcome(PanelSkin p, Anchor a, int w, int h) : base(p, a, w, h)
        {

        }

        public void Init(Game1 game)
        {
            parentGame = game;

            Header title = new Header("My Game Title");
            window.AddChild(title);

            Paragraph test = new Paragraph("Select the options for you game");
            window.AddChild(test);

            Button button = new Button("Instructions");
            window.AddChild(button);

            button.OnClick = (Entity btn) =>
            {
            //test.Text = "button clicked";
            base.show(false);
                parentGame.State = Game1.GameState.Screen1;
                window.ClearChildren();
            };

            Button button1 = new Button("Play");
            window.AddChild(button1);

            button1.OnClick = (Entity btn) =>
            {
                //test.Text = "button clicked";
                base.show(false);
                parentGame.State = Game1.GameState.Screen2;
                window.ClearChildren();
            };
        }
    }

class Screen2 : UIScreen
    {
        public Screen2(PanelSkin p, Anchor a, int w, int h) : base(p, a, w, h)
        {

        }

        public void Init(Game1 game)
        {
            parentGame = game;

            Header title = new Header("Play Options");
            window.AddChild(title);

            Paragraph test = new Paragraph("Select the options for you game");
            window.AddChild(test);

            Button button1 = new Button("Close");
            window.AddChild(button1);

            button1.OnClick = (Entity btn) =>
            {
                //test.Text = "button clicked";
                base.show(false);
                parentGame.State = Game1.GameState.Welcome;
                window.ClearChildren();
            };
        }
    }

the constructor class requires you to specify parameters for each of the items within the base screen constructor. The screen constructor just passes the values into the base constructor.

The Init method requires a parameter for the game. This means the later button1.OnClick can access the enum to switch screens. This code creates a header, paragraph, a button to carry out an action on the paragraph, and a button to access the enum and switch screens.

Note you need to do this for each screen I have created a screen for Welcome which is the game title and buttons to instructions and play. You could also add images etc to make a better title screen. I have created a Screen1 for instructions, and a Screen2 for the play options.

Creating screens in Game1.cs

You will need to declare variables for your screens

        Welcome title;
        Screen1 instructions;
        Screen2 playOptions;

in the Initialize method add the following:

            // TODO: Add your initialization logic here
            UserInterface.Initialize(Content, BuiltinThemes.editor);

            title = new Welcome(PanelSkin.Fancy, Anchor.Center, 300, 300);
            instructions = new Screen1(PanelSkin.Fancy, Anchor.Center, 300, 300);
            playOptions = new Screen2(PanelSkin.Fancy, Anchor.Center, 300, 300);

            title.Init(this);
            instructions.Init(this);
            playOptions.Init(this);

Make sure the Update method includes the line:

UserInterface.Active.Update(gameTime);

And Draw includes this line:

UserInterface.Active.Draw(spriteBatch);

Finally create the SwitchScreen method, this is called when the enum is changed:

        public void SwitchScreen()
        {
            switch (State)
            {
                case GameState.Welcome:
                    title.show(true);
                    instructions.show(false);
                    playOptions.show(false);
                    break;
                case GameState.Screen1:
                    title.show(false);
                    instructions.show(true);
                    playOptions.show(false);
                    break;
                case GameState.Screen2:
                    title.show(false);
                    instructions.show(false);
                    playOptions.show(true);
                    break;
            }
        }

You will need a case for each possible enum value. State is the property created at the begining with the get/set method.