How to remove a previous rectangle created with pygame.draw.rect and having a picture as background

Viewed 36

Searching the solution to my problem, I saw that some persons encoutered the same problem as I have, and the solution was to display the background at the beggining of the loop, so we don't see previous position etc.. In my case the rectangle represents a health bar for a character in my Python game. I tried screen.blit(background, [0,0]) or even tried pygame.display.flip(), and that strangely does not solve the problem, I don't know what else to do. Here is the part of my code :

import os
import pygame
pygame.init()
screen = pygame.display.set_mode((885,500))
backgroundgame = pygame.image.load(os.path.join(image, 'background.jpg'))

def display():
    screen.blit(backgroundgame, [0,0])
    pygame.display.update()

def health_bar(health_player, health_ennemy):
    screen.blit(backgroundgame, [0,0])
    pygame.draw.rect(background, health_player, (x, y-20, 65, 8))

palying = True
while playing:
    health_bar(health_player, health_ennemy)
    display()
   

I didn't put all my code, but actually the rectangle moves with the sprite, but can still see the preivous healthbar (rectangles), even updating the screen. There are not a single error running the script.

0 Answers
Related