You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's have some more fun with variables. In this program we are going to use the variable 'sides' to change the number of sides we draw each time. Write your program, save it and then run.
Using color to fill objects we create.
# Fill in a circle with color
import turtle as t
t.color('#0000FF')
t.begin_fill()
t.circle(100)
t.end_fill()
Another example of filling
# Fill in square
import turtle as t
t.color("#6A5ACD")
t.begin_fill()
for i in range(4):
t.forward(150)
t.right(90)
t.end_fill()
Draw a color square using 'input'.
# Draw a colored square from inputs
import turtle as t
# taking input for the side of the square
side = int(input("Enter the length of the side of the square: "))
# taking the input for the color
col = input("Enter the color name or hex value of color(# RRGGBB): ")
# set the fillcolor
t.fillcolor(col)
# start the filling color
t.begin_fill()
# drawing the square of side s
for x in range(4):
t.forward(side)
t.right(90)
# ending the filling of the color
t.end_fill()
Draw a Color Spiral
# ColorSpiral.py
import turtle as t
t.bgcolor("black")
# You can choose between 2 and 6 sides for some cool shapes!
sides = 6
colors = ["red", "yellow", "blue", "orange", "green", "purple"]
for x in range(360):
t.pencolor(colors[x%sides])
t.forward(x * 3/sides + x)
t.left(360/sides + 1)
t.width(x*sides/200)
Change the number of sides and run the program again.
What happened?
Change the background color to "blue".
Change line 5 of the program to read: sides = eval(input("Enter a number of sides between 2 and 6: "))
Save it! Run it!
What happened?
Function are blocks of code we can use over and over again. Here is an example of how we create a function to draw ColorSquareSpiral.py. We create functions in Python using the 'def' argument.
def my_function():
print("Hello from a function")
Run the function and then enter 'my_function()' in the REPL.
What was the output?
# ColorSquareSpiral function
def ColorSquareSpiral():
import turtle as t
t.speed(0)
t.bgcolor("black")
colors = ["red", "yellow", "blue", "green", "orange"]
for x in range(200):
t.pencolor(colors[x%5])
t.forward(x)
t.left(91)
Save the code and give it a name like 'ColorSquareSpiralFunction.py.
'Run' the code. Then open the REPL.
Open up the 'REPL' and enter 'ColorSquareSpiral()' and press enter.
What happened?
Build a 5 pointed star wtih Python
# Turtle Star
import turtle as t
for i in range(5):
t.forward(100)
t.right(144)
Change the size of the star.
Change the color of the star
Change the color. Change the pen size.
Another Star
# 5 point star with color
import turtle as t
# decide colors
cir= ['red','green','blue','yellow','purple']
# decide pensize
t.pensize(4)
# Draw star pattern
t.penup()
t.setpos(-90,30)
t.pendown()
for i in range(5):
t.pencolor(cir[i])
t.forward(200)
t.right(144)
t.penup()
t.setpos(80,-140)
t.pendown()
# choose pen color
t.pencolor("Black")
t.done()
Make a rainbow with Python
# Turtle Rainbow
# Import turtle package
import turtle as t
# Creating a turtle screen object
sc = t.Screen()
# Defining a method to form a semicircle
# with a dynamic radius and color
def semi_circle(col, rad, val):
# Set the fill color of the semicircle
t.color(col)
# Draw a circle
t.circle(rad, -180)
# Move the turtle to air
t.up()
# Move the turtle to a given position
t.setpos(val, 0)
# Move the turtle to the ground
t.down()
t.right(180)
# Set the colors for drawing
col = ['violet', 'indigo', 'blue',
'green', 'yellow', 'orange', 'red']
# Setup the screen features
sc.setup(600, 600)
# Set the screen color to black
sc.bgcolor('black')
# Setup the turtle features
t.right(90)
t.width(10)
t.speed(7)
# Loop to draw 7 semicircles
for i in range(7):
semi_circle(col[i], 10*(
i + 8), -10*(i + 1))
# Hide the turtle
t.hideturtle()
Draw a color filled star
# Color Filled Star
import turtle as t
# function to draw
# colored star
def colored_star():
# size of star
size = 80
# object color
t.color("red")
# object width
t.width(4)
# angle to form star
angle = 120
# color to fill
t.fillcolor("yellow")
t.begin_fill()
# form star
for side in range(5):
t.forward(size)
t.right(angle)
t.forward(size)
t.right(72 - angle)
# fill color
t.end_fill()
# Driver code
colored_star()
Random color choices
import turtle as t
import random
colors = ["red","green","blue","orange","purple","pink","yellow"] # Make a list of colors to picvk from
t.width(10) #What does this line do?
length = 10
for count in range(200):
color = random.choice(colors) #Choose a random color
t.forward(length)
t.right(135)
t.color(color) # Why is color spelt like this?
length = length + 5
# Password Generator
import string
import random
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(random.choice(characters) for x in range(random.randint(8,16)))
print(password)
We can create random spirals using the random module together with the turtle module.
#RandomSpirals.py
import random
import turtle as t
t.bgcolor("black")
colors = ["red", "yellow", "blue", "green", "orange", "purple",
"white", "gray"]
for n in range(50):
# Generate spirals of random sizes/colors at random locations
t.pencolor(random.choice(colors)) # Pick a random color
size = random.randint(10,40) # Pick a random spiral size
# Generate a random (x,y) location on the screen
x = random.randrange(-t.window_width()//2,
t.window_width()//2)
y = random.randrange(-t.window_height()//2,
t.window_height()//2)
t.penup()
t.setpos(x,y)
t.pendown()
for m in range(size):
t.forward(m*2)
t.left(91)