Skip to content

Instantly share code, notes, and snippets.

@AbrahamAriel
Last active July 31, 2021 18:34
Show Gist options
  • Save AbrahamAriel/26d01391e5bd028df1ecec58486131b8 to your computer and use it in GitHub Desktop.
Save AbrahamAriel/26d01391e5bd028df1ecec58486131b8 to your computer and use it in GitHub Desktop.
(2017) Programming Paradigm -- Lab 2 (Assignment) -- Functions, Selections, Repetitions, Recursions with Turtle Graphics. (A little bit overkill, I should say myself.)
import random
import turtle
# Name: Window name
# Colour: Background Colour
# Width and Height: Width and Height of Window
def makeWindow(name="MY_WINDOW", colour="white", width=800, height=600):
scr = turtle.Screen()
scr.title(name)
scr.bgcolor(colour)
scr.setup(width, height)
return scr
# Shape: Turtle's shape
# Colour: Turtle's colour (not Pen's)
# Size: Thickness of Pen
def makeTurtle(shape="turtle", colour="black", size=1):
ttl = turtle.Turtle()
ttl.shape(shape)
ttl.color(colour)
ttl.pensize(size)
ttl.speed(0) # Doesn't seem to be working?
return ttl
# Draw a star
def drawStar(ttl):
for i in range(5):
ttl.forward(350)
ttl.right(144)
# Draw a coloured star
def drawStarColourful(ttl):
for i in range(5):
ttl.pencolor(prettyTurtle())
ttl.forward(350)
ttl.right(144)
# Draw a spiral
def drawSpiral(ttl):
len = 1
for i in range(200):
# ttl.pencolor(prettyTurtle()) # Uncomment this line to use random colours
ttl.forward(len)
ttl.right(30)
len += 1
# Draw a spiral (Dotted)
def drawSpiralDotted(ttl):
len = 1
for i in range(200):
# ttl.pencolor(prettyTurtle()) # Uncomment this line to use random colours
if (i % 2 != 0):
ttl.penup()
ttl.forward(len)
ttl.right(30)
len += 1
ttl.pendown()
else:
ttl.forward(len)
ttl.right(30)
len += 1
# Draw a Snowflake
def drawSnowflake(ttl, length, level):
if level > 0:
for i in range(5):
# ttl.pencolor(prettyTurtle()) # Uncomment this line to use random colours
ttl.forward(length)
drawSnowflake(ttl, length / 2.5, level - 1)
ttl.backward(length)
ttl.right(72)
# Pretty Turtles, AKA random colours
def prettyTurtle():
colours = ["blue","black", "brown","red","yellow","green","orange","beige","turquoise","pink"]
random.shuffle(colours)
return colours[0]
# Main function
def main():
scr = makeWindow()
ttl = makeTurtle()
scr.tracer(0, 0) # Skips the drawing animation
ttl.write("Please choose a pattern through the terminal. Thank you!\nYou can choose another pattern after a chosen pattern has been completed. :)", True, align="center")
print("Choose a pattern:")
print("1. Star")
print("2. Star (Colourful)")
print("3. Spiral")
print("4. Spiral (dotted)")
print("5. Snowflake")
print("6. Exit")
while True:
try:
choice = int(input("\nEnter your choice: "))
if choice == 1:
scr.title("Star")
scr.reset()
drawStar(ttl)
scr.update() # Update the screen once done
elif choice == 2:
scr.title("Star (Colourful)")
scr.reset()
drawStarColourful(ttl)
scr.update() # Update the screen once done
elif choice == 3:
scr.title("Spiral")
scr.reset()
drawSpiral(ttl)
scr.update() # Update the screen once done
elif choice == 4:
scr.title("Spiral (dotted)")
scr.reset()
drawSpiralDotted(ttl)
scr.update() # Update the screen once done
elif choice == 5:
scr.title("Snowflake")
scr.reset()
drawSnowflake(ttl, 100, 6)
scr.update() # Update the screen once done
elif choice == 6:
print("\nThank you for playing! :)")
print("~ Yours Truly")
break
else:
print("Invalid choice! Please try again.")
except ValueError:
print("Invalid choice! Please try again.")
# Start the program
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment