Python Animation

This was the code I wrote for an animation that moved sprites along a path of colored lines a player could draw. I wrote this in 2011 and enjoyed the introduction to programming it gave me, but I haven’t used Python again since then.

>>> #————————————————————————-#
# This is to find all the red locations in all the different red #
# pictures. I needed them all in one list, rather than a tuple, thus #
# the For loops. Then I found out I could do the same thing by drawing #
# a red line and using all the red pixels in that line rather than a #
# bunch of different pictures #
#————————————————————————-#
##AllRedPositions1 = []
##for var in redloc1:
## AllRedPositions1.append(var)
##for var in redloc2:
## AllRedPositions1.append(var)
##for var in redloc3:
## AllRedPositions1.append(var)
##for var in redloc4:
## AllRedPositions1.append(var)
##
##AllRedPositions2 = []
##for var in redloc5:
## AllRedPositions2.append(var)
##for var in redloc6:
## AllRedPositions2.append(var)
##for var in redloc7:
## AllRedPositions2.append(var)
##
##AllRedPositions = (redloc1[0], redloc2[0], redloc3[0], redloc4[0], redloc5[0], redloc6[0], redloc7[0])
#————————————————————————-#
# greenColor(picture) finds the (x,y) of the green pixels in picture #
# I made this before figuring out that I could just add this to the #
# “redColor” function, which is now called “colorPositions” #
#————————————————————————-#
##def greenColor(picture):
## W = getWidth(picture)
## H = getHeight(picture)
## green_positions = []
## for x in range(W):
## for y in range(H):
## r,g,b = picture[x,y]
## color = Color(r,g,b)
## green = Color(0,255,0)
## if color.distance(green) < 100:
## green_positions.append((x,y))
## return green_positions
##
##
##greencurves = greenColor(redgreen)
##
##AllGreenPositions1 = greencurves[0:25]
##AllGreenPositions2 = greencurves[25:62]
###————————-Animate fish————————————#
# I made this in the process of trying to figure out how to animate #
# the fish without making it animate for each “position in positions” #
# for the drawSun function. But then I got that. #
#—————————————————————————#
##def fishAnimation():
## frames = []
## for position in greenloc1:
## frame = duplicatePicture(ocean)
## drawFish(frame, position[0], position[1])
## frames.append(frame)
## return frames
#———————Real Code——————————————#
import math
import os
import glob
import time
#————————-Colors——————————————#
yellow = makeColor(255,255,0)
red = makeColor(255,0,0)
green = makeColor(0,255,0)
blue = makeColor(0,0,255)
skin = makeColor(239, 202, 146)
darkyellow = makeColor(225, 179, 17)
watercolor = makeColor(0, 128, 131)
gray = makeColor(83,84,71)
fish = makeColor(168, 18, 175)
black = makeColor(0,0,0)
#custom_Color = pickAColor()
#color_distance = distance(color1, color2)
##
###————————–Sun Parameters———————————#
sunWidth = 150
sunHeight = 130
sunXOffset = sunWidth/2
#————————–Imported Images——————————–#
water = makePicture(“water.jpg”) #cartoon ocean
shark = makePicture(‘shark2.jpg’) #shark jumping
happyshark = makePicture(‘sharkshut.jpg’) #shark smiling
ocean = makePicture(‘oceanafter.jpg’) #empty ocean
field = makePicture(‘newfield.bmp’) #field
newfield = makePicture(‘newfield2.bmp’) #final field
#—————————-Red/Green Images—————————-#
# These dots1-dots7 became useless after I figured out drawing the #
# red and green lines #
#————————————————————————#
dots1 = makePicture(“redgreen1.bmp”)
dots2 = makePicture(“redgreen2.bmp”)
dots3 = makePicture(“redgreen3.bmp”)
dots4 = makePicture(“redgreen4.bmp”)
dots5 = makePicture(“redgreen5.bmp”)
dots6 = makePicture(“redgreen6.bmp”)
dots7 = makePicture(“redgreen7.bmp”)
redgreen = makePicture(“redgreen8.bmp”)
#——————————————————————-#
# getDotPictures() retrieves all the files containing #
# “redgreen*.bmp” #
#——————————————————————-#
def getDotPictures():
mpath = getMediaPath()
dotFiles = glob.glob(os.path.join(mpath,’redgreen*.bmp’))
dotFileNumbers = []
for i,fname in enumerate(dotFiles):
num = int(fname[len(mpath)+8:-4])
dotFileNumbers.append(num)
dotFileNumbers.sort()
dotPictures = []
for dfn in dotFileNumbers:
dotPicture = makePicture(‘redgreen%s.bmp’%(str(dfn)))
dotPictures.append(dotPicture)
return dotPictures
allRedDotPictures = getDotPictures()
#——————————————————————-#
# colorPositions(picture) finds the (x,y) of the red and green #
# pixels in picture #
#——————————————————————-#
red_positions = []
green_positions = []
blue_positions = []
black_positions = []
def colorPositions(picture):
W = getWidth(picture)
H = getHeight(picture)
for x in range(W):
for y in range(H):
r,g,b = picture[x,y]
color = Color(r,g,b)
red = Color(255,0,0)
if color.distance(red) < 100:
red_positions.append((x-sunXOffset,y))
if color.distance(green) < 100:
green_positions.append((x,y))
if color.distance(blue) < 100:
blue_positions.append((x,y))
if color.distance(black) < 100:
black_positions.append((x,y))

#——————————————————————-#
# The variables for the separated color positions #
#——————————————————————-#

allpositions = colorPositions(redgreen)
redloc1 = red_positions[0:22]
redloc2 = red_positions[22:45]
greenloc1 = green_positions[0:22]
greenloc2 = green_positions[22:45]
blueloc = blue_positions[:]
blackloc = black_positions[:]
#—————————-Sun Sprite———————————–#
def drawSun(picture, x, y):
#x,y should change, but for now:
Width = sunWidth
Height = sunHeight
addOvalFilled(picture, x, y, Width, Height, darkyellow)
addLine(picture, x-10, y+80, x-110, y+80, darkyellow)
addLine(picture, x, y+20, x-100, y-25, darkyellow)
addLine(picture, x+15, y+133, x-60, y+208, darkyellow)
addLine(picture, x+90, y+145, x+90, y+235, darkyellow)
addLine(picture, x+140, y+115, x+210, y+185, darkyellow)
addLine(picture, x+161, y+74, x+250, y+74, darkyellow)
addLine(picture, x+157, y+22, x+212, y-21, darkyellow)
addLine(picture, x+100, y-4, x+118, y-25, darkyellow)
addLine(picture, x+45, y-4, x+25, y-25, darkyellow)
#————————————————————————-#
#—————————-Fish—————————————–#
def drawFish(picture, x, y):
W = 43
H = 45
addOvalFilled(picture, x, y, W, H, fish)
addArc(picture, x+10, y+10, 30, 30, 40, 115, color=(0,0,0))
addOvalFilled(picture, x+35, y+18, 4, 4, color=(0,0,0))
addOvalFilled(picture, x+28, y+18, 4, 4, color=(0,0,0))
addLine(picture, x+5, y+5, x-20, y-20, fish)
addLine(picture, x-20, y-20, x-20, y+40, fish)
addLine(picture, x-20, y+40, x+5, y+28, fish)
addLine(picture, x-15, y-15, x-15, y+37, fish)
addLine(picture, x-11, y-11, x-10, y+35, fish)
addLine(picture, x-5, y-5, x-5, y+33, fish)
addLine(picture, x-3, y-2, x-3, y+30, fish)
#———————–Ball———————————————–#
def drawBall(picture, x, y):
W = 20
H = 20
addOvalFilled(picture, x, y, W, H, color=(0, 6, 116))
#———————–Ghost Dog/Bird—————————————#
def drawGhost(picture, x, y):
W = 40
H = 53
addOvalFilled(picture, x, y, W, H, color=(255,255,255))
addOvalFilled(picture, x+25, y, 25, 21, color=(255,255,255))
addOvalFilled(picture, x+40, y+5, 3, 3, color=(0,0,0))
addLine(picture, x+12, y+53, x-7, y+60, color=(0,0,0))
addLine(picture, x+12, y+53, x+20, y+60, color=(0,0,0))
addLine(picture, x+25, y+53, x+25, y+65, color=(0,0,0))
addLine(picture, x+25, y+53, x+13, y+65, color=(0,0,0))
#————————————————————————#

def makeEmptyCopy(picture):
W = getWidth(picture)
H = getHeight(picture)
empty = makeEmptyPicture(W,H)
return empty

def duplicatePicture(picture):
W = getWidth(picture)
H = getHeight(picture)
empty = makeEmptyPicture(W,H)
target = empty
for x in range(W):
for y in range(H):
r,g,b = picture[x,y]
target[x,y] = r,g,b
return target
def makeFrame(W,H):
frame = makeEmptyPicture(W,H)
return frame
#———————Animates the Sun—————————————#
def erinsAnimation():
frames = []
for position in range(22):
frame = duplicatePicture(ocean)
drawSun(frame, redloc1[position][0]-60, redloc1[position][1]-128)
if greenloc1[position][1] < 147:
drawFish(frame, greenloc1[position][0], greenloc1[position][1])
frames.append(frame)
for position in range(19):
frame = duplicatePicture(shark)
drawSun(frame, redloc2[position][0]-60, redloc2[position][1]-128)
if greenloc2[position][1] < 147:
drawFish(frame, greenloc2[position][0], greenloc2[position][1])
frames.append(frame)
frames.append(happyshark)
frames.append(happyshark)
frames.append(ocean)
return frames
def secondAnimation():
frames = []
for position in range(65):
frame = duplicatePicture(field)
drawBall(frame, blueloc[position][0], blueloc[position][1])
drawGhost(frame, blackloc[position][0]-50, blackloc[position][1]+95)
frames.append(frame)
frames.append(newfield)
return frames
#——————————————————————————–#
# Above: #
# imports colors Sun parameters #
# Images drawSun(pic,x,y) makeEmptyCopy(pic)#
# duplicatePicture(pic) makeFrame(W,H) getDotPictures() #
# allreds redCOlor() AllRedPositions #
# draw_line(pic, x, color) SunAnimation(pic, positions) #
# drawFish(pic,x,y) #
# #
#——————————————————————————–#

 

Advertisements