[英]Displaying text in pygame screen
这是我的程序的一部分
running = True
win_size = [800,600]
Color_screen=[201, 169, 167]
Color_line=(0,0,0)
# initialize pygame
pygame.init()
scr = pygame.display.set_mode(win_size)
pygame.display.set_caption('Traverse')
scr.fill(Color_screen)
pygame.display.flip()
# Drawing Traverse
for i in range(len(x)-1):
pygame.time.delay(1000)
pygame.draw.line(scr,Color_line,(x[i],y[i]),(x[i+1],y[i+1]))
pygame.display.flip()
# Displaying Scale
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render(f'Scale : {scale} pixels = 1 metre', True, Color_line)
textRect = text.get_rect()
textRect.center = (100, 50)
scr.blit(text,[100,20])
while running:
# looking for events
for events in pygame.event.get():
if events.type == QUIT:
running = False
pygame.quit()
一切似乎都很好,只是文本没有显示在屏幕上。 正在绘制导线,但未绘制文本。 代码中是否缺少某些内容?
在文本 Surface 是blit
之后,您必须通过pygame.display.flip()
或pygame.display.update()
更新显示Surface
:
font = pygame.font.Font('freesansbold.ttf', 32)
text = font.render(f'Scale : {scale} pixels = 1 metre', True, Color_line)
textRect = text.get_rect()
textRect.center = (100, 50)
scr.blit(text,[100,20])
pygame.display.flip() # <---
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.