0

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. 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 i.e. 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.

3 Answers3

1

You have to call pygame.display.flip or pygame.display.update to actually update the screen with the content of the win surface.

Also, you should have a main loop that handles events by calling 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()
sloth
  • 99,095
  • 21
  • 171
  • 219
0

You need to use pg.display.update() or pg.display.flip() after you have drawn the text i.e. after win.blit line. The difference and use of the two can be found here : Difference between pygame.display.update and pygame.display.flip

sk_462
  • 587
  • 1
  • 7
  • 16
0

As people have already commented that you have to call pygame.display.update() or pygame.display.flip() Here's the full code:

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()
Vitrioil
  • 179
  • 1
  • 6