[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Erratic behavior with pygame.font.SysFont



The problem I am having initially arose when I was writing a simple
pygame script that would allow me to 'click through' one bye one all
the fonts returned by the function pygame.font.get_fonts(), and see
each one rendered individually in the pygame window. Clicking was
supposed to result in the font switching to the next one in the list
of system fonts available. However, I tried implementing this several
different ways, but I couldn't even switch the font once successfully.
I wouldn't get any errors or bugs, the program would just freeze. If I
tried to click the "X" and close the pygame window, a little popup
would appear saying "program is not responding, blah blah blah".
I am using pygame 1.9.1, on a Dell Inspiron 1420 running Windows
Vista.

Here is a simple 31-line script that should allow the user to click
through and view each available system font one by one, but freezes up
after the first click every time (note that it freezes and doesn't
have any errors or traceback):

import pygame
import sys

if __name__ == "__main__":
    pygame.init()
    size = width,height = 400,400
    screen = pygame.display.set_mode(size)
    clock = pygame.time.Clock()

    FONT_LIST = pygame.font.get_fonts()
    counter = 0
    font = pygame.font.SysFont(FONT_LIST[counter],20)
    font_image = font.render("current font: %s" % FONT_LIST[counter],
True, (255,255,255))

    while True:
        time_passed = clock.tick(20)
        for event in pygame.event.get():
            if event.type == pygame.QUIT or (event.type ==
pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button
== 1:
                if counter + 1 < len(FONT_LIST):
                    counter += 1
                    print '1 ~ switching to the next font'
                    font = pygame.font.SysFont(FONT_LIST[counter], 20)
                    print '2 ~ initialized new font'
                    font_image = font.render('current font: %s"' %
FONT_LIST[counter], True, (255,255,255))
                    print '3 ~ rendered new font'

        screen.fill((0,0,0))
        screen.blit(font_image,(100,100))
        pygame.display.flip()

Using the print statements, I deduced that the problematic line is the
one after the first print statement inside the inner if block. The
second print statement is never executed, the program just hangs
there.

Any insight?
Thanks,
Daniel