To install and use the graphics library, and also get experience
using random numbers.
For this lab, you will write a program which will draw random circles to the
screen. The circles will have random positions, radii, and colors.
- First you will need to download the graphics library, as described
in these instructions.
- Use this
example program that we saw last as a starting point for the lab.
It draws a red circle to the middle of the screen
- Make sure that you can run the program as it is. If not double-check that the
graphics.py library is in the same folder as your program.
- Next, change it so that the circle appears at a random position on the screen.
Instead of being fixed at (200, 200), you should make the X and Y coordinates
random numbers between 0 and 400. Use a different random number for each.
- Next, make it so that the radius is also chosen randomly. It should be set
to a random number between 5 and 200.
- Finally, make the color randomly chosen as one of three options. You can
choose the three colors you want from this
list. To choose an option randomly, get a random number from 1 to 3. Then use
if statements to set the color based on which random number was chosen.
When you run the program, it should now make the circle show up in
different places, with a different size and color (from the three you picked).
Sometimes most of the circle will be off the screen, which is OK.
You will have to re-run the program for it to reset.
For this lab, there is a chance for extra credit. To get it, you should
modify the program so that it draws a new random circle each time the mouse is
clicked inside the window.
When your program is finished, email the .py file to email@example.com.
Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.