For createRectangles, did we need to return the rectangle? Wouldn’t the function have just drawn the rectangle anyways without returning the rectangle?
Yes; but the for the extension, I wanted to keep track of all the rectangles and find the biggest. If I didn’t return the Rectangle, I wouldn’t be able to find out how big it was. Take a look at the solution I posted and see how I use the returned value.
why can you change a variable (e.g. var1 in var2=var1 changes var1)?
This only happens if we mutate an object; that is, var1
and var2
share an object value. For example, if the value is a Rectangle
, we don’t have two distinct Rectangle
s but one such object with two variables that refer to it. So, since there is only one Rectangle
, a modification from var2
is also a modification to var1
.
would you use a while loop to keep making objects (circles, rectangles) if theres no definite amount that the user wants to make? If so, how would you define when to stop making shapes?
That’s a great question. We would need some kind of user signal. What are the options for getting user input from the GraphWin
documentation? One that we have used is getMouse()
. We could designate an area of the window that says “Click to Exit” and see if the Point
returned from getMouse()
was there. This is actually what you’ll do for Lab 6.
The second option is getKey()
which asks the user to enter a key on the keyboard. So you could say “Hit Escape to exit” and if getKey()
is equal to “Escape”, break the loop.
How to create rectangles with different variable names?
I’d suggest putting them into a list, just like when we asked the user to enter a bunch of numbers.
How would one create more smooth animation?
More steps and a smaller sleep time will be smoother.
How does sleep work?
Great question! I had to look this up. My area of expertise is not the operating system, but here is what I learned. sleep()
utilizes the underlying operating systems mechanism for sleeping. For example, here is how Windows does sleep. The operating system has to handle many programs running at once e.g., your browser, audio player, word processor, Slack channel, etc. If the operating system let one program run constantly, none of the other programs would get a chance to do anything. So the OS forces programs to block (or pause) so other programs get a chance. sleep()
in Python specifically asks the operating system to force a block on itself. It’s sort of like asking to be subbed out of a basketball game rather than waiting for your coach to bring you out.
How to create multiple images
We can create several GraphWin
objects in one program, and we can draw separately to them. Try this:
>>> from graphics import *
>>> win1 = GraphWin("Window 1")
>>> win2 = GraphWin("Window 2")
>>> circ = Circle(Point(25,25), 10)
>>> circ.setFill("red")
>>> circ.draw(win1)
Circle(Point(25.0, 25.0), 10.0)
>>> rect = Rectangle(Point(75,75),Point(100,100))
>>> rect.setFill("green")
>>> rect.draw(win2)
Rectangle(Point(75.0, 75.0), Point(100.0, 100.0))
Result:
How would we use loops and boolean statements to find the maximum rectangle
I’ve given the solution and added a little about in the class notes.
How do you use the final position of the circle as a starting position for another run of the program
If you mean rerunning the Python program, everything that Python keeps track of goes away when the program finishes. We can choose to “save” the result by writing it to a file - which is how most programs keep track of information. We’ll learn about reading from files next week.
I’m interested in learning more about animation.
The animation we have done is very simple, but it is the base structure for computer animation: pause the program, update one of the elements on the graphic window, repeat. We have two upper-level courses related to more advanced ideas - Graphics taught by Professor Danner and Animation by Professor Normoyl.