4

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
Red
  • 26,798
  • 7
  • 36
  • 58
  • 1
    **True == value > 0 or value < 0** and **False == 0** so you are getting those results. – Spiros Gkogkas Mar 22 '21 at 02:12
  • Found it weird cause the documentation doesn't mention anything about returning int values, I'll just use it like that then, ty. – César Machado Mar 22 '21 at 02:22
  • 3
    `if X == True:` is almost never the right thing to say, anyway. Just use `if X:` (and likewise `if not X:` instead of an explicit comparison to `False`). – jasonharper Mar 22 '21 at 02:59

1 Answers1

6

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

Red
  • 26,798
  • 7
  • 36
  • 58