gpt4 book ai didi

python - 如何在矩形内添加文本?

转载 作者:行者123 更新时间:2023-12-01 09:10:07 25 4
gpt4 key购买 nike

我定义了对象fences的类,如下所示:

class GeoFence(pygame.sprite.Sprite):
def __init__(self, rect, risk_level, *groups):
self._layer = 1
pygame.sprite.Sprite.__init__(self, groups)
self.image = pygame.surface.Surface((rect.width, rect.height))
self.image.fill(GREEN)
self.rect = rect
self.risk_level = risk_level
self.font = pygame.font.SysFont('Arial', 25)
screen.blit(self.font.render(risk_level, True, (255,0,0)), (200, 100))

每个对象fence都是一个绿色矩形。我想在这个矩形内显​​示文本。该文本由从列表risks 中随机选择的字符串risk_level 定义。

all_sprites = pygame.sprite.LayeredUpdates()
fences = pygame.sprite.Group()

risks = ["HIGH","MEDIUM","LOW"]
for rect in (pygame.Rect(510,150,75,52), pygame.Rect(450,250,68,40), pygame.Rect(450,370,68,48)):
risk = risks[random.randint(0,2)]
GeoFence(rect, risk, all_sprites, fences)

此代码不会失败,但标题不会显示在屏幕上的任何位置。

最佳答案

问题是,当创建 Sprite 时,您在屏幕表面的坐标 (200, 100) 处传输文本,因此文本会消失当屏幕在下一帧中被清除时。

为了将文本传输到 GeoFence 对象上,您需要将其传输到坐标 (0, 0)< 处的 self.image (如果应该左上对齐)。

如果表面需要适合文本的大小,您可以先渲染文本表面并将其尺寸用作 self.image 表面的尺寸。

import random
import pygame


pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
BG_COLOR = pygame.Color('gray12')
GREEN = pygame.Color('green')
FONT = pygame.font.SysFont('Arial', 25)


class GeoFence(pygame.sprite.Sprite):

def __init__(self, rect, risk_level, *groups):
self._layer = 1
pygame.sprite.Sprite.__init__(self, groups)
text_surface = FONT.render(risk_level, True, (255,0,0))
self.image = pygame.surface.Surface(text_surface.get_size())
self.image.fill(GREEN)
self.image.blit(text_surface, (0, 0))
self.rect = self.image.get_rect(topleft=rect.topleft)
self.risk_level = risk_level


all_sprites = pygame.sprite.LayeredUpdates()
fences = pygame.sprite.Group()

risks = ["HIGH","MEDIUM","LOW"]
for rect in (pygame.Rect(510,150,75,52), pygame.Rect(450,250,68,40),
pygame.Rect(450,370,68,48)):
risk = random.choice(risks)
GeoFence(rect, risk, all_sprites, fences)

done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

all_sprites.update()

screen.fill(BG_COLOR)
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(60)

也可以将另一种颜色传递给 Font.render将用作背景颜色。这将使您可以稍微缩短代码:

class GeoFence(pygame.sprite.Sprite):

def __init__(self, rect, risk_level, *groups):
self._layer = 1
pygame.sprite.Sprite.__init__(self, groups)
self.image = FONT.render(risk_level, True, (255,0,0), GREEN)
self.rect = self.image.get_rect(topleft=rect.topleft)
self.risk_level = risk_level

如果您想让文本居中,请为文本创建另一个矩形,将其center坐标设置为self.rect.center,然后将其用作blit文本的位置。当您执行此操作时,self.rect 应仍位于默认位置 (0, 0)。

def __init__(self, rect, risk_level, *groups):
self._layer = 1
pygame.sprite.Sprite.__init__(self, groups)
text_surface = FONT.render(risk_level, True, (255,0,0))
self.image = pygame.surface.Surface(
(text_surface.get_width()+110, text_surface.get_height()+20))
self.image.fill(GREEN)
# Create the rect, but don't set the coordinates yet.
self.rect = self.image.get_rect()
# Set the center of the text_rect to the center of the self.rect.
text_rect = text_surface.get_rect(center=self.rect.center)
# Then blit the text at the text_rect and it will be centered.
self.image.blit(text_surface, text_rect)
# Afterwards set the rect to the desired position.
self.rect.topleft = rect.topleft

关于python - 如何在矩形内添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51730802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com