Graphics Lab
This is a lab activity, not an assignment. Therefore, it's not required that you complete it or hand it in. However, the concepts in this lab will be useful for completing the homework assignments (and will be useful for exams down the road), so I'd encourage you to put in a serious effort on it in class, and to consider finishing any remaining parts outside of class.
Goals: Practice using the graphics.py package and object-oriented programming.
Setup
Log into a lab computer, mount COURSES, and navigate to your STUWORK directory. Look back at the lab from the first day of class or get help from Anna or Rachel if you need a reminder on how to complete these steps.
Create a folder in your STUWORK directory called graphicsLab. Open VSCode, and drag the graphicsLab folder
into the VSCode window. Download the
starter code
and move all files into your new folder.
The download should include three Python files, graphics.py, graphicsDemo.py, and shrinkingCircle.py.
You'll want to have the documentation for the graphics library available as you work on this lab. You can find the documentation at https://mcsp.wartburg.edu/zelle/python/graphics/graphics/.
Part 1: Get familiar with the code
- First, read through the code in
shrinkingCircle.py. Try to get a sense of what it might be doing, overall, and what the role of each line is. - Then, run the code by typing
python3 shrinkingCircle.pyin the terminal. Keep clicking on the graphics window until it closes. - Go back to
shrinkingCircle.pyand see if you have a better understanding now of what it's doing.
Part 2: write shrinkingCirclePair
Create a new class called shrinkingCirclePair. This class should create two circles in the graphics window and alternate
which one shrinks when you click the screen.
You may find it helpful to use (and modify) shrinkingCircle in your new class definition, feel free to do that if you like.
Part 3: create a button
Create a new class Button. Its constructor should create a Rectangle (using the graphics library's Rectangle object)
at some position on the screen. Add some code in main to test out your class.
Then add the following methods to the Button class:
changeColor(self)should, when called, change the color of the button. For simplicity, you can just choose two different colors to have the button alternate between. In this method, you'll need to "undraw" the old button and redraw it again with a different color. Look at the documentation if you're unsure how to do this!isClicked(self, point)should, when called, take in aPoint(presumably one that was returned from a mouse click) and then return True if the click was somewhere on the button and False otherwise.
Update your main function to use these methods to change the color of the button only when it is clicked
on using the mouse.
Extra time?
See what else you can do by looking at the graphics documentation and trying things out. Or, you
can sketch out a picture and try to draw it using graphics.py (but I recommend still looking
at the documentation so that you're learning new things, rather than just drawing Rectangles and Circles).
Anna's acknowledgements
This lab was adapted from a lab used by Layla Oesper. Thanks for sharing!