9.17: Removing the Far Away Grass and Squirrel Objects
- Page ID
# go through all the objects and see if any need to be deleted. for i in range(len(grassObjs) - 1, -1, -1): if isOutsideActiveArea(camerax, cameray, grassObjs[i]): del grassObjs[i] for i in range(len(squirrelObjs) - 1, -1, -1): if isOutsideActiveArea(camerax, cameray, squirrelObjs[i]): del squirrelObjs[i]
During each iteration of the game loop, the code will check all of the grass and enemy squirrel objects to see if they are outside the "active area". The
isOutsideActiveArea() function takes the current coordinates of the camera (which are stored in
cameray) and the grass/enemy squirrel object, and returns
True if the object is not located in the active area.
If this is the case, this object is deleted on line 4  (for grass objects) or line 7  (for squirrel objects). This is how squirrel and grass objects get deleted when the player moves far enough away from them (or when the enemy squirrels move away far enough from the player). This ensures that there is always a number of squirrels and grass objects near the player.