In: Computer Science
Write a python program that will allow a user to draw by inputting commands. The program will load all of the commands first (until it reaches command "exit" or "done"), and then create the drawing.
Must include the following:
change attributes:
color [red | green | blue]
width [value]
heading [value]
position [xval] [yval]
drawing:
draw_axes
draw_tri [x1] [y1] [x2] [y2] [x3] [y3
draw_rect [x] [y] [b] [h]
draw_poly [x] [y] [n] [s]
draw_path [path]
random
random [color | width | heading | position]
e.g. "random color" would set turtle to a random drawing color
Heres my starter code:
import turtle
#================ command loading =================
lines = [] #initialize empty list
# load up all commands into the list
def load_commands():
while(1):
line = input('>')
words = line.split()
if words[0] == 'exit':
break
lines.append(line)
#============== turtle drawing functions ============
def draw_rect(alex, x, y, b, h):
alex.pu()
alex.goto(x,y)
alex.pd()
for i in range(2):
alex.fd(b)
alex.lt(90)
alex.fd(h)
alex.lt(90)
#==================== process commands (do the
drawing)======
def process_commands():
# ------ initialize turtle stuff ----------------
turtle.TurtleScreen._RUNNING=True
wn = turtle.Screen() # Set up the window and its attributes
wn.bgcolor("lightgreen")
wn.title("Alex meets a function")
alex = turtle.Turtle() # Create alex
#-----------------------------------------
# process each line and draw
for line in lines:
words = line.split()
if words[0] == "draw_rect":
x = int(words[1])
y = int(words[2])
b = int(words[3])
h = int(words[4])
draw_rect(alex,x,y,b,h)
wn.exitonclick()
#================== MAIN ======================
def main():
print("loading commands")
load_commands()
print("drawing")
process_commands()
main()
PROGRAM:
OUTPUT:
SUMMARY:
Program to load commands and draw based on the commands .
Step1: Load commands()
Step2: Process command()
Step 3: Show the final result