【问题标题】:python and pygame font centering is working but I don't know whypython 和 pygame 字体居中工作,但我不知道为什么
【发布时间】:2017-08-09 15:22:31
【问题描述】:

我的代码运行良好,尽管我完全不知道为什么。

我的代码是:

def textobjects(message, font,color):
    Textsurface = font.render(message, True, color)
    return Textsurface, Textsurface.get_rect()

def Display_text(message,color):
    Heading = pygame.font.SysFont("Arial",70)
    TextShown, Textrect = textobjects(message, Heading,color)
    Textrect.center = ((displaywidth/2),0)
    gameWindow.blit(TextShown,Textrect)
    pygame.display.update()
    time.sleep(2)
    game_loop()


def border():
    Display_text("You hit the border",wine)

我不知道为什么我能做到:

Textrect.center = ((displaywidth/2),0)

无需定义“.center”。 .center 是 pygame/python 预定义模块吗?

【问题讨论】:

  • 是的,pygame.Rect 有一种叫做 center 的态度。但我强烈建议你从一个最基本的例子开始
  • 这不完全是我的代码,不。我只是在看一个视频教程,很想知道它为什么会起作用。感谢您的回复。
  • 我在哪里可以找到初学者(我自己)的基本示例?
  • @chubel 这里有两个链接会有所帮助:pygame.org/docs/tut/newbieguide.htmlpygame.org/docs
  • 我很感激。

标签: python fonts pygame sdl


【解决方案1】:

.center 是 rect 对象的一部分,您可以获取和设置它

你也可以这样做

Rect.left
Rect.top
Rect.right
Rect.bottom
Rect.topleft
Rect.bottomleft
Rect.topright
Rect.bottomright
Rect.centerx
Rect.centery

【讨论】:

    猜你喜欢
    • 2016-02-13
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2015-12-16
    • 1970-01-01
    相关资源
    最近更新 更多