Jesus Cuevas
Jesus Cuevas

Reputation: 57

pygame blitting - center

I am trying to make a script in python for pygame to draw a button with text centered, but when I blit onto the screen, it blits to the x and y I give it, not a proportionally centered location. I want to be able to center it to a set of (x,y,w,h). How would I do this? Here's my code:

# Imports
import pygame

class Text:
    'Centered Text Class'
    # Constructror
    def __init__(self, text, (x,y,w,h), color = (0,0,0)):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        # Start PyGame Font
        pygame.font.init()
        font = pygame.font.SysFont("sans", 20)
        self.txt = font.render(text, True, color)
    # Draw Method
    def Draw(self, screen):
        coords = (self.x, self.y)
        screen.blit(self.txt, coords)

Edit: Comments, yes I know but I only used x and y as temporary variables because I have no idea what the centered x and y would be to center the text. (I want to know how to center its CENTER to a rect, not its top left corner)

Upvotes: 2

Views: 9489

Answers (4)

MestreLion
MestreLion

Reputation: 13676

Let pygame do the maths for you by using Rects and assigning the text's center to the destination center:

# Start PyGame Font
pygame.font.init()
font = pygame.font.SysFont("sans", 20)

class Text:
    'Centered Text Class'
    # Constructror
    def __init__(self, text, (x,y,w,h), color = (0,0,0)):
        self.rect = pygame.Rect(x, y, w, h)
        self.txt = font.render(text, True, color)
    # Draw Method
    def Draw(self, screen):
        coords = self.txt.get_rect()
        coords.center = self.rect.center
        screen.blit(self.txt, coords)

Upvotes: 0

Kal Zekdor
Kal Zekdor

Reputation: 1224

You'll want to use the font.size() method to determine how large the rendered text will be.

Something like:

class Text:
    """Centered Text Class"""
    # Constructror
    def __init__(self, text, (x,y), color = (0,0,0)):
        self.x = x #Horizontal center of box
        self.y = y #Vertical center of box
        # Start PyGame Font
        pygame.font.init()
        font = pygame.font.SysFont("sans", 20)
        self.txt = font.render(text, True, color)
        self.size = font.size(text) #(width, height)
    # Draw Method
    def Draw(self, screen):
        drawX = self.x - (self.size[0] / 2.)
        drawY = self.y - (self.size[1] / 2.)
        coords = (drawX, drawY)
        screen.blit(self.txt, coords)

Upvotes: 7

martineau
martineau

Reputation: 123413

I think something like the following does what you want. It uses pygame.font.Font.size() to determine the amount of space needed to render the text, and then centers that within rectangular region defined by CenteredText instance.

class CenteredText(object):
    """ Centered Text Class
    """
    def __init__(self, text, (x,y,w,h), color=(0,0,0)):
        self.x, self.y, self.w, self.h = x,y,w,h
        pygame.font.init()
        font = pygame.font.SysFont("sans", 20)
        width, height = font.size(text)
        xoffset = (self.w-width) // 2
        yoffset = (self.h-height) // 2
        self.coords = self.x+xoffset, self.y+yoffset
        self.txt = font.render(text, True, color)

    def draw(self, screen):
        screen.blit(self.txt, self.coords)
        # for testing purposes, draw the rectangle too
        rect = Rect(self.x, self.y, self.w, self.h)
        pygame.draw.rect(screen, (0,0,0), rect, 1)

Given:

text = CenteredText('Hello world', (200,150,100,100))

Here's the results from calling text.draw(screen) in a 500x400 pixel window.

screenshot of sample output

Upvotes: 3

le_wofl
le_wofl

Reputation: 303

If you want to perfectly centre an object: When you give Pygame coordinates for an object it takes them to be the coordinates for the upper left corner. Thus we have to halve the x and y coordinates.

coords = (self.x/2, self.y/2)
screen.blit(self.txt, coords)

Other than that your question is unclear.

Upvotes: 0

Related Questions