Skip to content Skip to sidebar Skip to footer

Draw a Circle With the Turtle Being the Center

Cartoon Circles with Python Turtle Graphics

Python Turtle Graphics Circles - Archery Target

In this lesson we are going to acquire how to describe circles with Python Turtle Graphics. We will then modify the default circle method so that we can centre our circles at specific (x, y) coordinates, and then have some fun some with creating an archery target and adding some interactivity.

As you may already know, Python Turtle Graphics is fantastic style to learn virtually programming and also about Maths. In full general in that location seems to be petty integration betwixt these two subjects at school level, which is something I hope to see modify. Many of my posts on this blog are written to aid further this cause. Run into Computer Maths Category for related posts.

Before we showtime, please note that there are many means to achieve the same goal with Python Turtle Graphics. While this can exist a adept matter, it can also lead to confusion. For this reason I have done things is a certain way which I consider gives the best foundation for making full use of the potential of this module. For example:

  • I create a screen object so I can control its colour and championship etc.
  • I use functions which have an existing turtle as an statement, to help discourage the use of global variables and provide added flexibility, so the aforementioned office can piece of work for multiple turtles.

Don't worry too much about these details if they don't brand total sense to you. The lawmaking is fairly self explanatory and there are comments to assist.

Drawing Circles with Python

The default mode to create circles in with Python Turtle Graphics is to simple use the circle method, every bit in the post-obit example.

            import turtle  # Set upwardly screen screen = turtle.Screen() screen.championship("Circumvolve") screen.setup(450, 450) screen.bgcolor("cyan")  # Create a turtle toby = turtle.Turtle() toby.speed(0) toby.width(5) toby.hideturtle() toby.color("red")  # Draw a circle starting at (x, y) radius = 100 toby.circle(radius)  # Arrive all work properly turtle.washed()                      

Python turtle circle

This is fine for many purposes, merely it can be frustrating as it doesn't give you control of where the centre of the circle is. Observe how in the example above the circle is non centred at the location of the turtle (chosen toby), which came into being at the default location of (0, 0). Using the default method, the circle is fatigued from the staring indicate, and then the starting bespeak is on the circumference.

Drawing Circles Centred at (x, y)

The next program demonstrates how to draw circles centred at specific (x, y) coordinates. It does this by utilize of a role draw_circle() which takes several arguments as described in the code.

Using this function, it is relatively like shooting fish in a barrel to draw an archery target. Meet the plan below.

            import turtle   def draw_circle(tur, x, y, radius, colour="blackness"):     """     Draws a circumvolve with middle at (x, y), radius radius and color color.     Create your turtle elsewhere and pass it in as tur.     """     tur.color(color)     tur.pu()     tur.goto(x, y - radius)  # -radius because the default circle method starts drawing at the edge.     tur.pd()     tur.begin_fill()     tur.circumvolve(radius)     tur.end_fill()   # Set up screen screen = turtle.Screen() screen.championship("Archery") screen.setup(450, 450) screen.bgcolor("cyan")  # Draw the target toby = turtle.Turtle() toby.speed(0) toby.width(5) toby.hideturtle()  draw_circle(toby, 0, 0, 160, "blackness")  # Draw a black circle at coords (0, 0) with radius 160 pixels draw_circle(toby, 0, 0, 120, "bluish") draw_circle(toby, 0, 0, 80, "red") draw_circle(toby, 0, 0, forty, "yellow")  # Go far all work properly turtle.done()                      

This plan makes use of lots of features which you tin can use in your ain programs. You tin can use as much or as picayune as you like, only experiment with the ideas. If yous don't accept many ideas, endeavor just making small changes, similar irresolute the colours or sizes of the circles. Some of the colours available in Python Turtle Graphics tin can be establish here.

The Next Level

This section contains some more advanced Python programming techniques, so if you are a relative beginner, you may want to go out it for subsequently on.

For example it includes

  • Effect detection with Python Turtle Graphics
  • Event callback functions
  • Passing additional arguments to a callback using lambda
            import turtle  CROSS_SIZE = 20   def draw_circle(tur, x, y, radius, colour="blackness"):     """     Draws a circle with eye at (x, y), radius radius and color color.     Create your turtle elsewhere and pass it in as tur.     """     tur.color(color)     tur.pu()     tur.begin_fill()     tur.goto(x, y - radius)  # -radius because the default circle method starts drawing at the border.     tur.pd()     tur.circle(radius)     tur.end_fill()   def draw_plus(tur, x, y, length=CROSS_SIZE):     """     Draws a cross centered at (x, y) with existing turtle tur and length given past CROSS_SIZE.     """     tur.penup()     tur.goto(x, y - (length / 2))     tur.pendown()     tur.goto(x, y + (length / 2))     tur.penup()     tur.goto(ten - (length / 2), y)     tur.pendown()     tur.goto(x + (length / 2), y)     impress("Mouse click at", ten, ",", y)  # for useful feedback nearly where you clicked.   screen = turtle.Screen() screen.championship("Archery") screen.setup(450, 450) screen.bgcolor("cyan") screen.listen()  # Describe cross when screen is clicked cross_turtle = turtle.Turtle(visible=False) cross_turtle.color("greenish") cross_turtle.width(iv) cross_turtle.speed(0) # The lambda here is a useful trick to enable additional arguments to exist passed to the onclick callback. screen.onclick(lambda ten, y, tur=cross_turtle: draw_plus(tur, x, y)) screen.onkey(lambda: cross_turtle.clear(), "space")  # Articulate crosses on keypress.  # Describe the target toby = turtle.Turtle() toby.speed(0) toby.width(5) toby.hideturtle()  draw_circle(toby, 0, 0, 160, "blackness")  # Describe a black circumvolve at coords (0, 0) with radius 160 pixels draw_circle(toby, 0, 0, 120, "blue") draw_circle(toby, 0, 0, fourscore, "cherry-red") draw_circle(toby, 0, 0, twoscore, "yellowish")  # Arrive all work properly. turtle.done()                      

Python turtle archery

In that location are lots of ingredients here that you can employ in your ain projects. As earlier, get ahead and edit $.25 of the program or use bits in you lot ain project. The program is not currently a game every bit such, just I expect it could exist made into i. Can you think of how? I'chiliad thinking some kind of random positioning of the crosses or reflex-testing game. We haven't looked at timers and animation in this article, but for sure they are possible with Python Turtle Graphics. It may be that an thought you have might become possible with a bit more cognition, and so perhaps make a annotation and come back to it later. If y'all have an thought that you want assistance with, let me know in the comments and I'll encounter if I can help.


This lesson has shown you how to draw circles using Python Turtle Graphics and so how to improve the basic functionality and add some interactive features. I hope you found it fun and interesting.

Happy calculating!

carrwhispooke.blogspot.com

Source: https://compucademy.net/drawing-circles-with-python-turtle-graphics/

Kommentar veröffentlichen for "Draw a Circle With the Turtle Being the Center"