Как вывести текст в pygame

от admin

Work with text¶

In pygame, text cannot be written directly to the screen. The first step is to create a Font object with a given font size. The second step is to render the text into an image with a given color. The third step is to blit the image to the screen. These are the steps:

Once the font is created, its size cannot be changed. A Font object is used to create a Surface object from a string. Pygame does not provide a direct way to write text onto a Surface object. The method render() must be used to create a Surface object from the text, which then can be blit to the screen. The method render() can only render single lines. A newline character is not rendered.

Initialize a font¶

Initializing the font can take a few seconds. On a MacBook Air the the creation of a system Font object:

took more then 8 seconds:

The function get_fonts() returns a list of all installed fonts. The following code checks what fonts are on your system and how many, and prints them to the console:

You will get something like this:

Render the text¶

The font object can render a given text into an image. In the example below, we place a blue bounding rectangle around that text image:

We then create two more fonts, Chalkduster and Didot at a size of 72 points. We render a text with both fonts:

Finally the text images are blit to the screen like regular images:

This is the result:

Here is the full code.

Edit text with the keybord¶

The keyboard event can be used to edit a text. First we create a text which we save in a string variable text and which we render to an image:

Then we define the bounding rectangle and furthermore a cursor rectangle which is juxtaposed to the text bounding rectangle:

Inside the event loop we watch out for KEYDOWN events. If the key press is a BACKSPACE and the lenght of the string is larger than 0, then we remove the last character, else we append the new character to the text variable:

Then we render the modified text, update the bounding rectangle, and place the cursor box at the end of the updated bounding rectangle:

How to display text in pygame? [duplicate]

I can’t figure out to display text in pygame.
I know I can’t use print like in regular Python IDLE but I don’t know how.

This is only the beginning part of the whole program.
If there is a format that will allow me to show the text I type in the pygame window that’d be great. So instead of using print I would use something else. But I don’t know what that something else is. When I run my program in pygame it doesn’t show anything.
I want the program to run in the pygame window instead of it just running in idle.

Pygame tutorial #1: displaying rendered text in a window.

Recently I had an idea to create a story-based game using python and pygame library. So I decided it’d be worth giving it a shot.

First things first, let’s import the stuff that we need for now.

Now, let’s configure all the necessary stuff.

The set.caption() function changes the window’s name. You could name the window anything you want.

Firstly, we need to create a font renderer that renders text with specific font and size. But creating a rendered font requires 2 things: the font that we need, and the size of the text.

The problem with passing the first thing to the font renderer is that you need to pass the string of the entire path to the font. It is time-consuming to manually search for the fonts you need, so Pygame offers a simple function: font.match_font() , which returns the path to the font, given the font’s name.

Pygame also offers a function that returns a list of available fonts: font.get_fonts() . Try running this in your shell to see all available fonts:

Usually when running this function, it randomly shuffles the list, so I strongly discourage getting fonts from this list via index.
Now that we settled the question, let’s create the font renderer.

Now let’s try to render the message with our rendered font, and color (with a XY position of 5, 500)
This is how it should look like:

This is not a really convenient way to render messages, but at least it renders it now! Let’s simplify this even more:

We can simplify it even more!
Let’s remove the font renderer function and write the message display as a one big function.

At this point the function looks much simpler and requires only one line to be called. It’ll come in handy when writing story-based games.

It is also annoying to write long function calls with the same parameters all the time. So let’s write a list with desired parameters to run the message display function with two arguments instead of five.

Now, let’s use the variable.

We managed to simplify this task to calling the function with ease and 1 short line of code!

Let’s not forget to make a while-loop so our window won’t close as soon as we launch the game.

Here’s the result of our hard work:

The progress might look insignificant, but later on we will use this function to write dialogue between characters, and much more!

PyGame Text

In this section we will look at how you include and manipulate text in Pygame. Text is valuable for many aspects of a good game. Text can be a little bit fiddly to set up in PyGame but once you have it set up it’s fairly easy to work with.

Getting Set Up

Before we begin, let’s create a new file (call it text.py) and copy in the template code from the previous section

Once you’ve saved your file with the template code in it, run the file to make sure you’ve copied it in ok. (You should get a blank white window.)

Including Text — The Simple Method

Including text in PyGame may be done as a three step process :

  • Load the font for the text
  • Render the text you want to a Text surface object
  • Blit the text to the display surface (window)

Let’s start by adding a variable with a colour for our text (place it just below the existing BACKGROUND variable) :

  • # Colours
  • BACKGROUND = (255, 255, 255)
  • TEXTCOLOUR = (200, 100, 0)
  • # Game Setup

Next up we will add a few commands to render a basic text message in the center of the window :

  • pygame.display.set_caption(‘My Game!’)
  • # set up Fonts
  • fontObj = pygame.font.Font(None, 32)
  • textSufaceObj = fontObj.render(‘blah blah’, True, TEXTCOLOUR, None)
  • # The main function that controls the game

What this does is :

Похожие статьи