Pokemon Like Auto Type Text

Hello. I always wanted to showcase some code of mine. With Twitch plays Pokemon i decided that i would make a Pokemon related code example. This example  is done using the MonoGame framework with C#. Think of when you walk up to professor oak and when you press A, he talks. The text doesn’t just appear, It writes. I always thought of this as a cool effect. If you have any questions ask in the comments or email me in the contact section. (sweeney1995@live.co.uk)

The Text Box Class

First start off with the using statements at the top of the class.

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

// Creating by Darren Sweeney - dsweeneyblog.wordpress.com

Writing the variables.

        // The Area in which the text will write in
        private Rectangle textBox;
        // Visually shows the text box
        private Texture2D debugBox;
        private SpriteFont font;
        private String text;
        private Vector2 position;
        // Parsing is the breaking up of the ordinary text.
        private String parsedText;
        private Double typeTextLength;
        // The delay it takes for a letter to write in milliseconds
        private int delayInMilliseconds;
        private float scale;
        private String typedText;

        // Allows you to start the text when you want
        private bool startWriting;

        public bool StartWriting
            set { startWriting = value; }

        // Can be used to check when the text is finished writing
        private bool isDoneWriting = false;

        public bool GetDoneWriting
            get { return isDoneWriting; }

The Text Parser

        // What this code will do is insert a newline once the length of the current line plus the length
        // of the current word is longer than the width of the text box, and it repeats until there are
        // no more words to process
        private String parseText(String text)
            String line = String.Empty;
            String returnString = String.Empty;
            String[] wordArray = text.Split(' ');

            foreach (String word in wordArray)
                //                                                            scale - (If the text box has a scale, by default it's 1)
                if (font.MeasureString(line + word).Length() > textBox.Width * 1f)
                    returnString = returnString + line + '\n';
                    line = String.Empty;

                //Formats the line until it can fill with width
                line = line + word + ' ';

            return returnString + line;

The Constructor

        public TextBox(ContentManager content, String newText, Vector2 newPosition, float newScale, int width, int height, int letterDelay)
            // Loading Content
            debugBox = content.Load("textBox");
            font = content.Load("font");

            text = newText;
            position = newPosition;
            scale = newScale;
            textBox = new Rectangle((int)position.X, (int)position.Y, width, height);
            delayInMilliseconds = letterDelay;

            parsedText = parseText(text);

            startWriting = false;

The Update Method

        public void Update(GameTime gameTime)
            if (isDoneWriting == false)
                // No delay means instant display of the text
                if (delayInMilliseconds == 0)
                    typedText = parsedText;
                    isDoneWriting = true;
                else if (typeTextLength < parsedText.Length)                 {                     if (startWriting == true)                     {                         typeTextLength = typeTextLength + gameTime.ElapsedGameTime.TotalMilliseconds / delayInMilliseconds;                     }                     if (typeTextLength >= parsedText.Length)
                        typeTextLength = parsedText.Length;
                        isDoneWriting = true;

                    // A substring extracts a string and requires that you indicate a start index and a length
                    // ie. Substring(0, 3) returns a substring containing the first three chars.
                    typedText = parsedText.Substring(0, (int)typeTextLength);

The Draw Method

        public void Draw(SpriteBatch spriteBatch)
            // You can comment out drawing the debug box if you just want to see the text.
            // The debug box is just to show the area in which the text can appear in.
            spriteBatch.Draw(debugBox, textBox, Color.Red);
            // Draw the text
            spriteBatch.DrawString(font, typedText, new Vector2(textBox.X, textBox.Y), Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);

The Reset Method

        public void Reset()
            startWriting = false;
            isDoneWriting = false;
            typeTextLength = 0;

The Game1 Class

Writing the variables.

        TextBox textBox;

        KeyboardState keyboard;

Creating an instance of TextBox.cs

        protected override void Initialize()
            //                                          Text                         Position           Scale    Width    Height    letter delay - (delayInMilliseconds)
            textBox = new TextBox(Content, "I came when you beat the elite four.", new Vector2(50, 50),   1f,     200,     100,          50);


The Update Method.

        protected override void Update(GameTime gameTime)
            keyboard = Keyboard.GetState();

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))


            // Undo the comment so that when you press space the text will start to write
            if (keyboard.IsKeyDown(Keys.Space))
                textBox.StartWriting = true;

            if (textBox.GetDoneWriting == true)
                // Do something that may happen after text is done drawing
                // such as NPC has spook so let player walk away, select opition, etc

            // If you want to reset the text to its initial state


The Draw Method.

        protected override void Draw(GameTime gameTime)



What it should look like when finished


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s