1. Engineering
  2. Computer Science
  3. the program language used python 3 i need to add...

Question: the program language used python 3 i need to add...

Question details

The program language used: python 3

I need to add a loop code to get a dragon curve using the turtle module.Most of the code is written below, it indicates where to add the "loop code" to finish the program. Also I need to color the dragon curves with random colours by asking the random module to return a number between 0 and 1 (inclusive) using its random() function

# Python modules import turtle as turtle # Module supplied by CMPT 141 import makedragon as dragonsegg def draw(dragon, x, y, segsize, heading): Given a string describing a dragon, use Turtle graphics to draw its shape :param dragon: A string that describes the shape ofthe dragon. :param x, y: the Turtle coordinates to start drawing the dragon :param segsize: How big each segment of the dragon is, in Turtle pixels. :param heading: the orientation of the first segment of the Dragon return: None turtle.up() turtle.goto(x, y) turtle.down() turtle.setheadinal ADD CODE TO THIS FUNCTION # this is where you draw the dragon! # use a loop! # main program # set up the turtle to draw as fast as possible turtle.delay (0) turtle.speed (0) turtle.hideturtle) # A dragons shape is defined by its level # Try different values here # Dont try anything above 20 here! level - 4 # Now we can decide how big the dragon should be # from nose to tail in screen pixels # 500 pixels fills my default turtle window nicely # Adjust to taste! dragon_size500

# The length of a dragon segment is determined by # its level, and its size segment size-dragon_size (2 (evel 2)) # to make low level dragons colourful # use a wider pen for smaller dragons turtle.pensize(25 /1 level) # To draw a dragon from upper left to lower right # we have to set the turtle to the appropriate direction # Play with this value to orient you r dragon differently! heading 45 * ( level-1) # make a dragon of the desired level my_dragondragonsegg. dragon (level) # now at long last, draw the dragon! # starting a little to the left and a little up # adjust to taste! draw(my_dragon, -140, 140, segment_size, heading) # tell the turtle were done! turtle.done ()

# This module consists of a single function, # whose purpose is to create a string from which we can # draw a dragon. # A dragon curve is defined here to consist of 3 characters: # F means to draw a straight line of a constant size R means to make a 90 degree turn to the Right (clockwise) L means to make a 90 degree turn to the Left (counter-clockwise) # # Dragon curves have many interesting properties # The function here is recursive. Thats Chapter 16. # Dont worry if you dont understand what it is doing! def dragon(lev) Produces the DNA for a dragon of level lev param lev: the level of the dragon return: a string that describes the dragon if lev - : return F else: # produce DNA for a smaller dragon d - dragon (lev-1) # make a copy, reverse it, and swap the L and R rd-d [ : :-1] . replace ( R , T ) . replace ( L , R ) . replace(T , L) # construct the DNA from the two , joining them with an R return d+R +rd

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution