Last active
December 22, 2015 22:19
-
-
Save dermotbalson/6539612 to your computer and use it in GitHub Desktop.
Orbit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
displayMode(FULLSCREEN) | |
supportedOrientations(PORTRAIT) | |
function setup() | |
D=100 --diameter of planet | |
S=120 --speed of planet in pixels per second | |
R=math.random(300,1000)--radius of orbit | |
a=0 --starting angle for planet | |
--set position of centre of sun at bottom | |
X=WIDTH/2 | |
Y=-100 | |
--calculate planet speed in degrees per redraw | |
da=6 * S / (2 * math.pi * R) | |
end | |
function draw() | |
background(0) | |
local x=X+R*math.sin(math.rad(a)) --turn a into radians for use with math.sin | |
local y=Y+R*math.cos(math.rad(a)) | |
pushStyle() | |
--draw the sun | |
fill(255,255,0) | |
ellipse(X,Y,X+50) -- make the sun peek over the bottom of the screen | |
--draw the planet | |
fill(209, 168, 92, 255) | |
ellipse(x,y,D) | |
--reverse direction if we hit the sides | |
if x<D/2 or x>WIDTH-D/2 or y<D/2 then da=-da end | |
--update the planet angle | |
a=a+da | |
fontSize(24) | |
fill(255) | |
text("Restart (circular icon at bottom) to see different orbits",WIDTH/2,HEIGHT-100) | |
popStyle() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment