Setting position turtle python

turtle — Turtle graphics¶

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.

Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle , give it the command turtle.forward(15) , and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25) , and it rotates in-place 25 degrees clockwise.

Turtle can draw intricate shapes using programs that repeat simple moves.

../_images/turtle-star.png

from turtle import * color('red', 'yellow') begin_fill() while True: forward(200) left(170) if abs(pos())  1: break end_fill() done() 

By combining together these and similar commands, intricate shapes and pictures can easily be drawn.

The turtle module is an extended reimplementation of the same-named module from the Python standard distribution up to version Python 2.5.

It tries to keep the merits of the old turtle module and to be (nearly) 100% compatible with it. This means in the first place to enable the learning programmer to use all the commands, classes and methods interactively when using the module from within IDLE run with the -n switch.

The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support.

The object-oriented interface uses essentially two+two classes:

  1. The TurtleScreen class defines graphics windows as a playground for the drawing turtles. Its constructor needs a tkinter.Canvas or a ScrolledCanvas as argument. It should be used when turtle is used as part of some application. The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when turtle is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible. All methods of TurtleScreen/Screen also exist as functions, i.e. as part of the procedure-oriented interface.
  2. RawTurtle (alias: RawPen ) defines Turtle objects which draw on a TurtleScreen . Its constructor needs a Canvas, ScrolledCanvas or TurtleScreen as argument, so the RawTurtle objects know where to draw. Derived from RawTurtle is the subclass Turtle (alias: Pen ), which draws on “the” Screen instance which is automatically created, if not already present. All methods of RawTurtle/Turtle also exist as functions, i.e. part of the procedure-oriented interface.

The procedural interface provides functions which are derived from the methods of the classes Screen and Turtle . They have the same names as the corresponding methods. A screen object is automatically created whenever a function derived from a Screen method is called. An (unnamed) turtle object is automatically created whenever any of the functions derived from a Turtle method is called.

To use multiple turtles on a screen one has to use the object-oriented interface.

In the following documentation the argument list for functions is given. Methods, of course, have the additional first argument self which is omitted here.

Источник

Python – How To Set Starting Position in Turtle

How To Set Starting Position In Turtle

Python proves its abilities in a wide variety of fields. It is good for heavy computational jobs like Data Science and Machine Learning. It is great for creating computer networks and APIs. It is even great for creating graphics or games.

Today, we’re going to explore one of the most simple yet extremely powerful libraries of Python – Turtle. On our way, we’ll learn how to set the starting position of a drawing in Turtle and draw some simple shapes to get familiar with the syntaxes and functions of Turtle. So let’s get started with it.

What is Turtle?

Turtle is an animation and graphics development module that is a part of Python’s standard library. It helps in drawing complex shapes and animation with few simple functions. Python programmers often compare it with the famous Python library known as pygame. There are differences between pygame and turtle, but both have their own benefits. We’ll compare both at the end.

History of Turtle

In the year 1967, when computer programming was in its infancy, Wally Feurzeig, Seymour Papert, and Cynthia Solomon created a Logo programming language. The turtle was a part of this language and was created to introduce programming to kids. It had just two functions. Turtle.forward() and Turtle.right() .

Consider a turtle sitting on your computer screen. Now this turtle wants to move. When you use the function, Turtle.forward(10), the turtle moves 10 pixels in the direction it was originally facing. On its way, it draws a line on the screen. Wherever the turtle moves, it keeps drawing a line. Consider it a pen that never lifts. Now eventually, the turtle will hit the end of the screen. So it needs to rotate at some point. So for that, there’s the Turtle.right() function. You can rotate the turtle in the clockwise direction for 10 degrees by entering Turle.right(10) .

By just these two simple commands, complex shapes and drawings can be created.

Python’s Turtle module is based on this Turtle and has functions similar to these with many more complex functions. It depends on the tkinter library. So it needs a Python version that has a tkinter installed with it. There was a different Turtle module till Python 2.7. The turtle module till Python 2.7 had uppercase “T” in its name – “Turtle”. The turtle module in the newer versions of Python has lowercase “t” making it “turtle”. The newer version of the turtle is almost completely compatible with the older version.

Functioning of the turtle module

turtle has both object-oriented and procedure-oriented methods to create turtle graphics. It has mainly two classes – TurtleScreen() to create a window and Rawturtle() uses a canvas to draw. A subclass RawTurtle() known as Turtle is used to draw on the Screen . Both of these classes use Tkinter for basic graphics.

