Pygame object's previous images after moving still showing even though its been drawn over

Viewed 34

So I'm trying to make it so that every second a new material is created at the coordinates of all the producers which is being done by the change event and my producer list but the materials when moving the previous position still remains even though I've blit the grid png. Apologies for bad spag

class Producer(pygame.sprite.Sprite):

    def __init__(self,x,y):

        super().__init__()
 
               self.image=pygame.image.load('images/producer.png').convert_alpha()

        self.image=pygame.transform.rotate(self.image,90)

        self.image=pygame.transform.scale(self.image, (40, 40))

        self.rect=self.image.get_rect(topleft=(x,y))


    def update(self):

        producer_group.draw(screen)


    def create_material(self,co):
        return Material(co)


class Material(pygame.sprite.Sprite):

    def __init__(self,co):

        global producer_info

        super().__init__()

        self.image=pygame.Surface((10,10))

        self.image.fill((255,0,0 ))

        self.rect=self.image.get_rect(center=co)


    def update(self):

        x,y=co[0],co[1]

        decimal_co=str(x)+'.'+str(y)

        this_producer_info=producer_info.get(decimal_co)

        self.rect.y-=5

        print('material moving')

in the game loop

for event in pygame.event.get():

    if event.type==pygame.QUIT:

        pygame.quit()

        exit()

    if event.type ==change_event:

        if game_state=='play':

            screen.blit(grid_surface,(0,100))

            producer_cos=list(producer_info.keys())

            for x in producer_cos:

                co = x.split('.')

                co[0]=int(co[0])

                co[1]=int(co[1])

                print(co)


                material_group.add(Producer.create_material('self',co))

play game state

elif game_state=='play':

    screen.blit(grid_surface,(0,100))

    #buttons

    settings_mini_button.draw()

    shop_button.draw()

    edit_button.draw()

    blueprints_button.draw()

    map_button.draw()


    #machine stuff

    producer_group.draw(grid_surface)

    producer_group.update()

    crafter_group.draw(grid_surface)

    #producer_group.update(grid_surface)

    #materials

    material_group.draw(grid_surface)

    material_group.update()

    screen.blit(grid_surface,(0,100))


    #copy screen

    play_bg=screen.copy()
1 Answers

The scene must be redrawn in each frame. You do not want to permanently change the grid_surface. Either you have to draw the objects on the screen instead of the grid_surface or you have to copy the grid_surface in each frame and draw on the copy of the grid_surface instead of the original:

elif game_state=='play':

    producer_group.update()
    material_group.update()

    # clear the disaply
    screen.fill(0)

    # draw all things on the screen
    settings_mini_button.draw()
    shop_button.draw()
    edit_button.draw()
    blueprints_button.draw()
    map_button.draw()

    # copy grid_surface and draw objects on the copy
    copy_of_grid_surface = grid_surface.copy()
    producer_group.draw(copy_of_grid_surface)
    crafter_group.draw(copy_of_grid_surface)
    material_group.draw(copy_of_grid_surface)

    # blit copy_of_grid_surface on the screen
    screen.blit(copy_of_grid_surface, (0,100))
Related