Editor

Bounce off the walls

Pygame challenges · Bounce, step 1 of 3 · 10 XP

The balls fly off the screen and never come back.

Your task

Write bounce(position, speed, low, high). It moves position by speed. If the new position is below low or above high, put it back on that edge and reverse the speed. Return the new position and speed.

The game calls it twice per ball each frame: once across (x) and once down (y).

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
moves by its speedbounce(10, 3, 0, 100)(13, 3)
moves backwards toobounce(50, -4, 0, 100)(46, -4)
turns at the high endbounce(98, 5, 0, 100)(100, -5)
turns at the low endbounce(2, -5, 0, 100)(0, 5)
exactly on the edge is still insidebounce(97, 3, 0, 100)(100, 3)