9.36: Checking if Outside the Active Area
- Page ID
def isOutsideActiveArea(camerax, cameray, obj): # Return False if camerax and cameray are more than # a half-window length beyond the edge of the window. boundsLeftEdge = camerax - WINWIDTH boundsTopEdge = cameray - WINHEIGHT boundsRect = pygame.Rect(boundsLeftEdge, boundsTopEdge, WINWIDTH * 3, WINHEIGHT * 3) objRect = pygame.Rect(obj['x'], obj['y'], obj['width'], obj['height']) return not boundsRect.colliderect(objRect) if __name__ == '__main__': main()
isOutsideActiveArea() will return
True if the object you pass it is outside of the "active area" that is dictated by the
cameray parameters. Remember that the active area is an area around the camera view the size of the camera view (which has a width and height set by
WINHEIGHT), like this:
We can create a Rect object that represents the active area by passing
camerax - WINWIDTH for the left edge value and
cameray - WINHEIGHT for the top edge value, and then
WINWIDTH * 3 and
WINHEIGHT * 3 for the width and height. Once we have the active area represented as a Rect object, we can use the
colliderect() method to determine if the object in the
obj parameter is collides with (that is, is inside of) the active area Rect object.
Since the player squirrel, enemy squirrel and grass objects all have
'height' keys, the
isOutsideActiveArea() code can work with any type of those game objects.
Finally, after all the functions have been defined, the program will run the
main() function and start the game.