Skip to content

Instantly share code, notes, and snippets.

@ninadpchaudhari
Created December 9, 2024 23:46
Show Gist options
  • Save ninadpchaudhari/ef4762481dbac1bae5fc7382f44a9cef to your computer and use it in GitHub Desktop.
Save ninadpchaudhari/ef4762481dbac1bae5fc7382f44a9cef to your computer and use it in GitHub Desktop.
import requests
from math import *
from JES import *
# Ahyaan Malik - Animation Project - CSIS 110 - 12/4/2024
# Create an animation using exactly 1 for loop
# BONUS - I decided to make a music video out of this, with cool effects and many, MANY moving objects
# BONUS - MANY if statements were used, keep scrolling if you like that, stop scrolling if you don't
# BONUS - Many measures were taken to ensure correct syncing of the song, guessing won't cut it!
# BONUS - Created 3 extra functions, a copyInto function that removes the white off jpgs, a function to increase the r g b values of any pic by any amount, and a way to detect a certain color and change that color, for rainbow text.
# The blob was also created to bounce up and down as it goes, he'll be back soon
# BEWARE THE EXECUTION SPEED - Ya, it's very slow.
# VIEW THE MP4 - Since I had to speed up the song by 2.5%, I've created 2 mp4s, one with audio pitched up, and one without pitched up. Choose the one you like best.
# I spent like 20-25 hours on this... ow
# :3
def incValues(pic, r, g, b): # Increases the allocated r g b values
for p in getPixels(pic):
setRed(p, r*getRed(p))
setGreen(p, g*getGreen(p))
setBlue(p, b*getBlue(p))
# Made memories we knew would never fade
def copyIntoWithBackgroundRemover(pic, canvas, stX, stY, amount): # Sets all the white pixels of a jpg image to the canvas, giving the illusion of a png
for x in range(0, getWidth(pic)):
for y in range(0, getHeight(pic)):
picP = getPixel(pic, x, y)
picColor = getColor(picP)
newX = stX + x
newY = stY + y
if (newX < getWidth(canvas)) and (newY < getHeight(canvas)):
canvasP = getPixel(canvas, newX, newY)
if (distance(picColor, white)) < amount:
canvasColor = getColor(canvasP)
setColor(canvasP, canvasColor)
else:
setColor(canvasP, picColor)
def rainbowText(pic, r, g, b, color, amount): # Detects a color and makes it to any color, used to make certain words rainbow
for p in getPixels(pic):
if distance(getColor(p), color) < amount:
setRed(p, r)
setGreen(p, g)
setBlue(p, b)
def createAnimation(): # The main function, outputs a gif of the animation
bg1 = makePicture("assets//backgrounds//bg1.jpg") # All backgrounds
bg2 = makePicture("assets//backgrounds//bg2.jpg")
bg3 = makePicture("assets//backgrounds//bg3.jpg")
bg4 = makePicture("assets//backgrounds//bg4.jpg")
bg5 = makePicture("assets//backgrounds//bg5.jpg")
bg6 = makePicture("assets//backgrounds//bg6.jpg")
bg7 = makePicture("assets//backgrounds//bg7.jpg")
wordDont = makePicture("assets//words//DONT.jpg") # All words
wordStop = makePicture("assets//words//STOP.jpg")
wordMake = makePicture("assets//words//MAKE.jpg")
wordIt = makePicture("assets//words//IT.jpg")
wordPop = makePicture("assets//words//POP.jpg")
wordDJ = makePicture("assets//words//DJ.jpg")
wordBlow = makePicture("assets//words//BLOW.jpg")
wordMy = makePicture("assets//words//MY.jpg")
wordSpeakers = makePicture("assets//words//SPEAKERS.jpg")
wordUp = makePicture("assets//words//UP.jpg")
wordToo = makePicture("assets//words//TOO.jpg")
wordNight = makePicture("assets//words//NIGHT.jpg")
wordIma = makePicture("assets//words//IMA.jpg")
wordFight = makePicture("assets//words//FIGHT.jpg")
wordTil = makePicture("assets//words//TIL.jpg")
wordWe = makePicture("assets//words//WE.jpg")
wordSee = makePicture("assets//words//SEE.jpg")
wordThe = makePicture("assets//words//THE.jpg")
# word sunlight in the loop for the rainbow effect
wordTick = makePicture("assets//words//TICK.jpg")
wordTock = makePicture("assets//words//TOCK.jpg")
wordOn = makePicture("assets//words//ON.jpg")
wordThe2 = makePicture("assets//words//THE2.jpg")
wordClock = makePicture("assets//words//CLOCK.jpg")
wordBut = makePicture("assets//words//BUT.jpg")
wordThe3 = makePicture("assets//words//THE3.jpg")
wordParty = makePicture("assets//words//PARTY.jpg")
wordDont2 = makePicture("assets//words//DONT2.jpg")
wordStop2 = makePicture("assets//words//STOP2.jpg")
wordNo = makePicture("assets//words//NO.jpg")
wordOhWhoa = makePicture("assets//words//OHWHOA.jpg")
wordOhWhoa2 = makePicture("assets//words//OHWHOA2.jpg")
for i in range(0, 588): #588 frames, the main for loop
print("Generating frame: "+str(i)) # Prints frame count
if (i < 193) or (i > 216): # for the TOOOOOOOOONIGHT effect
canvas = makeEmptyPicture(500, 300)
addRectFilled(canvas, 0, 0, 500, 300, black) # Makes the canvas black
if (108 <= i < 144): # Background if statements
copyInto(bg1, canvas, 0, 0)
elif (144 <= i < 192):
copyInto(bg2, canvas, 133 - i, 61 - i//2)
elif (216 <= i < 240):
copyInto(bg3, canvas, 0, 108 - i//2)
elif (240 <= i < 288):
copyInto(bg4, canvas, 0, 70 - i//2)
elif (288 <= i < 336):
copyInto(bg5, canvas, 0, 0)
elif (336 <= i < 396):
copyInto(bg6, canvas, 168 - i//2, 112 - i//3)
elif (396 <= i < 492):
copyInto(bg7, canvas, 0, 198 - i//2)
if (108 <= i < 492): # Background color effects
if (i//6 % 4) == 0:
incValues(canvas, 5, 1, 1)
elif (i//6 % 4) == 1:
incValues(canvas, 1, 5, 5)
elif (i//6 % 4) == 2:
incValues(canvas, 1, 5, 1)
elif (i//6 % 4) == 3:
incValues(canvas, 5, 1, 5)
if (0 <= i < 6): # Lyric if statements
addText(canvas, 0, 0, "Now", 20, blue)
elif (6 <= i < 12):
addText(canvas, 0, 0, "Now the", 20, blue)
elif (12 <= i < 24):
addText(canvas, 0, 0, "Now the party", 20, blue)
elif (24 <= i < 36):
addText(canvas, 0, 0, "Now the party don't", 20, blue)
elif (36 <= i < 48):
addText(canvas, 0, 0, "Now the party don't start", 20, blue)
elif (48 <= i < 54):
addText(canvas, 0, 0, "Now the party don't start 'til", 20, blue)
elif (54 <= i < 66):
addText(canvas, 0, 0, "Now the party don't start 'til I", 20, blue)
elif (66 <= i < 78):
addText(canvas, 0, 0, "Now the party don't start 'til I walk", 20, blue)
elif (78 <= i < 82):
addText(canvas, 0, 0, "Now the party don't start 'til I walk in", 20, blue)
elif (82 <= i < 86):
addText(canvas, 0, 0, "Now the party don't start 'til I walk in.", 20, blue)
elif (86 <= i < 90):
addText(canvas, 0, 0, "Now the party don't start 'til I walk in..", 20, blue)
elif (90 <= i < 96):
addText(canvas, 0, 0, "Now the party don't start 'til I walk in...", 20, blue)
elif (96 <= i < 108):
copyIntoWithBackgroundRemover(wordDont, canvas, i//3 - 32, 3, 25)
elif (108 <= i < 120):
copyIntoWithBackgroundRemover(wordDont, canvas, 4, 3, 25)
copyIntoWithBackgroundRemover(wordStop, canvas, 273 + 40 - i//3, 2, 25)
elif (120 <= i < 126):
copyIntoWithBackgroundRemover(wordDont, canvas, 4, 3, 25)
copyIntoWithBackgroundRemover(wordStop, canvas, 273, 2, 25)
copyIntoWithBackgroundRemover(wordMake, canvas, 0, 218 + 42 - i//3, 25)
elif (126 <= i < 132):
copyIntoWithBackgroundRemover(wordDont, canvas, 4, 3, 25)
copyIntoWithBackgroundRemover(wordStop, canvas, 273, 2, 25)
copyIntoWithBackgroundRemover(wordMake, canvas, 0, 218, 25)
copyIntoWithBackgroundRemover(wordIt, canvas, 218, 219 + 44 - i//3, 25)
elif (132 <= i < 144):
copyIntoWithBackgroundRemover(wordDont, canvas, 4, 3, 25)
copyIntoWithBackgroundRemover(wordStop, canvas, 273, 2, 25)
copyIntoWithBackgroundRemover(wordMake, canvas, 0, 218, 25)
copyIntoWithBackgroundRemover(wordIt, canvas, 218, 219, 25)
copyIntoWithBackgroundRemover(wordPop, canvas, 335, 221 + 48 - i//3, 25)
elif (144 <= i < 156):
copyIntoWithBackgroundRemover(wordDJ, canvas, 14 - 52 + i//3, 0, 25)
elif (156 <= i < 162):
copyIntoWithBackgroundRemover(wordDJ, canvas, 14, 0, 25)
copyIntoWithBackgroundRemover(wordBlow, canvas, 132, 8 + i//3 - 54, 25)
elif (162 <= i < 168):
copyIntoWithBackgroundRemover(wordDJ, canvas, 14, 0, 25)
copyIntoWithBackgroundRemover(wordBlow, canvas, 132, 8, 25)
copyIntoWithBackgroundRemover(wordMy, canvas, 346 + 56 - i//3, 7, 25)
elif (168 <= i < 180):
copyIntoWithBackgroundRemover(wordDJ, canvas, 14, 0, 25)
copyIntoWithBackgroundRemover(wordBlow, canvas, 132, 8, 25)
copyIntoWithBackgroundRemover(wordMy, canvas, 346, 7, 25)
copyIntoWithBackgroundRemover(wordSpeakers, canvas, 6, 221 + 60 - i//3, 25)
elif (180 <= i < 192):
copyIntoWithBackgroundRemover(wordDJ, canvas, 14, 0, 25)
copyIntoWithBackgroundRemover(wordBlow, canvas, 132, 8, 25)
copyIntoWithBackgroundRemover(wordMy, canvas, 346, 7, 25)
copyIntoWithBackgroundRemover(wordSpeakers, canvas, 6, 221, 25)
copyIntoWithBackgroundRemover(wordUp, canvas, 380, 223 + 64 - i//3, 25)
elif (192 <= i < 204):
copyIntoWithBackgroundRemover(wordToo, canvas, i*5 - 960 + 40, 85, 20)
elif (i == 204): # since frames 204 to 216 reset the canvas, this only needs to be done once
copyIntoWithBackgroundRemover(wordToo, canvas, 100, 85, 20)
copyIntoWithBackgroundRemover(wordNight, canvas, 205, 84, 20)
elif (216 <= i < 228):
copyIntoWithBackgroundRemover(wordIma, canvas, 14 - 57 + i//4, 6, 25)
elif (228 <= i < 240):
copyIntoWithBackgroundRemover(wordIma, canvas, 14, 6, 25)
copyIntoWithBackgroundRemover(wordFight, canvas, 230 + i//3 - 48, 7 + i//4 - 34, 25)
elif (240 <= i < 246):
copyIntoWithBackgroundRemover(wordTil, canvas, 0, 0, 25)
elif (246 <= i < 252):
copyIntoWithBackgroundRemover(wordTil, canvas, 0, 0, 25)
copyIntoWithBackgroundRemover(wordWe, canvas, 163, 0, 25)
elif (252 <= i < 258):
copyIntoWithBackgroundRemover(wordTil, canvas, 0, 0, 25)
copyIntoWithBackgroundRemover(wordWe, canvas, 163, 0, 25)
copyIntoWithBackgroundRemover(wordSee, canvas, 314, 0, 25)
elif (258 <= i < 264):
copyIntoWithBackgroundRemover(wordTil, canvas, 0, 0, 25)
copyIntoWithBackgroundRemover(wordWe, canvas, 163, 0, 25)
copyIntoWithBackgroundRemover(wordSee, canvas, 314, 0, 25)
copyIntoWithBackgroundRemover(wordThe, canvas, 149, 94, 25)
elif (264 <= i < 288): # Rainbow letter effect
wordSunlight = makePicture("assets//words//SUNLIGHT.jpg") # The rainbow effect requires this be in the loop
sunlightColor = (255, 223, 50)
if (i % 11 == 0): # oh no, nested if statements
rainbowText(wordSunlight, 255, 0, 0, sunlightColor, 30)
elif (i % 11 == 1):
rainbowText(wordSunlight, 255, 150, 0, sunlightColor, 30)
elif (i % 11 == 2):
rainbowText(wordSunlight, 255, 255, 0, sunlightColor, 30)
elif (i % 11 == 3):
rainbowText(wordSunlight, 40, 255, 0, sunlightColor, 30)
elif (i % 11 == 4):
rainbowText(wordSunlight, 0, 255, 40, sunlightColor, 30)
elif (i % 11 == 5):
rainbowText(wordSunlight, 0, 255, 255, sunlightColor, 30)
elif (i % 11 == 6):
rainbowText(wordSunlight, 0, 80, 255, sunlightColor, 30)
elif (i % 11 == 7):
rainbowText(wordSunlight, 0, 0, 255, sunlightColor, 30)
elif (i % 11 == 8):
rainbowText(wordSunlight, 80, 0, 255, sunlightColor, 30)
elif (i % 11 == 9):
rainbowText(wordSunlight, 255, 0, 255, sunlightColor, 30)
elif (i % 11 == 10):
rainbowText(wordSunlight, 255, 0, 80, sunlightColor, 30)
copyIntoWithBackgroundRemover(wordTil, canvas, 0, 0, 25)
copyIntoWithBackgroundRemover(wordWe, canvas, 163, 0, 25)
copyIntoWithBackgroundRemover(wordSee, canvas, 314, 0, 25)
copyIntoWithBackgroundRemover(wordThe, canvas, 149, 94, 25)
copyIntoWithBackgroundRemover(wordSunlight, canvas, 25, 193, 25)
elif (288 <= i < 300):
copyIntoWithBackgroundRemover(wordTick, canvas, 13 - 75 + i//4, 7 - 50 + i//6, 25)
elif (300 <= i < 312):
copyIntoWithBackgroundRemover(wordTick, canvas, 13, 7, 25)
copyIntoWithBackgroundRemover(wordTock, canvas, 251 + 78 - i//4, 11 - 52 + i//6, 25)
elif (312 <= i < 318):
copyIntoWithBackgroundRemover(wordTick, canvas, 13, 7, 25)
copyIntoWithBackgroundRemover(wordTock, canvas, 251, 11, 25)
copyIntoWithBackgroundRemover(wordOn, canvas, 10, 222, 25)
elif (318 <= i < 324):
copyIntoWithBackgroundRemover(wordTick, canvas, 13, 7, 25)
copyIntoWithBackgroundRemover(wordTock, canvas, 251, 11, 25)
copyIntoWithBackgroundRemover(wordOn, canvas, 10, 222, 25)
copyIntoWithBackgroundRemover(wordThe2, canvas, 126, 226, 25)
elif (324 <= i < 336):
copyIntoWithBackgroundRemover(wordTick, canvas, 13, 7, 25)
copyIntoWithBackgroundRemover(wordTock, canvas, 251, 11, 25)
copyIntoWithBackgroundRemover(wordOn, canvas, 10, 222, 25)
copyIntoWithBackgroundRemover(wordThe2, canvas, 126, 226, 25)
copyIntoWithBackgroundRemover(wordClock, canvas, 285, 225, 25)
elif (336 <= i < 342):
copyIntoWithBackgroundRemover(wordBut, canvas, 8, 9, 25)
elif (342 <= i < 348):
copyIntoWithBackgroundRemover(wordBut, canvas, 8, 9, 25)
copyIntoWithBackgroundRemover(wordThe3, canvas, 275, 10, 25)
elif (348 <= i < 360):
copyIntoWithBackgroundRemover(wordBut, canvas, 8, 9, 25)
copyIntoWithBackgroundRemover(wordThe3, canvas, 275, 10, 25)
copyIntoWithBackgroundRemover(wordParty, canvas, 97, 100, 25)
elif (360 <= i < 372):
copyIntoWithBackgroundRemover(wordBut, canvas, 8, 9, 25)
copyIntoWithBackgroundRemover(wordThe3, canvas, 275, 10, 25)
copyIntoWithBackgroundRemover(wordParty, canvas, 97, 100, 25)
copyIntoWithBackgroundRemover(wordDont2, canvas, 3, 229, 25)
elif (372 <= i < 378):
copyIntoWithBackgroundRemover(wordBut, canvas, 8, 9, 25)
copyIntoWithBackgroundRemover(wordThe3, canvas, 275, 10, 25)
copyIntoWithBackgroundRemover(wordParty, canvas, 97, 100, 25)
copyIntoWithBackgroundRemover(wordDont2, canvas, 3, 229, 25)
copyIntoWithBackgroundRemover(wordStop2, canvas, 189, 229, 25)
elif (378 <= i < 396):
copyIntoWithBackgroundRemover(wordBut, canvas, 8, 9, 25)
copyIntoWithBackgroundRemover(wordThe3, canvas, 275, 10, 25)
copyIntoWithBackgroundRemover(wordParty, canvas, 97, 100, 25)
copyIntoWithBackgroundRemover(wordDont2, canvas, 3, 229, 25)
copyIntoWithBackgroundRemover(wordStop2, canvas, 189, 229, 25)
copyIntoWithBackgroundRemover(wordNo, canvas, 356, 229, 25)
elif (396 <= i < 444):
copyIntoWithBackgroundRemover(wordOhWhoa, canvas, 3, 38, 25)
elif (444 <= i < 492):
copyIntoWithBackgroundRemover(wordOhWhoa, canvas, 3, 38, 25)
copyIntoWithBackgroundRemover(wordOhWhoa2, canvas, 16, 176, 25)
elif (492 <= i): # End text
addText(canvas, 0, 30, "Thanks for watching!!", 20, white)
addText(canvas, 0, 60, "See you next semester!", 20, white)
addText(canvas, 5*i - 5*492, 250, "Ahyaan Malik", 10 - 492//20 + i//20, white)
if (i == 193): # Extra text
addText(canvas, 11, 50, ":3", 15, white)
elif (i == 195):
addText(canvas, 356, 220, "Yeah", 15, white)
elif (i == 197):
addText(canvas, 3, 200, "\"Meow\" - Blue", 15, white) # learned from my C# training, works here too!
elif (i == 199):
addText(canvas, 156, 229, "ow", 15, white)
elif (i == 201):
addText(canvas, 48, 30, "This is it", 15, white)
elif (i == 202):
addText(canvas, 199, 29, "wow", 15, white)
elif (i == 203):
addText(canvas, 70, 274, "ANWS never dies", 15, white)
elif (i == 204):
addText(canvas, 270, 40, "2 am", 15, white)
elif (i == 205):
addText(canvas, 20, 170, "not forgotten", 15, white)
elif (i == 206):
addText(canvas, 185, 170, "take me higher", 15, white)
elif (492 <= i): # Blob
blobFrame = i - 492 # Makes it slightly easier to read, yes, slightly
addRectFilled(canvas, int(500/96 * blobFrame), int(270-15*abs(sin(blobFrame*4*pi/96))), int(30 + 15*abs(sin(blobFrame*4*pi/96))), int(30- 15*abs(sin(blobFrame*2*pi/96))), blue) # "why not use //" - rounding before creates inaccuracies, so you must round after all calculations
# write canvas as a JPEG frame file in frames folder
writeFrame(i, "frames", canvas)
# I've got you home no matter where you are
movie = makeMovieFromInitialFile("frames\\frame000.jpg");
writeAnimatedGif(movie, "myAnimation.gif")
print("<!> An mp4 file from the gif was already created with external resources to incorporate audio. Please view that instead. <!>")
# *** Don't modify this function. ***
# This is program 175 in Python textbook. It writes pict to
# the indicated folder using file name frameXXX.jpg, where
# XXX is num padded with leading zeros if num less than 3 digits.
def writeFrame(num, folder, pict):
# Have to deal with single digit vs. double digit
numStr=str(num)
if num < 10:
writePictureTo(pict,folder+"/frame00"+numStr+".jpg")
if num >= 10 and num<100:
writePictureTo(pict,folder+"/frame0"+numStr+".jpg")
if num >= 100:
writePictureTo(pict,folder+"/frame"+numStr+".jpg")
# Function definitions above
############################
# Test code below
createAnimation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment