Skip to content

Instantly share code, notes, and snippets.

@orjanv
Created April 17, 2023 12:19
Show Gist options
  • Save orjanv/417bafbe96c5719ed720050425e24e8c to your computer and use it in GitHub Desktop.
Save orjanv/417bafbe96c5719ed720050425e24e8c to your computer and use it in GitHub Desktop.
Draw day and night with the Python turtle module
from turtle import *
from random import *
def RandomPosition():
penup()
posx = randint(-500, 500)
posy = randint(-200, 300)
setpos(posx, posy)
pendown()
def seagull(gullsize):
color("Grey")
pensize(2)
penup()
setheading(90)
RandomPosition()
pendown()
right(135)
forward(gullsize)
left(90)
forward(gullsize)
penup()
def skies(size):
color("White")
RandomPosition()
setheading(360)
pendown()
begin_fill()
circle(size,180)
setheading(90)
circle(size*1.33,180)
setheading(180)
circle(size,180)
forward((size*1.33)*2)
left(90)
forward(size*2)
end_fill()
penup()
def thesun():
color("Yellow")
RandomPosition()
pendown()
begin_fill()
circle(70)
end_fill()
penup()
def rainbow(size):
penup()
colors = ["#FF0000", "#FFA500", "#FFFF00", "#00FF00",
"#0000FF", "#4B0082", "#8F00FF"]
posx = randint(-400, 400)
posy = -450
for c in colors:
color(c)
setpos(posx, posy)
setheading(90)
pendown()
begin_fill()
circle(size, 180)
left(90)
forward(10)
left(90)
circle(-size+10, 180)
left(90)
forward(10)
end_fill()
penup()
posx = posx-10
size = size-10
def house(x, y, h, w, col):
color(col)
pencolor("Black")
setpos(x, y)
setheading(90)
pendown()
begin_fill()
for i in range(2):
forward(h)
right(90)
forward(w)
right(90)
end_fill()
penup()
setup(1200, 900)
bgcolor("SkyBlue")
tracer(0) # sett til 0 for å tegne umiddelbart, da betyr speed() ingenting
speed('fastest')
hideturtle()
# Draw the sun
thesun()
# Draw a rainbow
rainbow(300)
# Draw seven skies in random sizes
for sky in range(7):
size = randint(10, 40)
skies(size)
# Draw 20 seagulls
for gull in range(20):
gullsize = randint(5, 25)
seagull(gullsize)
exitonclick()
from turtle import *
from random import *
def RandomPosition():
penup()
posx = randint(-700, 700)
posy = randint(-500, 500)
setpos(posx, posy)
pendown()
def DrawStar(size, starcolor):
color(starcolor)
pendown()
begin_fill()
for line in range(5):
left(144)
forward(size)
end_fill()
penup()
def DrawConstallation(numberofstars):
RandomPosition()
for star in range(numberofstars-1):
dot(randint(5, 10), "White")
pendown()
left(randint(-110, 110))
forward(randint(20, 50))
dot(randint(5, 10), "White")
def DrawGalaxy(numberofstars):
starcolors = ["White","LightBlue","Pink"]
RandomPosition()
for star in range(numberofstars):
penup()
left(randint(-200, 200))
forward(randint(2, 15))
pendown()
dot(2, choice(starcolors))
# Setup values
skjerm_bredde = 1600
skjerm_hoyde = 1000
setup(skjerm_bredde, skjerm_hoyde)
tracer(0) # sett til 0 for å tegne umiddelbart, da betyr speed() ingenting
speed('fastest')
bgcolor("#000316")
hideturtle()
# Draw 50 random stars
for i in range(50):
RandomPosition()
DrawStar(randint(5, 25), "White")
# Draw 5 constellations
for constellation in range(5):
DrawConstallation(randint(4, 7))
# Draw 4 small galaxies, each with 40 stars
for galaxy in range(4):
DrawGalaxy(40)
exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment