Can you make the turtle the center of a circle in python? - python

I am making a tic tac toe game and when the user presses 'o' a circle is printed but the circle is always on the left of the turtle. i would like the turtle to be in the center of a box and draw the circle around itself.

You have to move turtle on your own - using left,right,forward, penup, pendowm.
Example
import turtle
radius = 100
# move
turtle.penup()
turtle.right(90)
turtle.forward(radius)
turtle.left(90)
turtle.pendown()
# circle
turtle.circle(radius)
# move back
turtle.penup()
turtle.right(-90)
turtle.forward(radius)
turtle.left(-90)
turtle.pendown()
Result:

There are a couple of ways to draw a circle centered around a Python turtle without moving the turtle. The first is the dot() method. It takes a diameter, rather than a radius, and optionally allows you to specify the color at the same time:
import turtle
RADIUS = 100
turtle.dot(RADIUS * 2)
turtle.dot(RADIUS * 1.6, turtle.bgcolor()) # "unfill" the circle
turtle.done()
Another way to do it is via stamping, that is, make the turtle cursor itself a circle, size it, and then call stamp():
import turtle
RADIUS = 100
CURSOR_RADIUS = 10
turtle.hideturtle()
turtle.shape('circle')
turtle.fillcolor(turtle.bgcolor())
turtle.shapesize(RADIUS / CURSOR_RADIUS, outline=RADIUS/5)
turtle.stamp()
turtle.done()
Both of the above have the side effect of overwritting anything that the circle surrounds, which doesn't seem like a problem for tic-tac-toe. To avoid this, you can, of course, temporarily shift the turtle's position, as #furas suggests:
import turtle
RADIUS = 100
turtle.width(RADIUS/5)
turtle.penup()
turtle.sety(turtle.ycor() - RADIUS)
turtle.pendown()
turtle.circle(RADIUS)
turtle.penup()
turtle.sety(turtle.ycor() + RADIUS)
turtle.pendown()
turtle.done()

Related

Center the flower using turtle

I want to draw a flower with turtle. Although I am facing problem in centering the flower (0,0) should be flower's center or where turtle initially is spawned. How can I center it?
import turtle
import math
turtle.speed(-1)
def Flower():
global radius, num_of
for i in range(num_of):
turtle.setheading(i * 360/num_of)
turtle.circle(radius*3.5/num_of,180)
radius = 50
num_of = 10
Flower()
I tried setting turtle to where it starts drawing but number of sides ruin it.
Since the turtle draws from a edge, we need to move the turtle to compensate for the radius of the entire image. To simplify this move, we align the starting point of the image with one (X) axis. We also switch from absolute coordinates (setheading()) to relative coordinates (right()) so our initial rotational offset doesn't get lost or need to be added to every position:
import turtle
import math
radius = 50
num_of = 13
def flower():
outer_radius = radius * 3.5 / math.pi
turtle.penup()
turtle.setx(-outer_radius) # assumes heading of 0
turtle.pendown()
turtle.right(180 / num_of)
for _ in range(num_of):
turtle.right(180 - 360 / num_of)
turtle.circle(radius * 3.5 / num_of, 180)
turtle.speed('fastest')
turtle.dot() # mark turtle starting location
flower()
turtle.hideturtle()
turtle.done()
To get the radius of the flower, we add up the diameters of all the petals and use the standard circumference to radius formula. There are probably simplifications, math-wise and code-wise, we could make.

How to make the handle after animating in python turtle?

This is the bycicle i am trying to make but i cant.
I am really sorry if it gets downgrades and i am really new to python turtle.
I am currently trying to animate. I am done with the animation but i am struglling with making the handle of the bycicle.
After writing the code it dosent make any change.
So if possible. Anyone can make the handle and explain how he did it?
Thank you..
import time
def moving_wheel(turtle):
turtle.fillcolor('orange')
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
def moving_handle(turtle):
pos = turtle.pos()
turtle.left(90)
turtle.forward(70)
turtle.left(120)
turtle.forward(110)
turtle.penup()
turtle.goto(pos)
turtle.right(210)
turtle.pendown()
if __name__ == "__main__":
screen = turtle.Screen()
screen.setup(600, 600)
screen.bgcolor('green')
screen.tracer(0)
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t3 = turtle.Turtle()
# set a turtle object color
t1.color('red')
t2.color('red')
t3.color('red')
# set turtle object speed
t1.speed(0)
t2.speed(0)
t3.speed(0)
t1.width(2)
t2.width(2)
t3.width(1.5)
t1.hideturtle()
t2.hideturtle()
t3.hideturtle()
# turtle object in air
t1.penup()
t2.penup()
t3.penup()
# set initial position
t1.goto(-250, 0)
t2.goto(-150,0)
t3.goto(-150,20)
t3.pendown()
# move turtle object to surface
t1.pendown()
t2.pendown()
# infinite loop
while True:
# clear turtle work
t1.clear()
t2.clear()
t3.clear()
# call function to draw ball
moving_wheel(t1)
moving_wheel(t2)
moving_handle(t3)
# update screen
screen.update()
# forward motion by turtle object
t1.forward(0.1)
t2.forward(0.1)
t3.forward(0.1)
There is no error in your code. You have to only sit and add more forward, left/right, penup/pendown, etc. - and this need to test different value and see which gives expected result.
I would split problem into smaller figures with more regular angles and size.
Bottom part looks like 3 triangles and it would be simpler to use regular triangles with angles 60.
Maybe you should even create function triangle(size) for this.
This is what I tried to draw these triangles.
def moving_handle(turtle):
pos = turtle.pos()
# --- first triangle without last side ---
# rotate to start position
turtle.left(120)
for _ in range(2):
turtle.forward(50)
turtle.left(120)
# move without drawing last side
turtle.penup()
turtle.forward(50)
turtle.left(120)
turtle.pendown()
# rotate back to old position
turtle.left(-120)
# --- second triangle
# move to new position without drawing
turtle.penup()
turtle.left(180)
turtle.forward(50)
turtle.left(-180)
turtle.pendown()
# draw trangle
# rotate to start position
turtle.left(120)
for _ in range(3):
turtle.forward(50)
turtle.left(120)
# rotate back to old position
turtle.left(-120)
# --- second triangle
# draw trangle
# rotate to start position
turtle.left(60)
for _ in range(3):
turtle.forward(50)
turtle.left(120)
# rotate back to old position
turtle.left(-60)
# move to the beginning
turtle.penup()
turtle.goto(pos)
turtle.pendown()

How to draw dot inside shape turtle python?

