0

I am building a Sudoku game. Board is a 2D array of digits from 0 to 9 where 0 is an empty space. I have trouble displaying my board with Pygame. When I select a square on the board with digit 5 and enter a new digit 7 Pygame is suppose to just display newly entered digit instead of the previous one. But instead it displays the new digit over the previous digit (like on the picture). Backend works perfectly meaning that e.g. when I enter 7, it substitutes 7 for 5 on my 2D array board. But for some reason it doesn't display correctly on the screen. My chunk of code responsible for drawing is below including the picture.

P.S. I tried functions pygame.event.pump() and pygame.display.flip() but nothing helps.

while game_running:

......code dealing with events........

 for r in range(9):
    for c in range(9):
        #draw small rectangle
        sm_rect = pygame.Rect(c*sm_rect_width, r*sm_rect_height, sm_rect_width, sm_rect_height)
        pygame.draw.rect(game_window, (0,0,0), sm_rect, 1)

        #draw large rectangle if it is time
        if r%3 == 0 and c%3 == 0:
            lrg_rect = pygame.Rect(c/3*lrg_rect_width, r/3*lrg_rect_height, lrg_rect_width, lrg_rect_height)
            pygame.draw.rect(game_window, (0,0,0), lrg_rect, 3)

        #draw number
            #Render text
        if board[r][c] == 0:
            txt_surface = font.render(" ", True, pygame.Color('dodgerblue2')) 
            game_window.blit(txt_surface, (c*sm_rect_width+17, r*sm_rect_height+15))
        if board[r][c] > 0:
            txt_surface = font.render(str(board[r][c]), True, pygame.Color('dodgerblue2')) 
            game_window.blit(txt_surface, (c*sm_rect_width+17, r*sm_rect_height+15))
       

  pygame.display.update()

enter image description here

Li Cooper
  • 71
  • 1
  • 2
  • 12
  • 1
    Do you clear the display in every frame? – Rabbid76 Apr 30 '21 at 04:31
  • 1
    `if board[r][c] == 0: txt_surface = font.render(" ", True, pygame.Color('dodgerblue2'))` Why do you display nothing? – D_00 Apr 30 '21 at 08:25
  • @D_00, initially I didn't have piece of the code. I tried to use it to overwrite previous digits. Didn't work. – Li Cooper Apr 30 '21 at 13:22
  • 1
    Ok then, that means that you are not updating the screen. Check the indentation of `pygame.display.update()`: the problem can't come from elsewhere, as you use a `list` to display the numbers. – D_00 Apr 30 '21 at 13:34
  • @D_00 thanks for help. I found an issue and just posted an answer to my question. – Li Cooper Apr 30 '21 at 13:36

1 Answers1

0

Thanks to suggestion from Rabbid76 I solved the issue.

The issue was that I was not clearing the display every frame.
Clearing of the screen is done by the function screen.fill(color) and I had it outside my while game_running loop.

I put it inside and now my screen is refreshing and board works as needed.

Here is the solution a little more on the issue: How to clear up screen in pygame?

P.S. Thanks for the help everyone.

D_00
  • 1,440
  • 2
  • 13
  • 32
Li Cooper
  • 71
  • 1
  • 2
  • 12