Details of this Paper

how do you use python's turtle module to draw a blue pentagon




how do you use python's turtle module to draw a blue pentagon at the position of each left click and and a red triangle at every right mouseclick?;Additional Requirements;Level of Detail: Show all work;Other Requirements: CSCI 1040 Laboratory Five;Procedures, Modules and Turtles!;Introduction;In the last laboratory we introduced the concept of modules and showed how you could create your own module and use modules that were created by others. In this laboratory we will expand on this concept by making extensive use of one of the standard Python modules. You will also have a chance to write some more procedures, remember practice is important in learning how to program. As an added bonus we will also learn about turtle graphics and use it to create pictures.;Turtle Graphics;Turtle graphics was developed many years ago to teach children about programming concepts. The original turtles were small robots that drew pictures on large pieces of paper with a small collection of coloured markers. Now we simulate these turtles on the computer screen, which I think is a lot less fun.;Turtle graphics is based on the idea of a turtle with a pen attached to its tail, in a humane and ecologically friendly way. As the turtle moves, with the pen down it leaves a trail of ink everywhere it goes. We can tell the turtle to raise or lower its tail, so we have some control over where the turtle draws. We can also tell the turtle to go forwards, go backwards and turn. This is how we get the turtle to draw different shapes. There are a number of other things that we can tell the turtle to do, some of which we will see later in this laboratory.;To get started with turtle graphics you need to import the turtle module, using an import statement like we used last laboratory. This is shown in figure 1. Once we have imported the turtle module we can start giving the turtle commands. The simplest one is the forward command. Type turtle.forward(100) and see what happens. Turtles start at the middle of the screen, this is called their home position, and they are pointing to the right. When we type in this command the turtle draws a line 100 units to the right. Now type turtle.right(120) and see what happens to the turtle. This causes the turtle to turn in a clockwise direction by 120 degrees. Repeat these two statements two more times and see what shape the turtle draws.;If we want the turtle to go backwards we can either use a negative parameter to forward() or use the back() procedure with a positive number. Similarly, to turn counter-clockwise we can pass a negative number to right(), or pass a positive number to left(). The penup() procedure is used to raise the turtle?s pen, so it doesn?t draw as it moves, and the pendown() procedure is used to lower the pen to the paper again. This is all you really need to draw a large number of interesting shapes. The bye() procedure can be used to close the turtle window when you are finished drawing.;Figure Some Basic Turtle Commands;Turtle Procedures;Typing in the commands to draw a shape at the Python shell is interesting the first time or two, but it quickly gets tedious. This means that we really need to have some procedures that will draw interesting shapes for us. We will start by producing a procedure that draws a regular polygon. In a regular polygon all the sides are the same length and all the angles between the sides are the same. The triangle that we drew in the previous section is a regular polygon, and gives us a hint of how we can construct our procedure.;We want a procedure that can draw any regular polygon, so the first parameter to our procedure will be the number of sides in the polygon. We also want to be able to change the size of the polygons, so the second parameter is the length of each side. At the start of the procedure we know how many sides we will need to draw, so this suggests a for statement, where inside the for statement we will draw one side of the polygon. Once we have drawn a side we need to turn the turtle so it is ready to draw the next side. The question is how much does the turtle need to turn for each side? Handy fact: the angles in a regular polygon must add up to 360 degrees. Thus the amount that we need to turn on each side is 360 divided by the number of sides.;Putting all of this together we get the procedure shown in figure 2. We start by importing the turtle module and then we have the definition of our poly procedure. We have two parameters, the number of sides and the length of each side. We have decided to use a default value for the length of each side to save on typi,ng later. The first thing we do in this procedure is compute the angle that the turtle must turn at the end of each side, and store this value in the dangle variable. Next we have a for statement that repeats the statements inside of it once for each side of the polygon. Inside the for statement we use forward() to draw the side followed by right() to turn the turtle at the end of the side.;Once you have finished entering the procedure, use Run Module to run it in the Python shell and call poly() with several different parameter values. You may want to move the turtle between calls to poly() so the polygons aren?t drawn on top of each other.;Figure The Regular Polygon Procedure;For our next example we will write a procedure for drawing square spirals. In a square spiral the angle between the legs or sides is 90 degrees and the length of the sides increases as we get further from the centre of the spiral. A square spiral with 200 sides is shown in figure 3.;Figure A Square Spiral;The square spiral procedure is similar to our procedure for drawing regular polygons. We have a for statement where the statements inside of the for statement draw one side of the spiral. In this case the turtle always turns 90 degrees at the end of each side. We also want the length of the side to increase as we draw the spiral. There is an easy way to do this. The for statement has a variable i, that goes from 1 to the number of sides by adding one to i each time the statements in the for statement are executed. This is exactly what we need for the length of the spiral sides. Our complete square spiral procedure is shown in figure 4.;Figure Square Spiral Procedure;Again try this procedure in the Python shell using the Run Module command. It takes the turtle some time to draw the spiral, so be patient.;Our first version of the poly() procedure, shown in figure 2 draws the outline of the polygon, but it does nothing with the inside of the polygon. Setting the inside of a polygon to a solid colour is called filling. Our next version of the poly() procedure draws a filled regular polygon. To do this we need to know how to change the colour that the turtle is drawing with. There are several procedures that help us with this. The pencolor() procedure is used to change the colour of the pen that the turtle is using. The fillcolor() procedure is used to change the colour of the area enclosed by the shape that the turtle draws. Finally, the color() procedure can be used to change both of these colours. The turtle also needs to know the shape that its needs to fill with colour. Before we start drawing this shape we call the begin_fill() procedure, and when we are finished drawing the shape we call the end_fill() procedure.;To produce our filled polygon procedure we start with our original poly() procedure and add the Python statements required to get a filled shape. The first thing that we do is add another parameter to the poly() procedure, which is the colour of the filled polygon. Inside this procedure we call the fillcolor() procedure before we start drawing the shape to tell the turtle the colour of the filled polygon. Just before the for statement we add a call to the begin_fill() procedure to indicate that the following turtle commands will be used to draw the filled shape. Finally after the for statement we add a call to the end_fill() procedure to indicate that we have finished drawing the filled shape.;Once you have finished entering the code for the new poly() procedure use Run Module to define the procedure in the Python shell and draw several filled polygons. For the colour parameter try things like ?red?, ?blue? or ?yellow?.;Figure Version of Poly that fills the polygon;Playing With Turtles;So far we have let the turtles run free, we write a procedure and then run the procedure, but otherwise we have no control over the turtle while it is drawing. Now we will look at how we can use our mouse to control what the turtle draws. We are going to produce a very simple drawing program using our turtles to do the drawing. In this program pressing the left mouse button over the turtle and dragging the mouse will draw a line on the screen. Pressing the right mouse button and dragging the mouse will move the turtle to a new location without drawing anything. But, how do we get the turtle to respond to the mouse?;We can have the mouse call one of our Python procedures whenever the user does something to the mouse. For example, each time the user presses the left mouse button, one of our Python procedures will be called. We can do the same thing whenever the mouse moves with a button pressed or when the user releases a button. So, in our drawing program, when the user moves the mouse with the left mouse button pressed we want to call a procedure that moves the turtle to the current mouse position drawing a line in the process.;This is the basis of our drawing program shown in figure 6. We have four procedures that respond to different user actions. Each of these procedures has two parameters, the x and y coordinates of the mouse?s current position. The moveEvent() procedure is called when the user moves the mouse with the right button pressed. This moves the turtle without drawing a line. This procedure starts by calling penup() to make sure that we don?t draw while we move. Then we call goto() with the current mouse position to move the turtle to that position. That?s all we need to do to have the turtle follow the mouse. The drawEvent() procedure is very similar and is called when the user moves the mouse while pressing the left mouse button. It makes sure that the pen is down and then moves the turtle to the current mouse position.;Figure A Simple Turtle Based Drawing Program;The next two procedures are a bit of fluff that makes the program look nicer. When we draw a line in our program the turtle shape changes to look like a turtle, and when we stop drawing it goes back to its original shape. The clickEvent() procedure is called when the user presses the left mouse button. The single line in this procedure changes the turtle shape to be a turtle. The releaseEvent() procedure is called when the button is released and it returns the turtle to its original shape.;Now we need to tell the turtle which procedures to use. The ondrag() procedure associates one of our Python procedures with moving the mouse with one of the buttons pressed. The first parameter to this procedure is the procedure to be called when the mouse moves and the second parameter is the button that the user must press. We use 1 for the left mouse button and 3 for the right mouse button. The onclick() procedure associates one of our procedures with the user pressing a mouse button. The first parameter to this procedure is the procedure to be called when the button is pressed and the second parameter is the button that the user must press. The onrelease procedure does basically the same thing, except its first parameter is called when the mouse button is released.;In our previous examples the turtle didn?t draw at full speed, instead it was animated to show how our programs work. Now that we are controlling the turtle with the mouse we no longer want it animated, instead it should immediately follow the mouse. To change this behavior we call the speed() procedure with a parameter of 0, so the turtle will move as fast as possible. Finally to get the whole thing started we call the mainloop() procedure.;Now you can run the program by using the Run Module command. The program will start running immediately and you will be able to interact with the turtle. To exit from the program press ctrl-C in the Python shell;Laboratory Report;For this laboratory you will write a slightly different drawing program putting together some of the procedures and programs that we have written in this laboratory. The drawing program in figure 6 draws lines as you drag the mouse. The new drawing program that you will write will draw a blue pentagon at the position of every left mouse click, and a red triangle at the position of every right mouse click. You can use the second poly() procedure in your program. There is one more turtle procedure that you will need to know. The onclick() procedure only calls your Python procedure if you press the button over the turtle. This won?t work for our program, since we want to be able to press the button anywhere on the screen. Instead use the onscreenclick() procedure, which has the same parameters as the onclick() procedure, but will call our Python procedure every time you click the mouse button regardless of where the mouse is located on the screen.


Paper#17486 | Written in 18-Jul-2015

Price : $57