Sei sulla pagina 1di 11

# Sketchbook v1.

import turtle

def lsystem_no_color():
global lsystem_string
global lsystem_rules
global lsystem_length
global lsystem_angle
global lsystem_iterations

x_positions = []
y_positions = []
position_list = []

def push():
global position_list
position_list.append((turtle.position(), turtle.heading()))

def pull():
global position_list
position, heading = position_list.pop()

turtle.up()
turtle.goto(position)
turtle.setheading(heading)
turtle.down()

turtle.up()
for _ in range(lsystem_iterations):
result = ""
for letter in lsystem_string:
replacement = ""
for rule in lsystem_rules:
if letter == rule[0]:
replacement = rule[1]

if replacement == "":
result = result + letter
else:
result = result + replacement

lsystem_string = result

turtle.up()
turtle.tracer(False)
for letter in lsystem_string:
if letter == "F" or letter == "A" or letter == "B" or letter == "C" or
letter == "D" or letter == "E":
turtle.up()
turtle.forward(lsystem_length)

elif letter == "+":


turtle.left(lsystem_angle)

elif letter == "X" or letter == "Y" or letter == "M" or letter == "N" or


letter == "O" or letter == "P":
continue
elif letter == "-":
turtle.right(lsystem_angle)

elif letter == "G" or letter == "H" or letter == "I" or letter == "J" or


letter == "K" or letter == "L":
turtle.up()
turtle.forward(lsystem_length)
turtle.down()

elif letter == "^":


turtle.left(180)

elif letter == "|":


turtle.left(90)

elif letter == "[":


push()

elif letter == "]":


pull()

x_positions.append(turtle.xcor())
y_positions.append(turtle.ycor())

turtle.update()

x_positions.sort()
y_positions.sort()
length = x_positions[-1] - x_positions[0]
height = y_positions[-1] - y_positions[0]

x = 0 - ((x_positions[0] + x_positions[-1])/2)
y = 0 - ((y_positions[0] + y_positions[-1])/2)

turtle.up()
turtle.home()

if length > 500 and height > 500:


if length < height:
distance = lsystem_length * (500/height)
if length > height:
distance = lsystem_length * (500/length)
if length == height:
distance = lsystem_length * (500/height)

else:
if length > height:
distance = lsystem_length * (500/length)
if length < height:
distance = lsystem_length * (500/height)
if length == height:
distance = lsystem_length * (500/height)

x_position = []
y_position = []

turtle.tracer(False)
for letter in lsystem_string:
if letter == "F" or letter == "A" or letter == "B" or letter == "C" or
letter == "D" or letter == "E":
turtle.up()
turtle.forward(distance)

elif letter == "+":


turtle.left(lsystem_angle)

elif letter == "X" or letter == "Y" or letter == "M" or letter == "N" or


letter == "O" or letter == "P":
continue

elif letter == "-":


turtle.right(lsystem_angle)

elif letter == "G" or letter == "H" or letter == "I" or letter == "J" or


letter == "K" or letter == "L":
turtle.up()
turtle.forward(distance)
turtle.down()

elif letter == "^":


turtle.left(180)

elif letter == "|":


turtle.left(90)

elif letter == "[":


push()

elif letter == "]":


pull()

else:
letter = int(letter)
turtle.pencolor(lsystem_colours[letter])

x_position.append(turtle.xcor())
y_position.append(turtle.ycor())

turtle.update()
x_position.sort()
y_position.sort()
length = x_position[-1] - x_position[0]
height = y_position[-1] - y_position[0]

x = 0 - ((x_position[0] + x_position[-1])/2)
y = 0 - ((y_position[0] + y_position[-1])/2)

turtle.up()
turtle.home()
turtle.goto(x,y)

turtle.down()
turtle.tracer(False)
for letter in lsystem_string:
if letter == "F" or letter == "A" or letter == "B" or letter == "C" or
letter == "D" or letter == "E":
turtle.forward(distance)
elif letter == "+":
turtle.left(lsystem_angle)

elif letter == "X" or letter == "Y" or letter == "M" or letter == "N" or


letter == "O" or letter == "P":
continue

elif letter == "-":


turtle.right(lsystem_angle)

elif letter == "G" or letter == "H" or letter == "I" or letter == "J" or


letter == "K" or letter == "L":
turtle.up()
turtle.forward(distance)
turtle.down()

elif letter == "^":


turtle.left(180)

elif letter == "|":


turtle.left(90)

elif letter == "[":


push()

elif letter == "]":


pull()

else:
letter = int(letter)
turtle.pencolor(lsystem_colours[letter])

turtle.update()

def lsystem_with_color():
global lsystem_string
global lsystem_rules
global lsystem_length
global lsystem_angle
global lsystem_iterations
global lsystem_colors

x_positions = []
y_positions = []
position_list = []

def push():
global position_list
position_list.append((turtle.position(), turtle.heading()))

def pull():
global position_list
position, heading = position_list.pop()

turtle.up()
turtle.goto(position)
turtle.setheading(heading)
turtle.down()
turtle.up()
for _ in range(lsystem_iterations):
result = ""
for letter in lsystem_string:
replacement = ""
for rule in lsystem_rules:
if letter == rule[0]:
replacement = rule[1]

if replacement == "":
result = result + letter
else:
result = result + replacement

lsystem_string = result

turtle.up()
turtle.tracer(False)
for letter in lsystem_string:
if letter == "F" or letter == "A" or letter == "B" or letter == "C" or
letter == "D" or letter == "E":
turtle.up()
turtle.forward(lsystem_length)

elif letter == "+":


turtle.left(lsystem_angle)

elif letter == "X" or letter == "Y" or letter == "M" or letter == "N" or


letter == "O" or letter == "P":
continue

elif letter == "-":


turtle.right(lsystem_angle)

elif letter == "G" or letter == "H" or letter == "I" or letter == "J" or


letter == "K" or letter == "L":
turtle.up()
turtle.forward(lsystem_length)
turtle.down()

elif letter == "^":


turtle.left(180)

elif letter == "|":


turtle.left(90)

elif letter == "[":


push()

elif letter == "]":


pull()

x_positions.append(turtle.xcor())
y_positions.append(turtle.ycor())

turtle.update()

x_positions.sort()
y_positions.sort()
length = x_positions[-1] - x_positions[0]
height = y_positions[-1] - y_positions[0]

x = 0 - ((x_positions[0] + x_positions[-1])/2)
y = 0 - ((y_positions[0] + y_positions[-1])/2)

turtle.up()
turtle.home()

if length > 500 and height > 500:


if length < height:
distance = lsystem_length * (500/height)
if length > height:
distance = lsystem_length * (500/length)
if length == height:
distance = lsystem_length * (500/height)

else:
if length > height:
distance = lsystem_length * (500/length)
if length < height:
distance = lsystem_length * (500/height)
if length == height:
distance = lsystem_length * (500/height)

x_position = []
y_position = []

turtle.tracer(False)
for letter in lsystem_string:
if letter == "F" or letter == "A" or letter == "B" or letter == "C" or
letter == "D" or letter == "E":
turtle.up()
turtle.forward(distance)

elif letter == "+":


turtle.left(lsystem_angle)

elif letter == "X" or letter == "Y" or letter == "M" or letter == "N" or


letter == "O" or letter == "P":
continue

elif letter == "-":


turtle.right(lsystem_angle)

elif letter == "G" or letter == "H" or letter == "I" or letter == "J" or


letter == "K" or letter == "L":
turtle.up()
turtle.forward(distance)
turtle.down()

elif letter == "^":


turtle.left(180)

elif letter == "|":


turtle.left(90)

elif letter == "[":


push()

elif letter == "]":


pull()

else:
letter = int(letter)
turtle.pencolor(lsystem_colours[letter])

x_position.append(turtle.xcor())
y_position.append(turtle.ycor())

turtle.update()
x_position.sort()
y_position.sort()
length = x_position[-1] - x_position[0]
height = y_position[-1] - y_position[0]

x = 0 - ((x_position[0] + x_position[-1])/2)
y = 0 - ((y_position[0] + y_position[-1])/2)

turtle.up()
turtle.home()
turtle.goto(x,y)

turtle.down()
turtle.tracer(False)
for letter in lsystem_string:
if letter == "F" or letter == "A" or letter == "B" or letter == "C" or
letter == "D" or letter == "E":
turtle.forward(distance)

elif letter == "+":


turtle.left(lsystem_angle)

elif letter == "X" or letter == "Y" or letter == "M" or letter == "N" or


letter == "O" or letter == "P":
continue

elif letter == "-":


turtle.right(lsystem_angle)

elif letter == "G" or letter == "H" or letter == "I" or letter == "J" or


letter == "K" or letter == "L":
turtle.up()
turtle.forward(distance)
turtle.down()

elif letter == "^":


turtle.left(180)

elif letter == "|":


turtle.left(90)

elif letter == "[":


push()

elif letter == "]":


pull()
else:
letter = int(letter)
turtle.pencolor(lsystem_colours[letter])

turtle.update()

print("Welcome to the Python sketchbook!")


x = 0
y = 0
finished = True
##### While loop to repeat the main menu

# Initialize the option to empty in order to enter the while loop


option = ""

while option != "q" and finished == True:


print()
print()
print("ANY TIME YOU WANT TO GO BACK PRESS B (EXCEPT FOR option 'm' and 't')")
print("FOR option 'm' and 't' PRESS 0 FOR ALL THE OPTIONS")
print()
print("Please choose one of the following options:")
print()
print("m - Move the turtle")
print("t - Rotate the turtle")
print("l - Draw a line")
print("r - Draw a rectangle")
print("c - Draw a circle")
print("p - Change the pen colour of the turtle")
print("f - Change the fill colour of the turtle")
print("w - Width of the pen")
print("s - Speed of the drawing")
print("q - quit the program")
print("d - l-systems")
print("q - Quit the program")
print()

option = input("Please input your option: ")

##### Handle the move option


if option == "m":
print()

# Ask the user for the x and y location


x = input("Please enter the x location: ")

if x == "b": # If the user wants to go back


finished = True

else:
y = input("Please enter the y location: ")
x = int(x)
y = int(y)
turtle.up()
turtle.goto(x, y)
turtle.down()

##### Handle the rotate option


elif option == "t":
print()
print()
direction = input("Do you want it on the 'left' or 'right'? ")
angle = int(input("By what angle to you want to rotate the turtle: "))
if direction == "left" or direction == "Left":
turtle.left(angle)
elif direction == "right" or direction == "Right":
turtle.right(angle)
elif direction == "b": # If the user wants to go back
finished = True

##### Handle the line option


elif option == "l":
print()

# Ask the user for the x and y location


x = input("Please enter the x location: ")

if x == "b": # If the user wants to go back


finished = True

else:
x = int(x)
y = input("Please enter the y location: ")
y = int(y)
turtle.goto(x, y) # Move the turtle and draw a line

##### Handle the rectangle option


elif option == "r":
print()

#Askes the user the basic information of the rectangle.


width = input("Enter the width of the rectangle: ")

if width == "b": # If the user wants to go back


finished = True

else:
width = int(width)
length = input("Enter the length of the rectangle: ")
length = int(length)
direction = input("Rectangle to be drawn 'clockwise' of
'anticlockwise': ")

# Ask the orientation of the rectangle.


if direction == "anticlockwise":
turtle.up()
turtle.goto(x, y)
turtle.down()
turtle.forward(length)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(length)
turtle.left(90)
turtle.forward(width)

elif direction == "clockwise":


turtle.up()
turtle.goto(x, y)
turtle.down()
turtle.forward(length)
turtle.right(90)
turtle.forward(width)
turtle.right(90)
turtle.forward(length)
turtle.right(90)
turtle.forward(width)

##### Handle the width option


elif option == "w":
print()
size = input("Choose the width size from 1 to 10: ")
if size == "b": # if the user wants to go back
finished = True
else:
size = int(size)
turtle.width(size)

##### Handle the speed option


elif option == "s":
print()
pace = input("Choose to speed of the drawing from 0 to 10: ")
if pace == "b": # if the user wants to go back
finished = True
else:
# Move the turtle and draw a line
pace = int(pace)
turtle.speed(pace)

##### Handle the circle option


elif option == "c":
print()
radius = input("Radius of your circle: ")
if radius == "b": # if the user wants to go back
finished = True

else:
radius = int(radius)
angle_of_rotation = input("What is the angle of the circle: ")
angle_of_rotation = int(angle_of_rotation)
direction = input("draw the circle \"anticlockwise\" or \"clockwise\":
")

if direction == "clockwise" or direction == "Clockwise":


radius = -radius
turtle.circle(radius, angle_of_rotation)

else:
turtle.circle(radius, angle_of_rotation)

##### Handle the lsystem option


elif option == "d":
print()
lsystem_length = input("What is the length of each string: ")
if lsystem_length == "b":
finished = True

else:
lsystem_string = input("What is your initial string: ")
lsystem_angle = input("What is the angle: ")
lsystem_iterations = input("How many iterations do you want: ")
lsystem_iterations = int(lsystem_iterations)
number = input("How many rules do you have in total: ")
number = int(number)
lsystem_rules = []

for _ in range(number):
rule = []
rule.append(input("What do you want to replace the letter with: "))
rule.append(input("What is the rule for that letter: "))
lsystem_rules.append(rule)

decision = input("Do you want to include the colours? (Enter \"yes\" or


\"no\"): ")

if decision == "yes":
number1 = input("How many colours do you want to include: ")
number1 = int(number1)
lsystem_colours = []
for _ in range(number1):
colour = ""
lsystem_colours.append(input("What colour do you want to
include (one at a time)"))
lsystem_with_color()

if decision == "no":
lsystem_with_color()

##### Handle the fill colour option


elif option == "f":
print()

#
# Please put your code here
#

turtle.done()

Potrebbero piacerti anche