Skip to content

Instantly share code, notes, and snippets.

@zrbecker
Created February 4, 2013 08:21
Show Gist options
  • Select an option

  • Save zrbecker/4705597 to your computer and use it in GitHub Desktop.

Select an option

Save zrbecker/4705597 to your computer and use it in GitHub Desktop.
### Graphics Variables
from Tkinter import *
WIDTH = 500.0
HEIGHT = 500.0
root = Tk()
canvas = Canvas(width=WIDTH, height=HEIGHT, bg="black")
moonshape = 0
### Math Variables
from math import *
earth_x = WIDTH / 2
earth_y = HEIGHT / 2
earth_radius = 25.0
orbit_scale_x = 200.0
orbit_scale_y = 100.0
orbit_rotation = -pi / 9.0
moon_radius = 15.0
moon_speed = 10
moon_position = 0.0
def MoonCoords():
moon_x = (orbit_scale_x * cos(moon_position) * cos(orbit_rotation)
- orbit_scale_y * sin(moon_position) * sin(orbit_rotation) + earth_x)
moon_y = (orbit_scale_x * cos(moon_position) * sin(orbit_rotation)
+ orbit_scale_y * sin(moon_position) * cos(orbit_rotation) + earth_y)
return (moon_x, moon_y)
def DrawEarth():
canvas.create_oval(earth_x - earth_radius,
earth_y - earth_radius,
earth_x + earth_radius,
earth_y + earth_radius,
width=0,
fill="blue")
def DrawMoon():
global moonshape
moon_x, moon_y = MoonCoords()
moonshape = canvas.create_oval(moon_x - moon_radius,
moon_y - moon_radius,
moon_x + moon_radius,
moon_y + moon_radius,
width=0,
fill="grey")
def UpdateMoon():
moon_x, moon_y = MoonCoords()
canvas.coords(moonshape, moon_x - moon_radius,
moon_y - moon_radius,
moon_x + moon_radius,
moon_y + moon_radius)
def Update():
global moon_position
moon_position += moon_speed / 1000.0
UpdateMoon()
root.after(10, Update)
def Main():
canvas.pack(expand=YES, fill=BOTH)
DrawEarth()
DrawMoon()
Update()
root.mainloop()
if __name__ == "__main__":
Main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment