knerr cs21 notes...
back to schedule
WEEK04: computer graphics, using objects
---------------------------------------------------------------
M: getX() and getY(), color, using sleep for animations
LAB4: due next week
QUIZ 2 today!
CIRCLE SHIFT:
- start with my circleShift.py program:
cp /home/jk/inclass/circleShift.py circleShift.py
- now modify it to move the circle to wherever the user
clicks the mouse. see if you can move it 4 times, then
close the window.
here's how to get a circle's center x and y locations:
cp = circ.getCenter()
cx = cp.getX()
cy = cp.getY()
and here's how to get a user-mouse-click point:
newpt = win.getMouse()
newx = newpt.getX()
newy = newpt.getY()
COLOR...rgb = red green blue:
- try using the color_rgb function!
>>> c = Circle(p,100)
>>> c.draw(w)
>>> color = color_rgb(0,0,0) # black
>>> c.setFill(color)
>>> color = color_rgb(0,0,255) # blue
>>> c.setFill(color)
>>> color = color_rgb(0,255,0) # green
>>> c.setFill(color)
>>> color = color_rgb(0,255,145)
>>> c.setFill(color)
ANIMATION and SLEEP:
this simple animation doesn't look very good:
>>> from graphics import *
>>> w = GraphWin("hello",600,600)
>>> p = Point(100,300)
>>> c = Circle(p,50)
>>> c.draw(w)
>>> c.setFill("blue")
>>> c.move(5,0)
>>> for i in range(200):
... c.move(1,0)
try adding the sleep command to slow down the animation:
>>> from time import sleep
>>> for i in range(200):
... c.move(1,0)
... sleep(0.05)
** see if you can write these programs:
- start with a circle, then ask the user to click
somewhere in the window, then animate the circle
to where the user clicked
NOTE: there are two ways to try this:
1. wherever they click, use a fixed number of steps
(e.g., 10) to get from where the circle is to where
the user clicked
2. wherever they click, use a fixed amount to move and
calculate the total steps needed based on the distance
from the current circle to where the user clicked
- start with a circle and animate it's motion in one direction.
if the circle gets to the edge of the graphics window, make
the circle start moving in the opposite direction.
- same as the last one, but allow motion in both the x and y directions! :)