Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cairo
- import math
- FRAMECOUNT = 10
- #############################
- #The particle or planets or whatever are called shades in the code.
- class Shade():
- #Initialize shade with a radius and centerpoint. Tuple expected by arg centerpoint.
- #Centerpoint is unused in this code, but I left it in because it could be used later.
- def __init__(self, radius, centerpoint):
- self.xCord, self.yCord = centerpoint
- self.radius = radius
- self.center = centerpoint
- #This lets shade change its own position.
- def updatePos(self, newX, newY):
- self.xCord = newX
- self.yCord = newY
- #This lets shade receive a tuple, unpack it, and update its position with it.
- def receiveCord(self, cords):
- x, y = cords
- self.updatePos(x, y)
- #This lets shade draw itself to the context. We're assuming
- #here that all shades are white, and want to be circles.
- #But further upgrades could change that. True for filled, False for outline.
- def drawSelf(self, context, fill):
- context.set_source_rgb(1,1,1)
- context.arc(self.xCord, self.yCord, self.radius, 0, 2*math.pi)
- if fill == True:
- context.fill()
- else:
- context.stroke()
- class Orbit():
- def __init__(self, frameCount, radius, center, phase):
- #Initializes Positions with the number of frames, a radius of
- #the orbit, a center of the orbit, and a phase. The phase is where
- #the planet starts in its orbit. At phase 0, the planet starts
- #at the far right and moves clockwise. Currently no ability to
- #make counter-clockwise.
- #
- #Takes the frame count and slices a circle into that many wedges,
- #then finds the x and y coordinate along a circle a unit circle and adds it
- #to a list.
- self.radian = 2*math.pi/frameCount
- self.slices = []
- for x in range(frameCount):
- currentSlice = self.radian * x
- xVal = math.cos(currentSlice)
- yVal = math.sin(currentSlice)
- self.slices.append((xVal,yVal))
- #Scales those coordinates by the radius. Creates a new list filled with
- #tuples of the correctly scaled x, y coordinates.
- self.storedCords = []
- for each in self.slices:
- xVal = (each[0]*radius)
- yVal = (each[1]*radius)
- self.storedCords.append((xVal,yVal))
- #Translates those coordinates to the correct centerpoint inside the image.
- #Goes through each coordinate and makes a new tuple with correct translation.
- self.centerpoint = center
- xCent, yCent = center
- for i in range(len(self.storedCords)):
- tempX = self.storedCords[i][0] + xCent
- tempY = self.storedCords[i][1] + yCent
- self.storedCords[i] = (tempX, tempY)
- # A phase of 0 means the planet starts on the right side.
- # A phase of half your framecount starts it on the left side.
- # Simple slice and dice.
- self.finalCords = self.storedCords[phase:]
- for x in self.storedCords[:phase]:
- self.finalCords.append(x)
- ###################################################
- surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1000, 1000)
- ctx = cairo.Context (surface)
- frames = FRAMECOUNT
- # Initial positions of Shades don't matter to the program.
- blackhole = Shade(125, (500,500))
- planet = Shade(25, (800, 500))
- moon = Shade(5, (750,500))
- # Find the planet's orbit around 500, 500, or our black hole.
- #And assign it to planetPos
- planetPos = Orbit(FRAMECOUNT, 300, (500, 500), 0)
- #animation
- for x in range(frames):
- # Blank the screen.
- ctx.set_source_rgb(0, 0, 0)
- ctx.rectangle(0, 0, 1000, 1000)
- ctx.fill()
- # Update the positions. Recursion!
- planet.receiveCord(planetPos.finalCords[x])
- moonPos = Orbit(FRAMECOUNT, 50, planetPos.finalCords[x], x)
- moon.receiveCord(moonPos.finalCords[x])
- # Tell the planets to draw themselves.
- blackhole.drawSelf(ctx, False)
- planet.drawSelf(ctx, True)
- moon.drawSelf(ctx, False)
- # Draw the thing to a png, and iterate over however many frames.
- ctx.stroke()
- print ('Creating Frame')
- surface.write_to_png('Frame' + str(x)+'.png')
- print ('Finished')
Advertisement
Add Comment
Please, Sign In to add comment