pythonpython-3.xpygamepygame2

Pygame colliderect() function returning 0 (zeros) and 1 (ones) instead of True and False?


colliderect() function won't return True and False; instead, it is returning 0 and 1. When I use the function type it says int instead of bool. If I code using 0 and 1 instead of the boolean value the code works, but why is it happening? Pygame documentation doesn't mention anything about it.

def collisions():
    tileHitsList = []
    for rect in tileList:
        print(testcube.colliderect(rect)) #it weirdly prints 0 and 1 instead of False and True

        #tileList is a list which contains the rects of the tiles being rendering on the display
        #testcube is a rect that hits the tiles

        if testcube.colliderect(rect) == True:
            tileHitsList.append(rect)
    return tileHitsList

Solution

  • It's pretty normal if the pygame documentation didn't say anything about it, as 1 and 0 are very commonly used to replace True and False.

    You can just do

    if testcube.colliderect(rect):
       # Your code
    

    without the ==.

    Here is a documentation on the matter: https://docs.python.org/3/reference/datamodel.html#index-10