Introduction¶“Turtle” is a python feature like a drawing board, which lets you command a turtle to draw all over it! Show You can use functions like Before you can use turtle, you have to import it. We recommend playing around with it in the interactive interpreter first, as there is an extra bit of work required to make it work from files. Just go to your terminal and type: Note Not seeing anything on Mac OS? Try issuing a command like Note Do you work with Ubuntu and get the error message “No module named
_tkinter”? Install the missing package with Note While it might be tempting to just copy and paste what’s written on this page into your terminal, we encourage you to type out each command. Typing gets the syntax under your fingers (building that muscle memory!) and can even help avoid strange syntax errors. The Note Want to start fresh? You can type The standard
turtle is just a triangle. That’s no fun! Let’s make it a turtle instead with the So much cuter! If you put the commands into a file, you might have recognized that the turtle window vanishes after the turtle finished its movement. (That is because Python exits when your turtle has finished moving. Since the turtle window belongs to Python, it goes away as well.) To prevent that, just put import turtle turtle.shape("turtle") turtle.forward(25) turtle.exitonclick() Note Python is a programming language where horizontal indenting of text is important. We’ll learn all about this in the Functions chapter later on, but for now just keep in mind that stray spaces or tabs before any line of Python code can cause an unexpected error. You could even try adding one to check how python will complain! Drawing a square¶Note You’re not always expected to know the anwer immediately. Learn by trial and error! Experiment, see what python does when you tell it different things, what gives beautiful (although sometimes unexpected) results and what gives errors. If you want to keep playing with something you learned that creates interesting results, that’s OK too. Don’t hesitate to try and fail and learn from it! Exercise¶Draw a square as in the following picture: For a square you will probably need a right angle, which is 90 degrees. Solution¶turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) Note Notice how the turtle starts and finishes in the same place and facing the same direction, before and after drawing the square. This is a useful convention to follow, it makes it easier to draw multiple shapes later on. Bonus¶If you want to get creative, you can modify your shape with the Tip Are you seeing an error like this: NameError: name 'turtle' is not defined when trying to view help? In Python you have to import names before you can refer to them, so in a new Python interactive shell you’ll need to Another way to find out about functions is to browse the online documentation. Caution If you misdrew anything, you can tell turtle to erase its drawing board with the Tip As you might have read in the help, you can modify the color with If you want to set an RGB value, make sure to run Drawing a rectangle¶
Exercise¶Can you draw a rectangle too? Solution¶turtle.forward(100) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(50) turtle.left(90) Bonus¶How about a triangle? In an equilateral triangle (a triangle with all sides of equal length) each corner has an angle of 60 degrees. More squares¶Exercise¶Now, draw a tilted square. And another one, and another one. You can experiment with the angles between the individual squares. The picture shows three 20 degree turns. But you could try 20, 30 and 40 degree turns, for example. Solution¶turtle.left(20) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.left(30) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.left(40) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(50) turtle.left(90) Can you draw with Python?This is just one of many different drawings you can make using the Python turtle library. Most developers use turtle to draw shapes, create designs, and make images.
How do you make anime in Python?You can create animations in Python by calling a plot function inside of a loop (usually a for-loop). The main tools for making animations in Python is the matplotlib. animation. Animation base class, which provides a framework around which the animation functionality is built.
How do you draw someone's face in Python?Following steps are used :. Import turtle.. Make objects.. Draw a circle and fill yellow color.. Draw eyes with two circles and fill white and black color respectively.. Draw circle for nose and fill black color.. Draw semi circle for mouth.. Draw semi circle for tongue and fill red color.. How do you make a sun in Python?Approach:. Import turtle module.. Set up a screen for turtle.. Instantiate a turtle object.. For making sun, define a method for circle along with radius and color.. Define a function for creating sun rays.. |