Code Sketch
Solar System
Category: Math
cleari() // Reset screen and stop old animations
// 1. Full-Screen Background (Centered properly at 0,0)
val bg = Picture {
setPenColor(noColor)
setFillColor(cm.black)
penUp(); moveTo(-1000, -1000); penDown()
moveTo(1000, -1000)
moveTo(1000, 1000)
moveTo(-1000, 1000)
moveTo(-1000, -1000)
}
draw(bg)
// 2. Starfield
val stars = Picture {
setPenColor(noColor)
setFillColor(cm.white)
for (i <- 1 to 250) {
val x = (math.random * 1600) - 800
val y = (math.random * 1600) - 800
penUp(); moveTo(x, y); penDown()
circle(1.2)
}
}
draw(stars)
// 3. The Sun
val sun = Picture.circle(30)
sun.setFillColor(cm.yellow)
sun.setPenColor(cm.orange)
draw(sun)
// 4. Orbits & Distances
val distances = Array(60.0, 95.0, 135.0, 175.0, 240.0, 300.0, 350.0, 400.0)
val names = Array("Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune")
for (d <- distances) {
val track = Picture.circle(d)
track.setPenColor(cm.darkGray)
track.setFillColor(noColor)
draw(track)
}
// 5. Asteroid Belt
val asteroidBelt = Picture {
setPenColor(noColor)
setFillColor(cm.gray)
for (i <- 1 to 250) {
val angle = math.random * math.Pi * 2
val radius = 205.0 + (math.random * 15.0)
val ax = radius * math.cos(angle)
val ay = radius * math.sin(angle)
penUp(); moveTo(ax, ay); penDown()
circle(1)
}
}
draw(asteroidBelt)
// 6. Planets (Bright, visible solid colors)
def makePlanet(size: Double, col: java.awt.Color) = {
val p = Picture.circle(size)
p.setFillColor(col)
p.setPenColor(cm.black)
draw(p)
p
}
val planets = Array(
makePlanet(8, cm.lightGray), // Mercury
makePlanet(10, cm.orange), // Venus
makePlanet(11, cm.blue), // Earth
makePlanet(9, cm.red), // Mars
makePlanet(16, cm.magenta), // Jupiter
makePlanet(14, cm.yellow), // Saturn
makePlanet(12, cm.cyan), // Uranus
makePlanet(11, cm.darkBlue) // Neptune
)
// 7. Name Labels
val labels = new Array[Picture](8)
for (i <- 0 until 8) {
labels(i) = Picture {
setPenColor(cm.white)
write(names(i))
}
draw(labels(i))
}
// 8. Animation Loop
val speeds = Array(0.04, 0.03, 0.025, 0.02, 0.012, 0.009, 0.006, 0.004)
val angles = Array(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 1.5)
animate {
asteroidBelt.rotate(0.1) // Slow spin for the asteroid belt
for (i <- 0 until 8) {
angles(i) = angles(i) + speeds(i)
val x = distances(i) * math.cos(angles(i))
val y = distances(i) * math.sin(angles(i))
planets(i).setPosition(x, y)
labels(i).setPosition(x + 10, y + 10) // Keeps text neatly next to the planet
}
}