Today's lab will focus on the interactive graphics sections of Chapter 4.

### Interactive Graphics

We will use the graphics package developed for the book. If you have not already downloaded the graphics.py, work through the directions in Lab 3.

The first program for today is click.py from the book (downloadable from the book's website). Try running the program (remember you need to have the graphics.py file in the same directory. What does it do? Let's look at the program:

• The first lines have a comment with the name of the program and ask that python load the graphics module:
```# click.py
from graphics import *

```
• These lines give the name of the function we are defining (main() and make a new variable, called win, that holds a graphics window object:
```def main():
win = GraphWin("Click Me!")

```
• Next, we have a definite loop that prints out the point the user clicked. The point is saved in a variable called p. The loop repeats 10 times. After the end of the function definition, the function main() is invoked:
```for i in range(10):
p = win.getMouse()
print("You clicked at:", p.getX(), p.getY())

main()
```

Let's modify this program in several ways:

• Allow the user to click 20 times instead of 10.
• Show the points that the user clicks on the screen.
(Hint: look in the lecture notes for how other objects are drawn on the screen. The same idea works for points.)
• Modify your program so that it will connect-the-dots that the user clicks upon.

This last one is more challenging. To draw a line you will need to remember the point from the click before. Let's make a new variable called oldPoint to save the point and use it in the loop body to draw a line:

```	p = win.getMouse()
print("You clicked at:", p.getX(), p.getY())
Line(oldPoint,p).draw(win)
oldPoint = p
```
If your loop body to this, you will get an error. What is it? What is missing? Remember you cannot use a variable until it has a value. So, before your loop, let's give oldPoint a value:
```	oldPoint = Point(0,0)
for i in range(10):
...
```
Now what happens when you run it? Where does your line start? Try it several times. Does it always start in the same place? To have it start where the user first clicked, we need to get that value before we draw the line. Here is one way to do this:
```def main():
win = GraphWin("Click Me!")
oldPoint = win.getMouse()
for i in range(10):
p = win.getMouse()
print("You clicked at:", p.getX(), p.getY())
Line(oldPoint,p).draw(win)
oldPoint = p
p.draw(win)
```

If you finish early, you may work on the programming problems.