Question

I have come as far as drawing a rectangle in pygame however I need to be able to get text like "Hello" into that rectangle. How can I do this? (If you can explain it as well that would be much appreciated. Thank-you)

Here is my code:

import pygame
import sys
from pygame.locals import *

white = (255,255,255)
black = (0,0,0)


class Pane(object):
    def __init__(self):

        pygame.init()
        pygame.display.set_caption('Box Test')
        self.screen = pygame.display.set_mode((600,400), 0, 32)
        self.screen.fill((white))
        pygame.display.update()

    def addRect(self):
        self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
        pygame.display.update()

    def addText(self):
        #This is where I want to get the text from

if __name__ == '__main__':
    Pan3 = Pane()
    Pan3.addRect()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit();

Thank you for your time.

Was it helpful?

Solution

You first have to create a Font (or SysFont) object. Calling the render method on this object will return a Surface with the given text, which you can blit on the screen or any other Surface.

import pygame
import sys
from pygame.locals import *

white = (255,255,255)
black = (0,0,0)


class Pane(object):
    def __init__(self):
        pygame.init()
        self.font = pygame.font.SysFont('Arial', 25)
        pygame.display.set_caption('Box Test')
        self.screen = pygame.display.set_mode((600,400), 0, 32)
        self.screen.fill((white))
        pygame.display.update()


    def addRect(self):
        self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
        pygame.display.update()

    def addText(self):
        self.screen.blit(self.font.render('Hello!', True, (255,0,0)), (200, 100))
        pygame.display.update()

if __name__ == '__main__':
    Pan3 = Pane()
    Pan3.addRect()
    Pan3.addText()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit();

enter image description here

Note that your code seems a little bit strange, since usually you do all the drawing in the main loop, not beforehand. Also, when you make heavy use of text in your program, consider caching the result of Font.render, since it is a very slow operation.

OTHER TIPS

Hi! To be honestly there is pretty good ways to write text in any place of current rect. And now i'll show how to do it pretty easy.


First of all we need to create object of rect instance:

                rect_obj = pygame.draw.rect(
                    screen,
                    color,
                    <your cords and margin goes here>
                )

Now rect_obj is object of pygame.rect instance. So, we are free to manipulate with this methods. But, beforehand lets create our rendered text object like this:

                text_surface_object = pygame.font.SysFont(<your font here>, <font size here>).render(
                    <text>, True, <color>
                )

Afterall we are free to manipulate with all methods, as i mensioned before: text_rect = text_surface_object.get_rect(center=rect_obj.center)

What is this code about? We've just got center cords of out current rect, so easy! Now, you need to blit ur screen like this: self.game_screen.blit(text_surface_object, text_rect)


Happy coding! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top