Python/Pygame: 在特定位置放置文本
Python/Pygame: Placing text in a specific location
在我的代码中,我试图在特定位置放置一个文本框。
screen = pygame.display.set_mode((1000, 600), 0, 32) #Set the window to 1000x600
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
text = font.render('Start', True, BLACK)
textRect = text.get_rect()
最后一行的作用是什么?我该如何修改这段代码,以便将文本放置在特定的 (x,y) 坐标处?
它获取与渲染表面关联的矩形,像这样更改位置:
textRect.center = (center_x, center_y) # set center to (x, y) coordinates
此外,请确保您正在更新屏幕并在游戏循环中绘制表面:
while True:
pygame.display.flip()
screen.blit(text, textRect)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
在我的代码中,我试图在特定位置放置一个文本框。
screen = pygame.display.set_mode((1000, 600), 0, 32) #Set the window to 1000x600
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
text = font.render('Start', True, BLACK)
textRect = text.get_rect()
最后一行的作用是什么?我该如何修改这段代码,以便将文本放置在特定的 (x,y) 坐标处?
它获取与渲染表面关联的矩形,像这样更改位置:
textRect.center = (center_x, center_y) # set center to (x, y) coordinates
此外,请确保您正在更新屏幕并在游戏循环中绘制表面:
while True:
pygame.display.flip()
screen.blit(text, textRect)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()