渲染不适用于str对象

发布于 2025-01-27 00:25:04 字数 658 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2025-02-03 00:25:04

我弄清楚了...

首先,Render不采用位置参数,因此我们不能使用这些参数。

链接到文档:
我的文本不会在PyGame中闪烁到我的显示器

第二,您必须指定要使用的字体。

我尝试使用此操作:

import pygame
from pygame.locals import *

pygame.init()

font_game = pygame.font.SysFont("Arial",20)

t = pygame.font.Font.render(
    "Testing: ",
    1,
    (255,255,255)
    )

但这是错误的

typeError:for'pygame.font.font'对象的描述符'渲染
应用于“ str”对象

如果您查看此处的文档,

https://www.pygame.org/docs/ref/ref/ref/font.html#pygame.font.font.render

这些是参数:

render(text, antialias, color, background=None)

背景是一个可选的参数,我指定了3个参数,所以我指定了3个参数我做错了什么?

安装pygame时,下载的文件之一是font.pyi文件。

如果您查看该文件,则可以:(

class Font(object):

    def __init__(self, name: Union[AnyPath, IO[Any], None], size: int) -> None: ...
    def render(
        self,
        text: str,
        antialias: bool,
        color: _ColorValue,
        background: Optional[_ColorValue] = None,
    ) -> Surface: ...

该文件中还有更多内容,但这是重要的部分)。

您不仅必须指定文本,tialias和颜色,而且如果您查看班级的第一行,def ___init __,那就是告诉您您需要指定字体和尺寸。

一旦我将代码更改为此,它就可以了:

font_game = pygame.font.SysFont("Arial",20)

t = pygame.font.Font.render(
    font_game,
    "Testing: ",
    1,
    (255,255,255)
    )

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:

import pygame
from pygame.locals import *

pygame.init()

font_game = pygame.font.SysFont("Arial",20)

t = pygame.font.Font.render(
    "Testing: ",
    1,
    (255,255,255)
    )

But this was erroring out

TypeError: descriptor 'render' for 'pygame.font.Font' objects doesn't
apply to a 'str' object

If you look at the documentation located here:

https://www.pygame.org/docs/ref/font.html#pygame.font.Font.render

These are the arguments:

render(text, antialias, color, background=None)

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 the font.pyi file.

If you look at that file, you have this:

class Font(object):

    def __init__(self, name: Union[AnyPath, IO[Any], None], size: int) -> None: ...
    def render(
        self,
        text: str,
        antialias: bool,
        color: _ColorValue,
        background: Optional[_ColorValue] = None,
    ) -> Surface: ...

(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:

font_game = pygame.font.SysFont("Arial",20)

t = pygame.font.Font.render(
    font_game,
    "Testing: ",
    1,
    (255,255,255)
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文