I am new with Turtle python library and I am trying to draw turtle dot inside the shape (square), which should look like on the picture below. The problem is that when I am trying to do this the shape covers the dot and I see only the shape (square).
enter image description here
My code:
def add_dot_square():
obj = Turtle()
obj.penup()
obj.shape("square")
obj.shapesize(1.5, 1.5)
obj.color("orange")
obj.goto(0, 0)
obj.dot(20, "red")
Turtle's can't appear behind things they draw, only other turtles (and even that's tricky.) Instead of the turtle being the square, have the turtle draw or stamp the square, and then place the dot atop it:
from turtle import Screen, Turtle
def add_dot_square(obj):
obj.penup()
obj.shape('square')
obj.shapesize(1.5)
obj.color('orange')
obj.goto(0, 0)
obj.stamp()
obj.dot(20, 'red')
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
add_dot_square(turtle)
screen.exitonclick()
I still ha[v]e a problem when I want to for example move this
square and dot
Let's rearrange the code a bit and add some motion:
from turtle import Screen, Turtle
def add_dot_square(obj):
obj.clear()
obj.stamp()
obj.dot(20, 'red')
screen = Screen()
screen.tracer(False)
turtle = Turtle()
turtle.hideturtle()
turtle.shape('square')
turtle.shapesize(1.5)
turtle.color('orange')
turtle.penup()
for _ in range(360):
turtle.circle(100, extent=1)
add_dot_square(turtle)
screen.update()
screen.exitonclick()

Python turtle end_poly() does not work

Turtle docs say when end_poly() is reached:
Stop recording the vertices of a polygon. Current turtle position is
last vertex of polygon. This will be connected with the first vertex.
With my example, the final line is not drawn from the last vertex back to the first vertex. It acts the same in 2.7 and 3.7 Python.
from turtle import *
print("position 0:",position())
width(5)
pencolor("red")
fillcolor("blue")
begin_fill()
begin_poly()
fd(100)
print("position 1:",position())
left(90)
fd(100)
print("position 2:",position())
end_poly()
end_fill()
p = get_poly()
print("poly p:",p)
register_shape("myShape",p)
shapes_ = getshapes()
print("shapes_:", shapes_)
Output:
position 0: (0.00,0.00)
position 1: (100.00,0.00)
position 2: (100.00,100.00)
poly p: ((0.00,0.00), (100.00,0.00), (100.00,100.00))
shapes_: ['arrow', 'blank', 'circle', 'classic', 'myShape', 'square', 'triangle', 'turtle']
Image of polygon
I believe this is an error in the documentation, but an understandable one.
First, you clearly can draw what you want by closing the figure yourself:
from turtle import Turtle, Screen
screen = Screen()
turtle = Turtle(visible=False)
turtle.width(5)
turtle.color("red", "blue")
position = turtle.position()
turtle.begin_fill()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.goto(position)
turtle.end_fill()
screen.exitonclick()
So what's the deal with polygons? Well, unless you implement the code yourself, there's nothing turtle can do, by default, with polygons, except use them as turtle cursors. In which case it's passing them onto tkinter which is responsible for the closing the polygon:
from turtle import Turtle, Screen
screen = Screen()
turtle = Turtle(visible=False)
turtle.penup()
turtle.begin_poly()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.end_poly()
screen.register_shape("myShape", turtle.get_poly())
turtle.shape("myShape") # when the polygon gets closed
turtle.shapesize(outline=5)
turtle.color("red", "blue")
turtle.stamp()
screen.exitonclick()
(I'm guessing it's in tkinter's canvas.coords() where the polygon gets closed.) Note that your width(5) means nothing in this case, nor does *_fill() as a cursor's outline width is set using shapesize() and a cursor is naturally filled. It doesn't need a color specified at polygon creation time, you can wait until the new cursor is deployed.
I believe this statement in the end_poly() documentation:
last vertex of polygon. This will be connected with the first vertex.
Should really reside in the polygon section of turtle's register_shape() documentation. But you can understand the error as turtle only thinks the role of *_poly() is for creating new cursors. Whereas polygons should be a first class, flexible datatype in turtle.

How do you make a circle from origin

I am trying to draw a target like this:
(source: newscientist.com)
using turtle.
The problem is turtle includes the origin as part of the graph, as opposed to the origin being in the center. My question is, how do I get turtle draw a circle AROUND the origin rather than include it?
import turtle
radius = 100
turtle.speed(0)
for rings in range(10):
turtle.circle(radius)
radius += 10
import turtle
radius = 100
turtle.speed(0)
for rings in range(10):
turtle.penup()
turtle.goto(0, -radius)
turtle.pendown()
turtle.circle(radius)
radius += 10
It's nicer to use radius as the loop variable
import turtle
turtle.speed(0)
for radius in range(100, 200, 10):
turtle.penup()
turtle.goto(0, -radius)
turtle.pendown()
turtle.circle(radius)
Then you might wish to define a function
import turtle
turtle.speed(0)
def origin_circle(turtle, radius):
turtle.penup()
turtle.goto(0, -radius)
turtle.pendown()
turtle.circle(radius)
for radius in range(100, 200, 10):
origin_circle(turtle, radius)

Categories

Resources