gpt4 book ai didi

Pygame Surface 被认为是一个元组

转载 作者:行者123 更新时间:2023-12-04 09:40:05 25 4
gpt4 key购买 nike

我正在尝试在屏幕上对文本表面进行 blit,但出现此错误:
类型错误:参数 1 必须是 pygame.Surface,而不是元组。
可能有什么问题?我确信我的文字是一个表面。

import pygame
import pygame.freetype
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((100, 100))
font = pygame.freetype.Font('resource2.ttf', 20)
text_image = font.render("text", (255,255,255))
screen.blit(text_image,(0,0))
pygame.display.flip()

最佳答案

来自 Manualpygame.freetype.Font.render() :

The return value is a tuple: the new surface and the bounding rectangle giving the size and origin of the rendered text.



所以你需要这样的东西:
text_font = pygame.freetype.Font( None, 20 )
text_image, text_rect = text_font.render( "text", (255,255,255) )

[...]

screen.blit( text_image, ( 0, 0 ) )

如~
import pygame
import pygame.freetype

pygame.init()
pygame.font.init()
screen = pygame.display.set_mode( (200, 100) )
text_font = pygame.freetype.Font( None, 20 )
text_image, text_rect = text_font.render( "text", (255,255,255) )
print(str(text_image))

clock = pygame.time.Clock()
done = False
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True

screen.fill( ( 0,0,0 ) ) # paint it black
screen.blit( text_image, ( 0, 0 ) )
pygame.display.flip()

# Clamp FPS
clock.tick_busy_loop(60)

pygame.quit()

关于Pygame Surface 被认为是一个元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62366383/

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