The objective of this lab is to become familiar with using arrays, color functions, and sounds in animations.
#---------------------------------------------------------------------- #playMovie - Play a list of pictures as a movie. #movie - A list/array of pictures. #frameRate - The number of frames that should be displayed per second. # Higher numbers will result in faster animations. #---------------------------------------------------------------------- def playMovie(movie, frameRate): import time canvas = duplicatePicture(movie) toSleep = max(1.0/frameRate, 0) for index in range(len(movie)): if isinstance(movie[index], Picture): copyPictureInto(movie[index],canvas,0,0) repaint(canvas) time.sleep(toSleep)
playMoviefunction into your .py file, and copy the function copyPictureInto from the Support Code, and test
playMovieby setting up a short movie by hand. Just create an array that contains three pictures that are the same size:
>>>pics =  * 3 #notation for creating an array containing three 0's >>>pics = makePicture(pickAFile()) >>>pics = makePicture(pickAFile()) >>>pics = makePicture(pickAFile())Try playing your very short movie with different values for the frame rate.
playMovie. (The explanation of what is happening is below.)
def moveRectInLine(): canvas = makeEmptyPicture(300,300, red) pict = duplicatePicture(canvas) rectSize = 20 # Set up an array and initialize it to all zeros picArray =  * getWidth(pict) count = 0 # Create the new pictures and save them in the array for x in range(0, getWidth(canvas) - rectSize, 10): addRectFilled(canvas, x, 10, rectSize, rectSize, white) copyOfCanvas = duplicatePicture(canvas) picArray[count] = copyOfCanvas addRectFilled(canvas, x, 10, rectSize, rectSize, red) count = count + 1 return picArray
picArray, to be all zeros, and I chose to make the array
getWidth(pict)elements long. (I chose this value because that is an upper limit on how many times the box might move horizontally.)
countwhich is used to keep track of how many pictures have been added to the array.
slowFadeOut, to JES. This function saves the new pictures in an array, and then returns the array. This function takes a long time to process so you should run it on small files. You may use functions you have written previously to make your own files smaller.
# Slow Fadeout # The original background, the new background and # the original picture are the parameters. # Be sure to try this out with small pictures! #PARAMETERS: # background - a picture of an empty background. # newBackground - a picture of some other scene that will become the # new background. # person - A picture that matches background, with the addition of a # person (or other object). def slowFadeOut(background, newBackground, person): # Set up an array and initialize it to all zeros picArray =  * 10 count = 0 # Only 10 frames or it takes too long for threshold in range (1, 101, 10): canvas = duplicatePicture(person) printNow("Frame number: " + str(count)) swapbg(canvas, background, newBackground, threshold) #copy the picture into the array copyOfCanvas = duplicatePicture(canvas) picArray[count] = copyOfCanvas count = count + 1 return picArray # Swap Background Program with a threshold of closeness for colors def swapbg(person, bg, newbg, threshold): # get all pixels in the picture and their colors for x in range(getWidth(person)): for y in range(getHeight(person)): personPixel = getPixel(person, x, y) personColor = getColor(personPixel) bgpx = getPixel(bg, x, y) bgColor = getColor(bgpx) # check if the difference in color is within the threshold if (distance(personColor, bgColor) < threshold): # if so, change the color to the new background's color bgcolor = getColor(getPixel(newbg, x, y)) setColor(personPixel, bgcolor)
slowSunset, so that it creates an array of all zeros, saves the new pictures in this array, and then returns the array. You will need to copy the
makeSunsetfunction from the notes on Pictures and Loops. Modify
makeSunsetto multiply by 0.8 instead of by 0.70. When you call the function, use a small file, such as the file beach-smaller.jpg file from the MediaSources directory to save time.
def slowSunset(picture): count = 0 canvas = duplicatePicture(picture) show(canvas) # Only 10 frames or it takes too long for x in range (1, 101, 10): printNow("Frame number: " + str(count)) canvas = makeSunset(canvas) repaint(canvas) count = count + 1
playMoviefunction that takes a sound as well as an array of pictures as parameters. Your modified function should play the sound during the animation. If the sound is shorter than the animation, it should be repeated as often as necessary. You will need to figure out the duration of the sound, and use the time.clock() function to determine whether or not enough time has passed to play the sound again.