渲染不适用于str对象
import pygame
from pygame.locals import *
pygame.init()
surf = pygame.display.set_mode((400,400))
pygame.draw.rect(
surface = surf,
color = (0,255,255),
rect = (100,100,100,50)
)
clock = pygame.time.Clock()
t = pygame.font.Font.render("Change Color",1,(255,255,255))
display.blit(
source = t,
dest = (100,100,100,50),
area = None,
special_flags = 0
)
pygame.display.update()
我收到的错误消息是:
t = pygame.font.Font.render("Change Color",1,(255,255,255))
TypeError: descriptor 'render' for 'pygame.font.Font' objects doesn't apply to a 'str' object
我在做什么错?
import pygame
from pygame.locals import *
pygame.init()
surf = pygame.display.set_mode((400,400))
pygame.draw.rect(
surface = surf,
color = (0,255,255),
rect = (100,100,100,50)
)
clock = pygame.time.Clock()
t = pygame.font.Font.render("Change Color",1,(255,255,255))
display.blit(
source = t,
dest = (100,100,100,50),
area = None,
special_flags = 0
)
pygame.display.update()
The error message I am getting is this:
t = pygame.font.Font.render("Change Color",1,(255,255,255))
TypeError: descriptor 'render' for 'pygame.font.Font' objects doesn't apply to a 'str' object
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我弄清楚了...
首先,
Render
不采用位置参数,因此我们不能使用这些参数。链接到文档:
我的文本不会在PyGame中闪烁到我的显示器
第二,您必须指定要使用的字体。
我尝试使用此操作:
但这是错误的
如果您查看此处的文档,
: https://www.pygame.org/docs/ref/ref/ref/font.html#pygame.font.font.render
这些是参数:
背景是一个可选的参数,我指定了3个参数,所以我指定了3个参数我做错了什么?
安装
pygame
时,下载的文件之一是font.pyi
文件。如果您查看该文件,则可以:(
该文件中还有更多内容,但这是重要的部分)。
您不仅必须指定文本,tialias和颜色,而且如果您查看班级的第一行,
def ___init __
,那就是告诉您您需要指定字体和尺寸。一旦我将代码更改为此,它就可以了:
I figured it out...
First of all,
render
does not take positional arguments, so we can't use those.Link to documentation:
my text won't blit to my display in pygame
Second, you have to specify the font to use.
I tried using this next:
But this was erroring out
If you look at the documentation located here:
https://www.pygame.org/docs/ref/font.html#pygame.font.Font.render
These are the arguments:
Background is an optional argument and I specified 3 arguments, so what did I do wrong?
When you install
pygame
, one of the files that gets downloaded is thefont.pyi
file.If you look at that file, you have this:
(There is more in that file, but that is the important part).
Not only do you have to specify the text, antialias, and color, but if you look at the first line in the class,
def __init__
, that's where it's telling you that you need to specify the font and size.Once I changed my code to this, it worked fine: