Why are multiple copies of my player appearing when its moving

Question:

When I make my character/player jump, a copy image of the player stays on the jumpY and the same is happening for when I shoot a health kit. (long story) My background scroll works but the copy thing is really bothering me. I have no idea why it’s happening. This is an image of what happens.
what happens when the player jumps.

What happens when the player shoots a health kit

I really don’t know how this happened. Here is the code:

import pygame from pygame import mixer
#Imports the pygame module. 

pygame.init()
#Initializes Pygame

screen = pygame.display.set_mode((800,600))
#Sets the screen to pygame looks and not normal python looks.

pygame.display.set_caption("Draft")
#Changes the title

icon = pygame.image.load('doctor.png') pygame.display.set_icon(icon)
#Changing the Icon

white = (255,255,255) black = (0,0,0)  red = (255,0,0) green = (0,255,0) blue = (0,0,255)
#Setting color variables to make it easier to access later in the code.

#Player player_img = pygame.image.load('Doctor_Running-removebg-preview (1).png') playerx = 20 playery = 390 playerx_change = 0 playery_change = 100
#Making the Player Variables. 

#Health Kit HealthImg = pygame.image.load('first-aid-kit.png') HealthX = 20 HealthY = 405 HealthX_Change = -10 HealthY_Change = 0 Health_State = "ready"
#Making the Health_Kit Variables. 


#Create a white screen  
DISPLAYSURF = pygame.display.set_mode((800,600))
DISPLAYSURF.fill(blue)
pygame.display.set_caption("Game")

#Creating Events

class Background():
      def __init__(self):
            self.bgimage = pygame.image.load('Ground.png')
            self.rectBGimg = self.bgimage.get_rect()
 
            self.bgY1 = 485
            self.bgX1 = 0
 
            self.bgY2 = 485
            self.bgX2 = self.rectBGimg.width
 
            self.moving_speed = 5
         
      def update(self):
        self.bgX1 -= self.moving_speed
        self.bgX2 -= self.moving_speed

        if self.bgX1 <= -self.rectBGimg.width:
            self.bgX1 = self.rectBGimg.width
        if self.bgX2 <= -self.rectBGimg.width:
            self.bgX2 = self.rectBGimg.width
             
      def render(self):
         DISPLAYSURF.blit(self.bgimage, (self.bgX1, self.bgY1))
         DISPLAYSURF.blit(self.bgimage, (self.bgX2, self.bgY2))


class Player():
    def draw_player():
        screen.blit(player_img,(playerx,playery))

class Health():
    def fire_Health (x,y):
        global Health_State
        Health_State = "fire"
        screen.blit(HealthImg, ( x + 70, y + 10))


#def states an event. The event will not occur unless you call the event in the main game loop. back_ground = Background()

#Main Game Loop clock = pygame.time.Clock()

SCEEN_UPDATE = pygame.USEREVENT pygame.time.set_timer(SCEEN_UPDATE,150)


running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
               playery += playery_change

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
               playery -= playery_change

            if event.key == pygame.K_SPACE:
                if Health_State == "ready":
                    Health_Fire_Sound = mixer.Sound('laser.wav')
                    Health_Fire_Sound.play()
                    Health.fire_Health(HealthX , HealthY)
                    HealthY  = playery + 10


    back_ground.update()
    back_ground.render()
 

    #Health Kit Movement
    if HealthX >= 600:
        HealthX = 20
        Health_State = "ready"

    if Health_State == "fire":
        Health.fire_Health(HealthX , HealthY)
        HealthX -= HealthX_Change

    # draw objects
    Player.draw_player()

    # update display
    pygame.display.update()
    clock.tick(60)

Answers:

The background does not cover the entire display. Therefore you have to clear the entire display in each frame:

running = True
while running:
    # [...]

    DISPLAYSURF.fill(blue)      # <-- clear display
    back_ground.update()
    back_ground.render()

    # [...]
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.