简体   繁体   English

Pygame-文本不会出现在窗口中

[英]Pygame - Text won't appear on the window

I was displaying some text on the window but when i executed the code, it only showed a blank black window. 我在窗口上显示了一些文本,但是当我执行代码时,它只显示了一个空白的黑色窗口。 I had no idea what was wrong, as no exceptions occurred. 我不知道出什么问题了,因为没有例外发生。 It just would not work. 只是行不通。

Well i am an absolute beginner who have just passed some Python course for a couple of weeks, so i was just playing around the pygame module, instead of having big plans like game developing. 好吧,我是一个绝对的初学者,刚刚通过了一些Python课程几周,所以我只是在玩pygame模块,而不是像游戏开发这样的大计划。 I also tried to search for similar problems but they are all so complicated that I can not understand quite well due to the long pieces of code. 我也尝试搜索类似的问题,但是它们都很复杂,由于代码很长,我无法完全理解。 I checked and no syntax is wrong, the font file is present, and the names of the objects are in the right place ie they are not using the wrong methods. 我检查了一下,没有语法错误,字体文件存在,并且对象名称在正确的位置,即它们未使用错误的方法。 i don't know what else i can try... 我不知道我还能尝试什么...

import pygame as pg

pg.init()

win = pg.display.set_mode((720,540))

consolas = pg.font.SysFont("Consolas.ttf", 100)

text = consolas.render("hello", False , (255,255,255))

win.blit(text , (0,0))

i expected the string "hello" will be blited on the surface with the size of 100 and color to be completely white, but the whole thing did not show up at all. 我预计字符串“ hello”会在表面上变黑,大小为100,颜色完全为白色,但整个过程根本没有出现。

You need to use pg.display.update() or pg.display.flip() after you have drawn the text ie after win.blit line. 在绘制文本之后,即在win.blit行之后,需要使用pg.display.update()或pg.display.flip()。 The difference and use of the two can be found here : Difference between pygame.display.update and pygame.display.flip 两者的区别和用途可以在这里找到: pygame.display.update和pygame.display.flip之间的区别

You have to call pygame.display.flip or pygame.display.update to actually update the screen with the content of the win surface. 您必须调用pygame.display.flippygame.display.update才能使用获胜表面的内容实际更新屏幕。

Also, you should have a main loop that handles events by calling pygame.event.get . 另外,您应该有一个主循环,可以通过调用pygame.event.get处理事件。 This ensures your window stays open. 这样可以确保您的窗口保持打开状态。 Also, if you don't process events, your window becomes unresponsive and maybe doesn't even draw anything (depending on your OS/window manager) 另外,如果您不处理事件,则窗口将无响应,甚至可能不会绘制任何内容(取决于您的操作系统/窗口管理器)

So add this to your code: 因此,将其添加到您的代码中:

run = True
while run:
for e in pg.event.get():
    if e.type == pg.QUIT:
        run = False
    pg.display.update()

As people have already commented that you have to call pygame.display.update() or pygame.display.flip() Here's the full code: 正如人们已经评论过的那样,您必须调用pygame.display.update()pygame.display.flip()下面是完整的代码:

import pygame as pg

pg.init()

win = pg.display.set_mode((720,540))

consolas = pg.font.SysFont("Consolas.ttf", 100)
running = True
while running:
    text = consolas.render("hello", False , (255,255,255))
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
    win.blit(text , (0,0))
    pg.display.flip()
pg.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM