Python: * Animate two faces moving in different directions at the same time in a
ID: 3847491 • Letter: P
Question
Python:
* Animate two faces moving in different directions at the same time in a program move2Faces.py. You cannot use the moveAllOnLine function. You will have to make a variation of your own. You can use the moveAll function separately for each face. Hint: imagine the old way of making an animated cartoon. If each face was on a separate piece of paper, and you wanted to animate them moving together, you would place them separately, record one frame, move them each a bit toward each other, record another frame, move each another bit toward each other, record another frame, .... In our animations “record a frame” is replaced by a short sleep to make the position visible to the user. Make a loop to incorporate the repetition of the moves.
Explanation / Answer
Added extra code to the backAndForth3,py to work as expected..
from graphics import *
import time
def moveAll(shapeList, dx, dy):
''' Move all shapes in shapeList by (dx, dy).'''
for shape in shapeList:
shape.move(dx, dy)
def moveAllOnLine(shapeList, dx, dy, repetitions, delay):
'''Animate the shapes in shapeList along a line.
Move by (dx, dy) each time.
Repeat the specified number of repetitions.
Have the specified delay (in seconds) after each repeat.
'''
for i in range(repetitions):
moveAll(shapeList, dx, dy)
time.sleep(delay)
def moveFaceInGivenDirection(shapeList, dx, dy, repetitions, delay):
''' Move all shapes in shapeList by (dx, dy).'''
for i in range(repetitions):
moveAll(shapeList, dx, dy)
time.sleep(delay)
def makeFace(center, win):
'''display face centered at center in window win.
Return a list of the shapes in the face.
'''
head = Circle(center, 25)
head.setFill("yellow")
head.draw(win)
eye1Center = center.clone() # face positions are relative to the center
eye1Center.move(-10, 5) # locate further points in relation to others
eye1 = Circle(eye1Center, 5)
eye1.setFill('blue')
eye1.draw(win)
eye2End1 = eye1Center.clone()
eye2End1.move(15, 0)
eye2End2 = eye2End1.clone()
eye2End2.move(10, 0)
eye2 = Line(eye2End1, eye2End2)
eye2.setWidth(3)
eye2.draw(win)
mouthCorner1 = center.clone()
mouthCorner1.move(-10, -10)
mouthCorner2 = mouthCorner1.clone()
mouthCorner2.move(20, -5)
mouth = Oval(mouthCorner1, mouthCorner2)
mouth.setFill("red")
mouth.draw(win)
return [head, eye1, eye2, mouth]
def main():
winWidth = 300
winHeight = 300
win = GraphWin('Back and Forth', winWidth, winHeight)
win.setCoords(0, 0, winWidth, winHeight) # make right side up coordinates!
rect = Rectangle(Point(200, 90), Point(220, 100))
rect.setFill("blue")
rect.draw(win)
faceList = makeFace(Point(40, 100), win)
faceList2 = makeFace(Point(150,125), win)
stepsAcross = 46
#moving two faces in different dorection
moveFaceInGivenDirection(faceList, 5, 0, stepsAcross, wait)
moveFaceInGivenDirection(faceList2, 7, 2, stepsAcross, wait)
Text(Point(winWidth/2, 20), 'Click anywhere to quit.').draw(win)
win.getMouse()
win.close()
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.