Basic Graphics Programming

Table of Contents

This is a pair programming assignment. If you are working in a pair, this means that you and your partner should be doing the entirety of this assignment side-by-side, on a single computer, where one person is "driving" and the other is "navigating." Set a timer to swap every 15 minutes. You can choose your favorite from this online timer page. Make sure your sound volume is audible, but not so loud to disturb the people around you.

If you are working in a pair, only one of you needs to submit your work via Moodle. That said, you should both have a copy of your work in case you want it someday, so make sure that both of you have copies of it; you can email it or use some other mechanism to transfer it.

We will use anonymous grading on Moodle, which means that the grader won't see your name until after the grading is done. This is an easy way to help add an extra element of fairness to the grading. Therefore, make sure your name doesn't appear on your actual submission. When you submit via Moodle, it will know you are. Thanks!

The graphics.py module

From a terminal window, create a directory called graphicsintro to hold your work for this assignment. Change to that directory in the terminal window. Go back and look at the UNIX lab if you forget how to do that. Keep working at the command line: you'll only get better at it if you keep practicing.

Save this graphics.py file to the graphicsintro directory that you just created.

You can open up graphics.py and look at it if you'd like. However, we won't be paying much attention for a while to what's inside it. Instead, we're going to use this module in our own programs.

Now similarly grab the file myArtwork.py and open it up. Read through the code and see if you can figure out what it will do.

To run your program, type (remember, don't type the $):

$ python3 myArtwork.py

from the terminal window. What do you see? Try modifying the program to accomplish the following:

  • Make the graphics window tall and skinny. What happens to the rectangle you drew when you run your new version? Why?
  • Change the background color. Can you make it white?
  • Make the rectangle green.

Here is the documentation for graphics.py. (A link is also on the Moodle home page). Take a few minutes to skim through the documentation to familiarize yourself with what's there, and where. Then try picking out a few methods that look like they might be interesting, and try them out. Modify your program to use other methods from this documentation page.

Your Own Design

Write a Python program named myDrawing.py. Your program should create a GraphWin object and, using the methods in graphics.py, draw a picture. Spend a little bit of time experimenting with the different methods, and then figure out how to combine these methods to draw more complex shapes or pictures. Use some arithmetic operations as part of your program.

Some suggestions:

  • It's up to you to decide what you'll use arithmetic for, but one idea would be to create a "scalable" shape. For example, you might ask the user to enter how long each edge of a square should be, and the program should draw a square accordingly.
  • Combine simple shapes in more complex ways. For example, combine triangles to form a star, or circles to generate different patterns, or rectangles to create a tic-tac-toe board.
  • Add the line
    from time import *
    

    to the top of your Python program. This gives you access to the "sleep" function, which will force Python to wait a certain period of time. For example, sleep(5) will make Python wait 5 seconds. Combine the use of sleep with the undraw() methods associated which each shape to make a flashing lightshow. (If you're really feeling ambitious, you might want to figure out how you could use these methods to make it appear as if something is moving across your canvas.)

Be creative, and have fun!

What to turn in

Turn in your work with Moodle. Creativity is highly valued!

Author: Dave Musicant

Emacs 24.5.1 (Org mode 8.2.10)

Validate