# Project 7: Planet Simulator from tkinter import Tk, Canvas from math import sin, cos from time import time from random import random class Planet: def __init__(self, radius=20, orbitradius=100, color="white", period=5): """Create a planet object. Sets planet data. Input: Radius, Orbitradius, color, period""" def position_tuple(self, secondselapsed = 0): """Method that computes the x,y positions of a planet. Input: time Output: (x, y)""" #Set converstion between seconds and days (simulation speed) # Calculate x, y, using cos, sin, days elapsed, and the planet's data class PlanetaryAnimator: def __init__(self, master): """Initiate Animator Object.""" #Bind the tk object to the animator object # Set Resolution # Set resizable OFF # Set Title # Bind "q" to quit the application # Create a canvas object, set background color and size # Pack the canvas object # Create an empty dictionary of planets def create_planet(self, planet): """Create a planet on screen. Input: Planet Object""" # Draw the planet's arc using draw_planetary_arc # Get the x, y coordinates at time 0 from the position tuple # Translate coordinates using _tktranslation() into Canvas coordinates # Draw a circle to the canvas, save the ID # Save the ID to a dictionary with each ID:Planet object def draw_planetary_arc(self, planet): """Draw the planet's arc Input: Planet Object""" # Calculate x0, x1, y0, y1 from the planet's orbit radius # Draw the orbit path to the canvas. ID does not need to be saved. def animate(self, starttime=time()): """Update the position of each planet with respect to time. Input: current time in seconds""" # For each id, planet in the objects dictionary # claculate coordinates with respect to time # move planet object to the new coordinate # Refresh using canvas.after() def _tktranslation(self, x, y, planet): """Method that translates x,y coordinates from a cartesian coordinate system to the Canvas coordinate system Input: Cartesian x, y Output: x0, y0, x1, y1""" def main(): # Create new tk object # Pass the tk object into the PlanetaryAnimator # Mercury: p = 87.97 days # Venus: p = 224.7 days # Earth: p = 365.3 days # Mars: p = 686.93 days # Jupiter: p = 11.86*365 days # Saturn: p = 29.42*365 days # Uranus: p = 83.75*365 days # Neptune: p = 163.72*365 days # Create a new planet object for each planet in the # solar system. Input the planet's data. # Use the method create_planet(Planet Object) to draw the # Graphical representation of each planet # Animate the simulation using the animate() method # Call tk mainloop to begin simulation if __name__ == '__main__': """This calls the main function""" main()