It has methods to control the motion of the turtle, the state of the turtle, control the pen, control the window and animation. There are more than 100 functions in the module to control every aspect of the graphic.

How to set up the turtle window

To set up the turtle window, first, we will import the turtle module.

After importing the turtle module, we’ll use the Screen() method of the turtle module to create a playground for our turtle.

Now that we have our screen, we’ll set up the size of the screen, the color of the playground, and the title of the window using the setup() , bgcolor() and title() , methods.

screen.setup(width=400, height=300) screen.bgcolor("white") screen.title("Turtle Window")

We would also need to exit the window at some point. So for that, we’ll use the exitonclick() method of the Screen class.

When we run the program, a turtle window will be launched with the mentioned configurations.

Sample Turtle Window

There we go! The [playground for our turtle is ready. Now let’s see to set the starting position of our turtle.

First, let’s check the current position of our turtle using the position() method.

Python Check Position Of The Turtle

So when we print the position of the turtle we get the coordinates (0.00,0.00) and an arrow appears on the turtle window facing towards the right and placed at the center of the window. This arrow is our turtle. That means if we use the turtle.forward() method right now, it will move towards the right the number of units mentioned from the center of the playground.

Now we can change the position of the turtle using the setx() , sety() method or the goto() method. But the problem with it is that if we use it right now so it will draw a line. So we will need to lift up our turtle first. Let’s see how we can set the starting position.

Set the starting position with the goto() method

We can set the starting position of our turtle by first lifting the turtle up with the penup() method and then using the goto() method to move it to a particular position.

turtle.penup() turtle.goto(100,100)

Setting The Starting Position Of The Turtle

So our turtle moved to the requested position. Now you can use the pendown() method to drop the turtle and start drawing what you want. Another way of doing this is using the setX() and the setY() methods.

Using the setx() and sety() method to set the starting position

Using the setx and sety method is very simple. Here you set the x and y coordinates separately.

Setx And Sety Method Turtle

The difference between setx() sety() method and goto method is that setx sety method provides more flexibility than the goto method. You can see in the above clip that it first moves 100 pixels in the positive x direction and then 100 pixels in the positive y direction. But the final position of the turtle remains the same in both methods.

How to draw something using in turtle?

Now that we have a starting position let’s try to draw something.

To draw something, first, we will have to drop our turtle using the pendown() method.

Now we can either go forward or backward using the forward() and backward() methods or we can rotate the turtle using the right() and left() methods. Using these methods, let’s try to draw a square and a circle.

import turtle screen = turtle.Screen() screen.setup(width=400, height=300) screen.bgcolor("white") screen.title("Turtle Window") turtle.forward(50) turtle.right(90) turtle.forward(50) turtle.right(90) turtle.forward(50) turtle.right(90) turtle.forward(50) turtle.penup() turtle. goto(-25, -25) turtle.pendown() turtle.circle(25) screen.exitonclick()

To draw a square, first, we moved 50 pixels towards the right, then rotated our turtle 90 degrees, and then repeated the same thing for 3 more times. Then we moved the turtle to a different location and made a circle using the circle() method.

Exiting a turtle window

To exit the turtle window, we simply can click the cross button like any other desktop application or we can use the ctrl+C command in our terminal.

Pygame vs. Turtle – Which is better?

Pygame and Turtle are both created for developing graphics. Pygame provides more advanced and low-level access to graphics, whereas Turtle is a lightweight beginner-friendly graphic module used to introduce programming in a simple way.

If you want to draw some basic animation or graphic designs, turtle is awesome for that purpose, and if you want to create games or heavy GUI software, pygame is the best choice.

So if we talk about capabilities, pygame is better than Turtle, and if we talk about simplicity, Turtle is better. Using turtle has another benefit that you don’t need to install any libraries to use it, whereas you need to install pygame to use it.

Conclusion

Turtle is an awesome library for creating simple graphic designs. It has very simple functions and doesn’t need any separate installations. It shows how simple programming can be if we do it the right way. It is a great option for artistic programmers to display their skills. Even if you’re not quite an artist, give it a try and write a few lines of code to draw a simple shape or anything you want. Practice is an important part of programming.

Источник

Читайте также:  Java for collection example
Оцените статью