我如下定义对象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
是一个绿色矩形。我想在此矩形内显示文本。文本由从列表risk_level
中随机选择的字符串risks
定义。
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)
此代码不会失败,但是标题不会显示在屏幕上的任何地方。
答案 0 :(得分:1)
问题在于创建精灵时,您是在(200, 100)
表面的坐标screen
上书写文本,因此当下一帧清除屏幕时,文本将消失。
为了使GeoFence
对象上的文本变亮,您需要将其变亮到坐标self.image
上的(0, 0)
上(如果应该将其左上对齐)。>
如果表面应适合文本的大小,则可以首先渲染文本表面,并将其大小用作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
,然后将其用作文本的空白位置。执行此操作时,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