Text Display Tutorial

Even though you are going to be making graphical 2D games, displaying text is important. You will be displaying text in order to display the score of the game or a character name. In RPGs, you'll be displaying text for character dialog. And, as you will learn later in your game making career, displaying text is also useful for debugging your game.

Here's the code we will be discussing in this tutorial.

graphics.Initialize
key.Initialize 

' create the font
MyFont = graphics.CreateFont("system", 10)

do while not key.Pressed(vk_escape) and not key.Pressed(vk_windowx)
    graphics.Clear
        graphics.SetText "Hiya", 100, 100, MyFont    
    graphics.Display
loop

graphics.Terminate
key.Terminate

Go ahead and copy and paste this into your Brutus2D IDE and hit F5 to test it out. Then, come back to this tutorial.

From previous tutorials, you should know everything except these two lines :

MyFont = graphics.CreateFont("system",10)
graphics.SetText "Hiya", 100, 100, MyFont

"Creating" Fonts

Brutus2D has no fonts. Instead, it has to get one from the player's computer and make a copy of it. This is "creating" a font and is exactly what graphics.CreateFont does. Let's take a look at the line where it's used :

MyFont = graphics.CreateFont("system",10)

"system" is the font to copy. "system" is used by most people because you can never tell for sure what fonts the player has installed on his computer — with one exception : system. It should always be there to be copied no matter what.

The number to the right of "system" is the size you want the font to be when it's copied. In this case, I've chosen 10. This is my personal choice, you should play around with this number and see what suits your game project.

After graphics.CreateFont has created the font, Brutus2D internally assigns that font a number. This number is known as the font index and it's how Brutus2D keeps track of fonts. Whenever you want to display text in a font you've created, you will have to refer to the font index. Don't worry, this is all easier than it sounds, as you will soon see.

The font index is stored in MyFont, which gives us an easy to remember name to use as the font index.

Displaying Text

Now that we've created a font with graphics.CreateFont, it's time to display some text on the screen. This is done with graphics.SetText. Here's the line again :

graphics.SetText "Hiya", 100, 100, MyFont

"Hiya" is the text to be displayed. The next two numbers is the position you want it displayed on the screen. The first number is how many pixels left from the left side of the screen and the second number is how many pixels down from the top side of the screen. Finally, we have to tell Brutus2D what font to use, which of course can be referred to by MyFont. (See, I told you it was easy.)

Numbers Vs. Strings

If you ran the entire example code at the beginning of this tutorial you will have noticed that Hiya is displayed without the quotes. This is because any text you give to Brutus2D is a string, which simply means the data between the quotes is text-only and not a number that can be used for math. Even numbers wrapped in quotes is a string.

Conclusion

You now know how to display text in Brutus2D. You can now move on to the next tutorial where you will learn about variables.

page_revision: 1, last_edited: 1205618036|%e %b %Y, %H:%M %Z (%O ago)