Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ),...

28
An Introduction to Programming Using Python David I. Schneider Turtle Graphics Section 3 Chapter 6 © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Transcript of Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ),...

Page 1: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

An Introduction to Programming Using PythonDavid I. Schneider

Turtle Graphics

Section 3Chapter 6

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 2: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Coordinates

• Objects and methods from the module named "turtle."

• Given statements,window appears

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

FIGURE 6.12 Turtle graphics

window.

Page 3: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Coordinates

FIGURE 6.13 Coordinate system for canvas.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 4: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Coordinates

• Think of the chevron as a small turtle with a pen attached to its tail

• Python statements– Raise, lower “tail”– Change color– Move turtle in variety of ways

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 5: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Functions from the turtle Module• Functions provided

– t.up( ), t.down( )– t.hideturtle( )– t.forward(distance), t.backward(distance)– t.goto(x,y)– t.pencolor(colorName)– t.setheading(deg)– t.left(deg), t.right(deg)– t.dot(diameter, colorName)

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 6: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Rectangles

FIGURE 6.14 A general rectangle.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 7: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Rectangles

• Two possible functions to draw rectangles

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 8: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Rectangles

• Two possible functions to draw rectangles

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 9: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Rectangles

• Example 1: Program draws a rectangle having a red border and a yellow interior.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 10: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Rectangles

• Example 1:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 11: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Flags

• Example 2: program draws flag shown on the right.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 12: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Flags

• Example 2:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 13: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Flags

• Example 2:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 14: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Flags

FIGURE 6.16 Five-Pointed Star

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 15: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Flags• Example 3: program draws the five-

pointed star in Fig. 6.16 (b).

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 16: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

The write Method

• Given s, a string as argument in• Displays the string s

– Bottom left corner of the string approximately at current position of the pen

• Other options

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 17: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

The write Method

• Example 4: Program displays the word Python with different alignments.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 18: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Bar Charts

• Example 5: Program creates bar chart on the right.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 19: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Bar Charts

• Example 5:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 20: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Bar Charts

• Example 5:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 21: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Bar Charts

• Example 5:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 22: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Line Charts

• Tabular data can be visually displayed it in a line chart.

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Table 6.3 Percentage of college freshmen who smoke.

Page 23: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Line Charts

• Example 6: Program uses data in Table 6.3 to create the line chart on the right

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 24: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Line Charts

• Example 6:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 25: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Line Charts

• Example 6:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 26: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Line Charts

• Example 6:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 27: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

Line Charts

• Example 6:

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.

Page 28: Turtle Graphics - poullis.orgFunctions from the turtle Module •Functions provided –t.up( ), t.down( ) –t.hideturtle( ) –t.forward(distance), t.backward(distance) –t.goto(x,y)

An Introduction to Programming Using PythonDavid I. Schneider

End

Section 3Chapter 6

© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.