1

I'm trying with this piece of code make a text appear when the condition is respected and then make it disappear, without refreshing all the page

it's like a small warning that appears when the player does something wrong and then disappear if he correct it.

for i in range (len(grid)): 
          if 2 not in grid[i] :  
            if grid[i] == grid[i -1]:
              display = True
              if display== True:
              #add a msg text    
                countText = myfont.render( "check the row again!", 1, (255, 0, 0))
                countRect = countText.get_rect()
                countRect.y, countRect.centerx = 10, 450
                SCREEN.set_clip(countRect)        # Allow updates only to area in countRect
                SCREEN.blit(background, (0,0))      # bg_img will only be drawn within countRect
                SCREEN.blit(countText, countRect)
                pygame.display.flip()
            else: 
              pass
      
              #countText = myfont.render('emptyagain', False, (0, 0, 0))
              #SCREEN.blit(background,(410,10))
              #SCREEN.fill(SNOW_COLOR)


thank you :)

leo
  • 35
  • 6
  • Does something about the code not work? If so, what? – blackbrandt Aug 30 '21 at 12:23
  • hi, thanks for the answer, this code actualy makes appear the message but i don't know how to maake the text desapear ones the player fix his mistake, i want to do that by only overloading the text (like en empty text) and refresh it without refreashing all the SCREEN – leo Aug 30 '21 at 12:27

1 Answers1

0

If you want the text to be persistent, you need to draw the message in the application loop.

Add a variable displayWarning in the global namespace. Reset the variable before the loop but set it True inside the loop:

displayWarning = Fasle
global displayWarning # just if this code is in a function

displayWarning = False
for i in range (len(grid)): 
    if 2 not in grid[i] :  
        if grid[i] == grid[i -1]:
            displayWarning = True

Draw the text in the application loop

# application loop
while run: 
    # [...]

    SCREEN.blit(background, (0,0))

    # [...]

    if displayWarning:
        countText = myfont.render( "check the row again!", 1, (255, 0, 0))
        countRect = countText.get_rect()
        countRect.y, countRect.centerx = 10, 450
        SCREEN.blit(countText, countRect)
        
    # [...]

    pygame.display.flip()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • hello @Rabbid76, i just tried it, the good thing is that the message show up when the condition is filled, the problem is that the message still here when the play fix the probelem. SCREEN.blit(background, (0,0)) is updating all my screen when i remove the message and it not what i want – leo Aug 30 '21 at 12:39
  • @leo You must set it `False` before the loop. If this does not work, then the condition `grid[i] == grid[i -1]` is met somewhere and it is set to `True` again. – Rabbid76 Aug 30 '21 at 12:43
  • 1
    your right the condition is set to True again, i'm trying to figure out why – leo Aug 30 '21 at 13:25