Editor

Catch the fruit

Pygame challenges · Catch, step 3 of 4 · 20 XP

The fruit falls straight through the basket.

Your task

Until now pygame's colliderect decided what you caught. Now the game asks a function of your own, so you know exactly what that check does. You will need the same idea in the Platformer.

The game hands you two boxes, each a tuple (x, y, w, h): the left edge, the top edge, the width and the height. So a box's right edge is x + w and its bottom edge is y + h.

Two boxes can miss each other in only four ways. The fruit is all the way to the left of the basket, all the way to the right, all the way above it or all the way below it. If none of the four is true, they overlap.

Write caught(basket, fruit) and return True when the two boxes overlap. Boxes that only touch along an edge do not count, the same rule pygame uses.

The function is at the top of the program, marked fix this. Press Run to play the game as it is, then Check when your function is ready.

What Check tests

CallShould give
a fruit in the basketcaught((200, 282, 80, 20), (230, 280, 20, 20))True
a fruit to the left of itcaught((200, 282, 80, 20), (150, 285, 20, 20))False
a fruit still falling above itcaught((200, 282, 80, 20), (230, 200, 20, 20))False
a fruit clipping the right cornercaught((200, 282, 80, 20), (275, 265, 20, 20))True
touching the left edge is not enoughcaught((200, 282, 80, 20), (180, 285, 20, 20))False
a fruit that already fell past itcaught((200, 282, 80, 20), (230, 302, 20, 20))False