planets.py
from visual import * from math import pi, sin, cos, radians """ a source of data for the following class templates: http://www.krysstal.com/solarsys_planets.html A partial implementation of our solar system """ def setscene ( ): """set the stage for our little drama""" scene2 = display(title='Orbiting Planets', fullscreen= True, autoscale = True, background=color.black) return scene2 class Planet (object): diameter = 1 from_sun = 1 period = 1 theta = 0 color = color.red scale = 10000 # magnify radius by this amount dist_scale = (149*1000000) * (1./500) # shrink distances by this amount def __init__(self, name, diameter, from_sun, period, color): self.name = name self.diameter = diameter * self.scale self.from_sun = from_sun * self.dist_scale self.period = period self.color = color self.r = (self.diameter / 2.0) def drawme(self): self.image = sphere(pos=(self.from_sun, 0, 0), radius = self.r, color = self.color) def orbit(self): self.theta = self.theta + 365.25/self.period x = self.from_sun * cos(radians(self.theta)) y = self.from_sun * sin(radians(self.theta)) self.image.pos = (x, y, 0) planets = [ Planet("Mercury", diameter = 0.38, from_sun = 0.387, period = 88, color = (1,1,0)), Planet("Venus", diameter = 0.95, from_sun = 0.723, period = 224.7, color = color.cyan), Planet("Earth", diameter = 1, from_sun = 1, period = 365.25, color = color.blue), Planet("Mars", diameter = 0.53, from_sun = 1.524, period = 687, color = color.red), Planet("Jupiter", diameter = 10.97, from_sun = 5.204, period = 11.75 * 365.25, color = color.green)] class Mercury( Planet ): diameter = 0.38 from_sun = 0.387 class Venus (Planet) : diameter = 0.95 from_sun = 0.723 class Earth ( Planet ): diameter = 1 from_sun = 1 class Mars ( Planet ): diameter = 0.53 from_sun = 1.524 class Jupiter ( Planet ): diameter = 10.97 from_sun = 5.204 class Saturn ( Planet ): diameter = 9.14 from_sun = 9.582 class Uranus ( Planet ): diameter = 3.98 from_sun = 19.201 class Neptune ( Planet ): diameter = 3.87 from_sun = 30.047 class Pluto ( Planet ): diameter = 0.10 from_sun = 39.236 def solarsystem( ): scene = setscene() scene.select() sun = sphere(pos=(0, 0,0), radius = 140000/2.0, color = color.yellow) for planet in planets: planet.drawme() for i in range(360): for planet in planets: planet.orbit() rate(50) print "OK, done..." scene.visible = False return if __name__ == "__main__": solarsystem( ) |