Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

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 whenever we move the 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 the colour of the lines that the turtle draws, and we can 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 can tell 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.forward (50) You can also move the turtle to a specific location specified by an (x, y) coordinate: r 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. goto (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.pensize (3) The line colour can be changed by calling t.pencolor (r, g, b) where r, g. and b are values between O and 1 indicating the red, green, and blue values of the colour, O.Obe- ing none of that colour component. 10 the maximum amount of that colour component Eg. t . pencolour (1 1.0, 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 How to get started We have provided a partially complete Python program to get you 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 We've also provided a very small module called makedragon 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 90o 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' in a 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 draw () where you'll 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 draw() In the draw) 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'sjust changing the direction of the turtle R, means turn the turtle right (counter-clockwise) 90 degrees. How to colour your dragon First, just get the dragon to appear on the screen without worrying about the colour. Any colou ill 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 O and 1 (this is not the only way). These numbers represent the "amount" of colour you want to combine: zero means none, and 1 means 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,0; green is 0,10: blue is 0,0,1: black is 0.0,O: white is111. You can set a turtle's pen-colour using three random numbers. You can ask the random module to return a number between O and 1 (inclusive) using its random) function just to be clear, the module's name is random, and it contains a function of the same name random ()). 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, you'll have to do that yourself. The documentation for functions in the random 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 # 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 ireturn: a string that describes the dragon if lev = 0: retu rn 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 return d+'R'+rd "R" # 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_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 we're done ! turt Le done() 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 whenever we move the 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 the colour of the lines that the turtle draws, and we can 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 can tell 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.forward (50) You can also move the turtle to a specific location specified by an (x, y) coordinate: r 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. goto (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.pensize (3) The line colour can be changed by calling t.pencolor (r, g, b) where r, g. and b are values between O and 1 indicating the red, green, and blue values of the colour, O.Obe- ing none of that colour component. 10 the maximum amount of that colour component Eg. t . pencolour (1 1.0, 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 How to get started We have provided a partially complete Python program to get you 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 We've also provided a very small module called makedragon 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 90o 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' in a 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 draw () where you'll 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 draw() In the draw) 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'sjust changing the direction of the turtle R, means turn the turtle right (counter-clockwise) 90 degrees. How to colour your dragon First, just get the dragon to appear on the screen without worrying about the colour. Any colou ill 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 O and 1 (this is not the only way). These numbers represent the "amount" of colour you want to combine: zero means none, and 1 means 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,0; green is 0,10: blue is 0,0,1: black is 0.0,O: white is111. You can set a turtle's pen-colour using three random numbers. You can ask the random module to return a number between O and 1 (inclusive) using its random) function just to be clear, the module's name is random, and it contains a function of the same name random ()). 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, you'll have to do that yourself. The documentation for functions in the random 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 # 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 ireturn: a string that describes the dragon if lev = 0: retu rn 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 return d+'R'+rd "R" # 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_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 we're done ! turt Le done()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions