Pygame - 移动矩形中的中心文本[重复]

Posted

技术标签:

【中文标题】Pygame - 移动矩形中的中心文本[重复]【英文标题】:Pygame - center text in moving rect [duplicate] 【发布时间】:2021-09-19 17:55:51 【问题描述】:

我正在 pygame 中制作一个数字游戏,并且有从屏幕顶部到底部的正方形,同时有一个数值,并且该数字显示在每个正方形上。我正在努力让文本以正方形为中心,当每个数字可以包含 1 - 4 位数字时,这会变得更加困难

class sqr:
    def __init__(self):
        self.colours = [(255,0,0), (0,255,0), (0,0,255)]
        self.Xpositions = [0,125,245,365,485]
        self.OnScreen = False
        self.X = random.choice(self.Xpositions) 
        self.Y = 0
        self.colour = random.choice(self.colours)
        self.number = random.choice([20,40,80])
        self.numberFont =  pygame.font.Font("TitilliumWeb-Black.ttf", 48)

    def drawSquare(self, colour,  X, Y):
        pygame.draw.rect(screen, colour, (X, Y, 120, 120))

    def numberTextFunc(self, X, Y):
        numberText = self.numberFont.render(f"self.number", True, (87, 63, 63))
        screen.blit(numberText, (X , Y))

square = sqr()

gameRunning = True
while gameRunning:

    background = screen.fill((0,100,140))
    
    #draw lines for grid
    for i in range(5):
        pygame.draw.rect(screen, (0,0,0), (line[i], 0 , 5, 800))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameRunning = False

    #square
    square.drawSquare(square.colour, square.X, square.Y)
    square.numberTextFunc(square.X, square.Y)
    square.OnScreen = True

    square.Y += 1

    if square.Y >= 680:
        square.Y = 680

    pygame.display.update()

【问题讨论】:

【参考方案1】:

使用pygame.Surface.get_rect 获取文本矩形。 pygame.Surface.get_rect.get_rect() 返回一个与 Surface 对象大小相同的矩形,该矩形始终从 (0, 0) 开始。将文本矩形的中心设置为正方形的中心。使用文本reectnagle 到blit 文本。 blit 的第二个参数是一个元组 (x, y) 或一个矩形。对于矩形,只考虑矩形的左上角。因此,您可以将文本矩形直接传递给blit

class sqr:
    # [...]

    def numberTextFunc(self, X, Y):
        numberText = self.numberFont.render(f"self.number", True, (87, 63, 63))
        rect = pygame.Rect(X, Y, 120, 120)
        textRect = numberText.get_rect(center = rect.center)
        screen.blit(numberText, textRect)

您可以简化代码。 XY 是对象的属性。因此无需将坐标传递给方法:

class sqr:
    def __init__(self):
        self.colours = [(255,0,0), (0,255,0), (0,0,255)]
        self.Xpositions = [0,125,245,365,485]
        self.OnScreen = False
        self.X = random.choice(self.Xpositions) 
        self.Y = 0
        self.colour = random.choice(self.colours)
        self.number = random.choice([20,40,80])
        self.numberFont =  pygame.font.Font("TitilliumWeb-Black.ttf", 48)

    def drawSquare(self, colour):
        pygame.draw.rect(screen, colour, (self.X, self.Y, 120, 120))

    def numberTextFunc(self):
        numberText = self.numberFont.render(f"self.number", True, (87, 63, 63))
        rect = pygame.Rect(self.X, self.Y, 120, 120)
        textRect = numberText.get_rect(center = rect.center)
        screen.blit(numberText, textRect)
gameRunning = True
while gameRunning:

    background = screen.fill((0,100,140))

    #draw lines for grid
    for i in range(5):
        pygame.draw.rect(screen, (0,0,0), (line[i], 0 , 5, 800))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameRunning = False

    #square
    square.drawSquare(square.colour)
    square.numberTextFunc()
    square.OnScreen = True

    square.Y += 1
    if square.Y >= 680:
        square.Y = 680

    pygame.display.update()

【讨论】:

以上是关于Pygame - 移动矩形中的中心文本[重复]的主要内容,如果未能解决你的问题,请参考以下文章

pygame / python中的文本输入框[重复]

Pygame移动加速问题,平台游戏[重复]

Pygame sprite创建重复而不是移动

Pygame的矩形没有移动?

我怎么知道Pygame中是否触及了圆圈和矩形?

自动填充屏幕高度的 div 中的引导中心文本 [重复]