Search code examples
pythonpygamepygame-tick

pygame.display.update causing flickering or not showing anything


In my game I am trying to make it so when you walk into a object, it displays an image. I'm pretty sure that pygame.display.update() is being called every frame because otherwise the game would be perfectly still.

However when I draw my new rect upon collision it doesn't appear, unless I put another pygame.display.update(rect) with it after it being drawn. This means that update is being called twice at one time, in the main game loop and after drawing the rect. This causes the rect (which has been drawn now) to flicker because of the multiple update calls.

I cannot figure it out why it doesn't get drawn without the second update call.

Main game loop call:

  def events(self):
    #game loop events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            self.playing = False
            self.running = False
  
  def update(self):  
    self.all_sprites.update()


  def main(self):
    while self.playing:
          self.events()
          self.update()
          self.draw()
    self.running = False

  def draw(self):
      self.screen.fill(black)
      self.all_sprites.draw(self.screen)
      self.clock.tick(FPS)
      
      pygame.display.update()

#create game instance
g= Game()
g.new()
while g.running:
  #main gameloop
  g.main()

pygame.quit()
sys.exit()

Here is when I call to draw the rect after collision with my object:

 def update(self):
    self.hitbox.center = numpy.add(self.rect.center,(8,22))
    self.interactionhitbox.center = numpy.add(self.rect.center, (8,16))
    if(self.showPopup):
      # Initialwzng Color
      color = (255,0,0)
        
      # Drawing Rectangle 
      rect = pygame.Rect((0,0,60,60))
      pygame.display.update(rect) # WITHOUT THIS LINE IT DOES NOT GET DRAWN, WITH IT IT FLICKERS
      pygame.draw.rect(self.game.screen, color, rect)

So basically with the second pygame.display.update(rect) call it appears but flickers, and without it it doesn't show up at all

Any help is appreciated sorry if this is a bad question or not formatted right I haven't been here since 2017!


Solution

  • The rectangle is not drawn because the screen will later be cleared with self.screen.fill(black) later. You must draw the rectangle after self.screen.fill(black) and before pygame.display.update().

    Create 2 images and choose the image to be drawn in update:

    def __init__(self, ...)
        # [...]
      
        self.image = ...
    
        self.original_image = self.image
        self.image_and_rect = self.image.copy()
        pygame.draw.rect(self.image_and_rect, (255,0,0), self.image_and_rect.get_rect(), 5)
    
    def update(self):
        self.hitbox.center = numpy.add(self.rect.center,(8,22))
        self.interactionhitbox.center = numpy.add(self.rect.center, (8,16))
    
        if self.showPopup:
            self.image = self.image_and_rect
        else:
            self.image = self.original_image