158k views
3 votes
Write a program that uses key events to make a circle larger and smaller.

Your circle should start at a radius of 100 and a position of 250, 250. Each time the user hits the left arrow, the circle should decrease by 10. If the user hits the right arrow, it should increase by 10.

Hint: Use the get_radius() and set_radius() method to change the size of your circle. Find more details and examples on these functions in the DOCs tab.

Challenge: Can you prevent the circle from getting smaller than a radius of 10 and larger than a radius of 400?


in python code

User Kevin Sun
by
5.9k points

1 Answer

6 votes

Answer:

circ = Circle(100)

circ.set_position(250, 250)

circ.set_color(Color.yellow)

add(circ)

def grow_circle(event):

if event.key == "ArrowLeft":

circ.set_radius(circ.get_radius() - 10)

if event.key == "ArrowRight":

circ.set_radius(circ.get_radius() + 10)

add_key_down_handler(grow_circle)

Step-by-step explanation:

By making it that when the left/right arrow is clicked the radius of the circle is first taking into account before anything else, then each time the arrow is clicked the current radius either gets 10 added or removed from it.

User Dzavala
by
6.5k points