Random.randient is Showing error when called in a Function

Question:

hey I am making a game in pygame I made a function in pygame that if bg_object is smaller than equal to the cap of the number objects allowed , I implemented this quickly, but I ran into a problem , the problem is whenever I try to blit a image in screen it is showing an error , I know to put it in a class for multiple objects in a background but I am testing this

Here is the Function:


# Backround Cosmetics
FlowerPos = [random.randint( SCREEN_WIDTH , SCREEN_HEIGHT ) , random.randint( SCREEN_WIDTH , SCREEN_HEIGHT )]
StonePos = [random.randint( SCREEN_WIDTH , SCREEN_HEIGHT ) , random.randint( SCREEN_WIDTH , SCREEN_HEIGHT )]
Grass1Pos = [random.randint( SCREEN_WIDTH , SCREEN_HEIGHT ) , random.randint( SCREEN_WIDTH , SCREEN_HEIGHT )]
Grass2Pos = [random.randint( SCREEN_WIDTH , SCREEN_HEIGHT ) , random.randint( SCREEN_WIDTH , SCREEN_HEIGHT )]

def Backround_cosmetics():
    bg = 0
    if bg <= Backround_cosmetics_cap:
        Random_bg = (random.randint( 1 , 3 ))

        if Random_bg == 1:
            Win.blit( flower , ( FlowerPos[0] , FlowerPos[1] ))
            bg += 1

        if Random_bg == 2:
            Win.blit( stone , ( StonePos[0] , StonePos[1] ))
            bg += 1

        if Random_bg == 3:

            grass_type = random.randint( 1 , 2 )
            if grass_type == 1:
                Win.blit( grass1 , (( Grass1Pos[0] , Grass1Pos[1] )))
                bg += 1

            if grass_type == 2:
                Win.blit( grass2 , (( Grass2Pos[0] , Grass2Pos[1] )))
                bg += 1
            

I am mentioning the Function in the loop like this:

while Running:
    [...]
    Backround_cosmetics()
    [...]

Answers:

random.randint(1,3) will randomly select 1 or 2, not 3. So, that might have raised an error in your case.

Answered By: Nuri Taş

You call the function random.randint( SCREEN_WIDTH , SCREEN_HEIGHT )

But the function returns an integrer between the first and the second variable.

random.randint(0, 10) would return a number between 0 and 10.

You probably want to call random.randint(0 , SCREEN_WIDTH) and random.randint(0 , SCREEN_HEIGHT).

Answered By: Ome
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.