Learn Turtle Graphics in Python

cosc 1306 computer science and programming n.w
1 / 40
Embed
Share

Discover the world of Turtle Graphics in Python with step-by-step instructions on importing the turtles package, creating colorful figures, drawing on a canvas, and mastering the art of moving and manipulating turtles to create various shapes. Dive into the basics of programming concepts using the fun and interactive Turtle module.

  • Python Programming
  • Turtle Graphics
  • Beginner-friendly
  • Drawing
  • Programming Concepts

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-Fran ois P ris jfparis@uh.edu Fall 2017

  2. THE ONLINE BOOK CHAPTER IV TURTLES

  3. Turtles? A Python package allowing novice users to draw simple colorful figures Python port of the Logo turtle Seymour Pappert Used to teach programming concepts to young children

  4. Starting up (I) Import the turtles package import turtle Create a drawing canvas mycanvas = turtle.Screen() A new instance of the Screenobject as defined in the turtle package

  5. Starting up (II) Create a turtle Alice = turtle.Turtle() A new instance of the Turtleobject as defined in the turtle package Turtle and turtle are different identifiers Python is case-sensitive Can have many turtles on the same canvas Move the turtle

  6. Example import turtle wn = turtle.Screen() # Creates a canvas alex = turtle.Turtle() # Create a turtle "alex" alex.forward(50) # Go forward 50 units alex.left(90) # Turn by 90 degrees alex.forward(30) # Go forward 50 units wn.mainloop() # Wait for user to close window

  7. Another example import turtle wn = turtle.Screen() shasta = turtle.Turtle() shasta.right(90) shasta.forward(80) shasta.left(90) shasta.forward(40) shasta.left(90) shasta.forward(80) wn.mainloop()

  8. Allowing drawing discontinuities shasta.penup() Tell the turtle shasta to start moving without leaving a trace Think of asking shasta to raise up its pen shasta.pendown() Tell the turtle shasta to return to its normal state and leave a trace Think of asking shasta to put its pen down to the paper

  9. Guess what this program draws import turtle wn = turtle.Screen() shasta = turtle.Turtle() shasta.right(90) shasta.forward(80) shasta.left(90) shasta.forward(40) shasta.left(90) shasta.forward(80) shasta.right(90) shasta.penup() shasta.forward(40) shasta.right(90) shasta.pendown() shasta.forward(80) shasta.penup() shasta.backward(40) shasta.left(90) shasta.pendown() shasta.forward(40) shasta.left(90) shasta.penup() shasta.forward(40) shasta.pendown() shasta.backward(80) wn.mainloop()

  10. The answer

  11. Do exercises in section 4.2 Teaches you to put things in the right order

  12. Making it look better Can Add color to canvas wn.bgcolor("lightgreen") Specify color and thickness of he pen of a given turtle tess = turtle.Turtle() tess.color("blue") tess.pensize(3)

  13. Another way to exit wn.exitonclick() Closes the canvas window when the user clicks his or her mouse

  14. Can have multiple turtles alice = turtle.Turtle() bob = turtle.Turtle() Each with its color and pensizeattributes alice.color("blue") bob.color("red") ...

  15. A small problem Want to congratulate several students for their project print("Congratulations, Alice!") print("Congratulations, Bob!") print("Congratulations, Carol!") Becomes quickly tedious

  16. The solution The for loop for x in ["Alice", "Bob", "Carol"] : print("Congratulations, " + x +"!") Repeats the statement(s) inside the loop for all entries in the list ["Alice", "Bob", "Carol"] assigning in turn the value of each entry tox

  17. What are lists? A truly wonderful construct of Python Extends the C/C++/Java/C# concept of array I love it and you will! Sequence of values inside square brackets separated by commas ["Alice", "Bob", "Carol"] [0, 1, 2, 3, 4, 5] [1, 3, 5, 7, 13, 17, 23]

  18. The for loop syntax for iterator in list : body of the list . . . . . . rest of the program

  19. Please note For statements end with a column Do not forget it! All statements that will be repeated Are said to be inside the loop Must all be indented by the samenumbers of spaces or tabs Must use them both in a consistent fashion Python is very very finicky Python is very very finicky

  20. Example (I) for x in ["Alice", "Bob", "Carol"] : print("Congratulations, " + x +"!") print("You did very well!") Will print two lines of congratulations for all three entries in the list Total of six lines

  21. Example (II) for x in ["Alice", "Bob", "Carol"] : print("Congratulations, " + x +"!") print("You did very well!") #outside Will print a single line of congratulations for all three entries in the list then You did very well! only once Total of four lines

  22. Example (III) for x in ["Alice", "Bob", "Carol"] : print("Congratulations, " + x +"!") print("You did very well!") #outside Will not print anything because the interpreter will refuse to execute the program

  23. Warning When you move pieces of code around in your programs Very easy to mess with the indentations in your code Python is unforgiving Must check and recheck your indentations

  24. The good Indentations make your code more concise and more legible Compare for index in list : loop body with for(weird construct) { loop body }

  25. The bad Python is unforgiving

  26. Application: drawing a square import turtle wn = turtle.Screen() alex = turtle.Turtle() for i in [0, 1, 2, 3]: # repeat 4 times alex.forward(50) alex.left(90) wn.exitonclick()

  27. Comments We could have used for i in [1, 2, 3, 4]: as the values of i are not used inside the loop It is just a convention to start all iterations with the zero value Unless you have a reason to do otherwise Shared with C/C++/Java/C#/Ruby and many other similar languages Get used to it!

  28. Babylonian method to compute square roots If ? is greater than the square root of a non- negative real number ? then ?/? will be lesser than that square (and vice versa) (? + ?/?)/ 2 will provide a better approximation Why? Because ??/? = ?

  29. Computing the square root of 2 Start with ? = 2 ?/? = 1 (? + ?/?)/2 = 1.5 Set new value of ? to 1.5 ?/? = 1.33333 (? + ?/?)/2 = 1.416666667 Stable value x = 1.414213562 after 5 steps

  30. Computing the square root of 99 Start with ? = 99 ?/? = 1 and (? + ?/?)/2 = 50 Set new value of ? to 50 ?/? = 1.8 and (? + ?/?) /2 = 25.99 Stable value x = 9.949874371 after 8 steps Use a spreadsheet to try it at home!

  31. The program """ Computes square roots """ number=float(input("Enter n >= 0: ")) root = number/2 for i in [0, 1, 2, 3, 4] : inverse = number/root root = (root + inverse)/2 print ("Its root is", root)

  32. A problem What if we need more precision? Must specify more iterations: for i in [0, 1, 2, 3, 4, 5, 8] : Becomes quickly tedious

  33. The solution range(n) Lets index go through list of n values Starting at 0 Ending at n 1 n is not in the range! Because we like to start at zero

  34. A better algorithm """ Computes square roots """ number=float(input("Enter n >= 0: ")) root = number/2 for i in range(10) : inverse = number/root root = (root + inverse)/2 print ("Its root is", root)

  35. Its output Enter a positive number: 4096 Its root is 1025.0 Its root is 514.4980487804878 Its root is 261.22960314045366 Its root is 138.4546481089779 Its root is 84.01917126201654 Its root is 66.38497483370875 Its root is 64.04284181000048 Its root is 64.0000143296318 Its root is 64.0000000000016 Its root is 64.0

  36. More turtle tricks (I) Can change the shape of the turtle alex.shape("turtle") Options include "arrow", "blank", "circle", "classic", "square", "triangle", and "turtle"

  37. More turtle tricks (II) Can change the speed of the turtle alex.speed(5) 1 is slowest and 10 is fastest Can ask the turtle to stamp a mark at its current location alex.stamp()

  38. A nice turtle program (I) import turtle wn = turtle.Screen() wn.bgcolor("lightgreen") tess = turtle.Turtle() tess.shape("turtle") tess.color("blue")

  39. A nice turtle program (II) tess.penup() size = 20 for i in range(30): tess.stamp() # Leave a mark size = size + 3 # Increase size tess.forward(size) # Move along tess.right(24) # Turn her 24o wn.mainloop()

Related


More Related Content