Given files:
1)Starter file:
# Dragons!
# This is a starter file!
# Look for comments indicating where you will do your work.
# 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.setheading(heading)
# 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 dragon's shape is defined by its level
# Try different values here.
# Don't 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_size = 500
# The length of a dragon segment is determined by
# its level, and its size
segment_size = dragon_size / (2 ** (level / 2))
# to make low level dragons colourful
# use a wider pen for smaller dragons
turtle.pensize(25 // 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 your dragon differently!
heading = 45 * (level - 1)
# make a dragon of the desired level
my_dragon = dragonsegg.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 we're done!
turtle.done()
2) make dragon file
# 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. That's Chapter 16.
# Don't worry if you don't 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 == 0:
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
Intro: Turtle Graphics In this question we will introduce you to turtle graphics. A turtle is a cute name for an entity that we can move around a drawing canvas to create pictures. The turtle has a pen which can be in either the up or down positions. When the turtle's pen is in the down position, the pen draws wheneverwe movethe turtle. We can draw a line by, say. moving the turtle 50 pixels to the right while the pen is down. We can also change thecolour of the lines that the turtle draws, and wecan even ask the turtle to draw specific shapes, such as a circle. When the pen is up, we can move the turtle without it drawing anything We can add turtle graphics to a program by importing the turtle module import turtle as t Now we can access the turtle module functions though the object t. For example, we cantell the turtle to put its pen down, move 50 pixels in the direction it is currently facing (drawing a 50-pixel long line in the process), and then lift its pen up t . down ( t. forvard (50) t. up) You can also move the turtle to a specific location specified by an (,) coordinate: a is the horizontal position, y is the vertical position. The center of the canvas window is (0,0). For example, we could move the turtle directly to position (20, 20) (down and to the right of the center) by calling t got o (20, 20) If the pen is down, then this will also draw a line from the turtle's current position to the position (20, 20) We can tell the turtle to draw lines of a given width (if the pen is down) like this t penaize (3) The line colour can be changed by calling t. pencolor(r, g, b) where r.g, and b are values between O and 1indicatingthe red. green, and blue values of the colour, O0 be- ing none of that colour component10 the maximum amount of that colour component E gt. pen colour ( 1 . 0, 1.0, .0) gives bright yellow (since red-green-yellow). If you'd like another example, Lecture 6, demo 2 shows a complete program that uses turtle graphics to draw a circle wherever the user clicks the mouse. It uses all of the turtle functions described here For this question you will visualize some data stored in a string using turtle graphics Problem Overview: Drawing Dragon Curves A dragon curve is a fascinating mathematical object, with many interesting properties We're mostly going to use it as an exercise to draw pictures using turtle graphics, but if you want to learn more, you cant go wrong starting with Wikipedia's introduction How to get started We have provided a partially complete Python progr am to getyou started a3q1-starter py. You can find it on the Moodle page as a resource for Assignment 3. Download it and add it to your Python project Weve also provided a very small module called nakedragon that produces the information you'll need to draw a dragon. You can download the module from the course Moodle page. This is not a built-in Python module! It should be placed in the same project or folder as your a3q1-starter-py. It can be imported the same way as any Python module, so long as it is in the same folder as a3q1-starter py. You can look at this file if you want, but you don't need to understand the contents of this file to complete the assignment The starter code imports this module as dragonsegg The function dragon) in the makedragon module takes one argument, which must be a positive integer. This integer describes the level of the the dragon you want, and should be in the range from O to about 18 (if you give an integer that's any higher than this, you might have to wait a very long time). The function returns a string consisting of only of the characters 'F' (meaning move Forward). 'L' (meaning turn Left 90 degrees), and R (meaning turn Right 90 degrees). This string describes the actions that the turtle needs to take to draw a dragon For example, calling dragon (4) returns the string 'FRFRFLFRFRFLFLFRFRFRFLFLFRFLFLF". You will use the string to tell the turtle where to go, and what to draw. Notice that 'F' alternates with either 'L' or 'R There are never two 'F' in a row. If we ignore the 'F', we can see one, two, or three R ina row, but never more. And sometimes we see one, two, or three 'L in a row (again, ignoring 'F'). The string starts with two 'R', and ends with two 'L' in a row The starter file a3q1-starter.py defines a function called drav) where youll add your code to draw the dragon. The rest of the file creates a few variables and calculates a few necessary values Near the bottom of the file is a function call to drav() In the drav) function, there is a comment that tells you to write some code to draw the dragon curve. Use a loop! Look at each character in the string ,F' means move forward. This is where the drawing happens. The amount you move forward deter- mines how big the dragon will appear on the screen. 'L' means turn the turtle left (clockwise) 90 degrees. No drawing happens here: it's just changing the direction of the turtle . 'R' means turn the turtle right (counter-clockwisel 90 degrees. to colour your dragon First, just get the dragon to appear on the screen without worrying about the colour. Any colour will do When you can correctly draw the dragon curve, you can think about how to colour it We want you to colour your dragon using the following arbitrary rule: Every time the string changes from L' to 'R' or from 'R' to 'L' (ignoring the 'F' between them), change the pen colour to a new random colour. This rule is a bit tricky, so think about strings and indices carefully! A colourized dragon curve with level 4 should resemble the following diagram It's vaguely dragon-shaped. And there are sequences of lines that are the same colour. Notice the almost black lines near the top (because there are two R in a row at the start of the string) and the three magenta lines in the middle (another sequence of three R in the middle of the string). Higher level Dragon curves are more interesting! But start with low level dragons first, because drawing takes a long time! Getting random colours Colours can be described by numbers. Perhaps you've learned something about how primary colours combine (red yellow make orange, etc). One way to do this on a computer is to use numbers between 0 and 1 (this is not the only way) These numbers represent the 'amount' of colour you want to combine; zero means none, and 1means all. We need three such numbers to represent all the colours a computer can use, in terms of how much red, green, and blue to combine Red is 1..0; green is O1.0, blue is 0.0,1 black is 0,00:white is 1.1.1 You can set a turtle's pen-colour using three random numbers. You can ask the rand om module to return a number between O and 1 (inclusive) using its random) function just to be clear, the module's name b rand om, and it contains a function of the same namerandom()). The number is random in the sense that you cannot predict what it will return, and it will change every time you ask for one. The starter code does not import the random module youll have to do that yourself. The documentation for functions in the rand om module can be found here (click to link) Other Hints The documentation for the turtle module containing descriptions of how to use the turtle module func- tions can be found here (click to link). Click on the name of a function to get more details When you run a program that uses turtle graphics, it pops up a canvas window on the screen. This window will often hide behind other windows (such as PyCharm) so if you think nothing is happening when you run your program, make sure you move your windows and look behind them. Also, the program is only terminated when you close the canvas window, so make sure you close the window between runs or you may get confused by having multiple drawing canvases open at the same time. Intro: Turtle Graphics In this question we will introduce you to turtle graphics. A turtle is a cute name for an entity that we can move around a drawing canvas to create pictures. The turtle has a pen which can be in either the up or down positions. When the turtle's pen is in the down position, the pen draws wheneverwe movethe turtle. We can draw a line by, say. moving the turtle 50 pixels to the right while the pen is down. We can also change thecolour of the lines that the turtle draws, and wecan even ask the turtle to draw specific shapes, such as a circle. When the pen is up, we can move the turtle without it drawing anything We can add turtle graphics to a program by importing the turtle module import turtle as t Now we can access the turtle module functions though the object t. For example, we cantell the turtle to put its pen down, move 50 pixels in the direction it is currently facing (drawing a 50-pixel long line in the process), and then lift its pen up t . down ( t. forvard (50) t. up) You can also move the turtle to a specific location specified by an (,) coordinate: a is the horizontal position, y is the vertical position. The center of the canvas window is (0,0). For example, we could move the turtle directly to position (20, 20) (down and to the right of the center) by calling t got o (20, 20) If the pen is down, then this will also draw a line from the turtle's current position to the position (20, 20) We can tell the turtle to draw lines of a given width (if the pen is down) like this t penaize (3) The line colour can be changed by calling t. pencolor(r, g, b) where r.g, and b are values between O and 1indicatingthe red. green, and blue values of the colour, O0 be- ing none of that colour component10 the maximum amount of that colour component E gt. pen colour ( 1 . 0, 1.0, .0) gives bright yellow (since red-green-yellow). If you'd like another example, Lecture 6, demo 2 shows a complete program that uses turtle graphics to draw a circle wherever the user clicks the mouse. It uses all of the turtle functions described here For this question you will visualize some data stored in a string using turtle graphics Problem Overview: Drawing Dragon Curves A dragon curve is a fascinating mathematical object, with many interesting properties We're mostly going to use it as an exercise to draw pictures using turtle graphics, but if you want to learn more, you cant go wrong starting with Wikipedia's introduction How to get started We have provided a partially complete Python progr am to getyou started a3q1-starter py. You can find it on the Moodle page as a resource for Assignment 3. Download it and add it to your Python project Weve also provided a very small module called nakedragon that produces the information you'll need to draw a dragon. You can download the module from the course Moodle page. This is not a built-in Python module! It should be placed in the same project or folder as your a3q1-starter-py. It can be imported the same way as any Python module, so long as it is in the same folder as a3q1-starter py. You can look at this file if you want, but you don't need to understand the contents of this file to complete the assignment The starter code imports this module as dragonsegg The function dragon) in the makedragon module takes one argument, which must be a positive integer. This integer describes the level of the the dragon you want, and should be in the range from O to about 18 (if you give an integer that's any higher than this, you might have to wait a very long time). The function returns a string consisting of only of the characters 'F' (meaning move Forward). 'L' (meaning turn Left 90 degrees), and R (meaning turn Right 90 degrees). This string describes the actions that the turtle needs to take to draw a dragon For example, calling dragon (4) returns the string 'FRFRFLFRFRFLFLFRFRFRFLFLFRFLFLF". You will use the string to tell the turtle where to go, and what to draw. Notice that 'F' alternates with either 'L' or 'R There are never two 'F' in a row. If we ignore the 'F', we can see one, two, or three R ina row, but never more. And sometimes we see one, two, or three 'L in a row (again, ignoring 'F'). The string starts with two 'R', and ends with two 'L' in a row The starter file a3q1-starter.py defines a function called drav) where youll add your code to draw the dragon. The rest of the file creates a few variables and calculates a few necessary values Near the bottom of the file is a function call to drav() In the drav) function, there is a comment that tells you to write some code to draw the dragon curve. Use a loop! Look at each character in the string ,F' means move forward. This is where the drawing happens. The amount you move forward deter- mines how big the dragon will appear on the screen. 'L' means turn the turtle left (clockwise) 90 degrees. No drawing happens here: it's just changing the direction of the turtle . 'R' means turn the turtle right (counter-clockwisel 90 degrees. to colour your dragon First, just get the dragon to appear on the screen without worrying about the colour. Any colour will do When you can correctly draw the dragon curve, you can think about how to colour it We want you to colour your dragon using the following arbitrary rule: Every time the string changes from L' to 'R' or from 'R' to 'L' (ignoring the 'F' between them), change the pen colour to a new random colour. This rule is a bit tricky, so think about strings and indices carefully! A colourized dragon curve with level 4 should resemble the following diagram It's vaguely dragon-shaped. And there are sequences of lines that are the same colour. Notice the almost black lines near the top (because there are two R in a row at the start of the string) and the three magenta lines in the middle (another sequence of three R in the middle of the string). Higher level Dragon curves are more interesting! But start with low level dragons first, because drawing takes a long time! Getting random colours Colours can be described by numbers. Perhaps you've learned something about how primary colours combine (red yellow make orange, etc). One way to do this on a computer is to use numbers between 0 and 1 (this is not the only way) These numbers represent the 'amount' of colour you want to combine; zero means none, and 1means all. We need three such numbers to represent all the colours a computer can use, in terms of how much red, green, and blue to combine Red is 1..0; green is O1.0, blue is 0.0,1 black is 0,00:white is 1.1.1 You can set a turtle's pen-colour using three random numbers. You can ask the rand om module to return a number between O and 1 (inclusive) using its random) function just to be clear, the module's name b rand om, and it contains a function of the same namerandom()). The number is random in the sense that you cannot predict what it will return, and it will change every time you ask for one. The starter code does not import the random module youll have to do that yourself. The documentation for functions in the rand om module can be found here (click to link) Other Hints The documentation for the turtle module containing descriptions of how to use the turtle module func- tions can be found here (click to link). Click on the name of a function to get more details When you run a program that uses turtle graphics, it pops up a canvas window on the screen. This window will often hide behind other windows (such as PyCharm) so if you think nothing is happening when you run your program, make sure you move your windows and look behind them. Also, the program is only terminated when you close the canvas window, so make sure you close the window between runs or you may get confused by having multiple drawing canvases open at the same time