Editor

Move the basket

Pygame challenges · Catch, step 1 of 4 · 10 XP

The basket is stuck: the arrow keys do nothing.

Your task

The basket's place on the screen is x, the position of its left edge. Every frame the game asks pygame which arrow keys are held down and hands the answers to your function as left and right, each True or False.

Write move_basket(x, left, right, speed, limit) and return the new x:

  • while left is True, take speed away from x; while right is True, add speed (holding both, they cancel out);
  • then keep the basket on the screen: x may not go below 0 or above limit.

The screen is 480 pixels wide and the basket 80, so the game passes 480 - 80 = 400 as limit, and 8 as speed: eight pixels a frame, sixty frames a second. Press Space to start a round: catch 20 fruit in 30 seconds.

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
right arrow: 6 to the rightmove_basket(200, False, True, 6, 400)206
left arrow: 6 to the leftmove_basket(200, True, False, 6, 400)194
no arrow: it staysmove_basket(200, False, False, 6, 400)200
both arrows cancel outmove_basket(200, True, True, 6, 400)200
it stops at the left edgemove_basket(3, True, False, 6, 400)0
it stops at the right edgemove_basket(398, False, True, 6, 400)400