I am trying to build a Sudoku solver with a GUI using pygame. I fill the screen with white, and draw rectangles for the grid, then i flip the screen to update it. Now i use the board array to draw grid values, using the following function
def on_render(self, board):
font = pygame.font.Font(pygame.font.match_font('nintendoext003'), 1)
screen = pygame.display.get_surface()
screen.fill((255, 255, 255))
for height in range(9):
for width in range(9):
rect = pygame.Rect(width*70, height*70, 68, 68)
pygame.draw.rect(screen, (0, 0, 0), rect)
pygame.display.flip()
screen = pygame.display.get_surface()
for height in range(9):
for width in range(9):
if not board[height][width] == 0:
value = font.render(str(board[height][width]), True, (255, 255, 255))
valrect = value.get_rect()
valrect.center = (width*70 + 34, height*70 + 34)
screen.blit(value, valrect)
the grid draws out correctly and the non zero elements are also read correctly, but the text is not rendered at all. since the render function is also gonna draw the solved grid as the program solves it, I want it to update as ever value is blit to screen. what am I doing wrong?
The last parameter to the constructor of pygame.font.Font
is the size of the font in pixel. So 1 means 1 pixel.
Furthermore you have to update the display after drawing the text. Note, it is sufficient to update the display once, after the entire scene has been drawn:
Use a different font size (e.g. 40) and update the display at the end of on_render
:
def on_render(board):
font = pygame.font.Font(pygame.font.match_font('nintendoext003'), 40)
screen = pygame.display.get_surface()
# clear display
screen.fill((255, 255, 255))
# draw grid
for height in range(9):
for width in range(9):
rect = pygame.Rect(width*70, height*70, 68, 68)
pygame.draw.rect(screen, (0, 0, 0), rect)
# draw text
for height in range(9):
for width in range(9):
if not board[height][width] == 0:
value = font.render(str(board[height][width]), True, (255, 255, 255))
valrect = value.get_rect()
valrect.center = (width*70 + 34, height*70 + 34)
screen.blit(value, valrect)
# update display
pygame.display.flip()