pythonpython-3.xclasspygameblit

The Objects placed in the background are flickering every tick instead of being placed at one place pygame pyhton


I am working on a game using pygame , I filled the background with a color and I want the background not to be boring and empty so i made some art and loaded it into the code using pygame.image.load() Function , now i want them to be placed at random places , I made a class for that , that looks like this:


class Backround_Cosmatics():
    def __init__( self , bg ):
        self.bg = bg
        self.Random_bg = random.randint( 1 , 3 )

    def DrawCosmatics(self , win):

            if self.Random_bg == 1:
                win.blit( flower , ( random.randint( 0 , SCREEN_WIDTH ) , random.randint( 0 , SCREEN_HEIGHT ) ))
                self.bg += 1

            if self.Random_bg == 2:
                win.blit( stone , ( random.randint( 0 , SCREEN_WIDTH ) , random.randint( 0 , SCREEN_HEIGHT ) ))
                self.bg += 1

            if self.Random_bg == 3:

                grass_type = random.randint( 1 , 2 )
                if grass_type == 1:
                    win.blit( grass1 , (( random.randint( 0 , SCREEN_WIDTH ) , random.randint( 0 , SCREEN_HEIGHT ) )))
                    self.bg += 1

                if grass_type == 2:
                    win.blit( grass2 , (( random.randint( 0 , SCREEN_WIDTH ) , random.randint( 0 , SCREEN_HEIGHT ) )))
                    self.bg += 1

in the init method I am defining what to place (stone or flower or grass)

in the drawcosmatics methods I am saying where to place them randomly grass are 2 types

In the while loop i am mentioning it like this:


    if len(Backround_cosmatics_list) <= Backround_cosmetics_cap:
        cosmatic = Backround_Cosmatics(0)
        Backround_cosmatics_list.append(cosmatic)

    for cosmatic in Backround_cosmatics_list:
        cosmatic.DrawCosmatics( Win )

I have a cap and a list like this outside the loop:

Backround_cosmetics_cap = 15
Backround_cosmatics_list = []

but when I run the function the background objects are just changing places every tick


Solution

  • Set random self.x, self.y and self.grass_type attributes in the constructor of Backround_Cosmatics. Use the attributes to draw the background:

    class Backround_Cosmatics():
        def __init__( self , bg ):
            self.bg = bg
            self.Random_bg = random.randint(1, 3)
            self.grass_type = random.randint(1 , 2) 
            self.x = random.randint(0, SCREEN_WIDTH)
            self.y = random.randint(0, SCREEN_HEIGHT)   
    
            if self.Random_bg == 1:
                self.image = flower
            elif self.Random_bg == 2:
                self.image = stone
            elif self.grass_type == 1:
                self.image = grass1
            else:
                self.image = grass2
    
        def DrawCosmatics(self, win):
            win.blit(self.image, (self.x, self.y))
            self.bg += 1
    

    Note that DrawCosmatics is executed in every frame, but the constructor (__init__) is executed only once. Thus, each attribute set in DrawCosmatics changes permanently and each attribute set in the constructor keeps its state until the end.