How to Set the Drawing Point in Turtle Graphics Python 3 - python

I'm using Turtle Graphics Module in Python 3 and I have drawn a Circle using circle method of Turtle with that i'm using Custom Shape which is a pencil, now problem is that i'ts drawing from the center of the pencil. I want it to draw from the tip of the Pencil, so how can i setup turtle to start drawing from the tip of the pencil rather than from the center point.
Here is my Code:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(500, 500)
screen.screensize(500, 500)
screen.register_shape('dpen.gif')
turtle = Turtle('dpen.gif')
def draw_circle():
turtle.home()
turtle.clear()
turtle.circle(90)
screen.listen()
screen.onkeypress(draw_circle, 'space')
screen.mainloop()
Thank You

When setting a custom pen, turtle will always draw from the centre of your image. There might be a way to change this, but a far simpler solution is to change your image so that the pencil is in the top right corner, so that the tip points to the middle of the image. Took me 10 seconds in powerpoint
Very crude example image (demonstrates point):

Related

How do I use the python turtle on click function to clear the canvas?

This is for a school project - I am still a beginner and have lots of trouble with functions. How would I clear the canvas with a click?
The answer depends on how severe a clearing you want to do. If you simply want to remove the drawing done by an individual turtle, but leave drawings by other turtles (background) in tact, as well as preserve aspects of the screen like its background color, you can use the screen's onclick() event to invoke an individual turtle's clear() method:
from turtle import Screen, Turtle
def clear_turtle(x, y):
turtle.clear()
screen = Screen()
screen.bgcolor('lavender')
screen.onclick(clear_turtle)
yertle = Turtle()
yertle.hideturtle()
yertle.dot(100)
turtle = Turtle()
turtle.hideturtle()
turtle.dot(50, 'yellow')
screen.mainloop()
If, on the other hand, you want a more scorched earth result, you can use the screen's onclick() event to invoke the screen's clear() method:
from turtle import Screen, Turtle
def clear_screen(x, y):
screen.clear()
screen = Screen()
screen.bgcolor('lavender')
screen.onclick(clear_screen)
yertle = Turtle()
yertle.hideturtle()
yertle.dot(100)
turtle = Turtle()
turtle.hideturtle()
turtle.dot(50, 'yellow')
screen.mainloop()
This will return the screen to it's initial blank (white) state and destroy all your turtle drawings as well as your turtles all the way down.

Python Turtle centre turtle on screen

I want to move the turtle in random directions while keeping the turtle at the center of the screen. How can I do this?
While True:
turtle.setheading(random.randint(0,360))
turtle.forward(10)
I want the camera screen to follow the turtle whereever its going.
You can do this by manipulating the scroll position using methods of the underlying tkinter window structure. Here's a turtle example that moves a ball but keeps it centered in the window.
What do you mean by "center of the screen"? Your code is turning your turtle by 0 to 360 and then move 10 forward. If you dont want to move the turtle remove the turtle.forward(10).
To move your turtle back to the start you could add turtle.home().
While True:
turtle.setheading(random.randint(0,360))
turtle.forward(10)
turtle.home()

Some steps of the turtle graph disappear while using turtle.tracer(False)

If I do not use turtle.tracer(False), the full image can be shown with the turtle animation. If I use turtle.tracer(False), the image will be incomplete. How can I solve this problem?
import turtle
turtle.setup(800, 600)
turtle.tracer(False) # if i use this function the image is shown incompletely
turtle.speed(0)
turtle.width(2)
turtle.up()
turtle.goto(100, -150)
turtle.down()
"""
drawing
"""
turtle.hideturtle()
turtle.done()
There's not enough code shown for a definitive answer but here's a quick fix to try. Make these final lines:
turtle.hideturtle()
turtle.done()
instead be:
turtle.hideturtle()
turtle.tracer(True)
turtle.done()
to allow any pending drawing to complete.
You need to use the turtle.update() method:
Perform a TurtleScreen update. To be used when tracer is turned off.
What tracer(False) does is it turns of updating the screen. This is useful when you want to draw something very big, that takes a lot of time. So what you do is:
Tracer(False)
"""
Drawing
"""
update()

Run a turtle program in full screen when initiated

I wrote a Python 3.7 program with Turtle Graphics. It is a simple program and works fine, but I want it to run full-screen when I initiate the program. How can I do this? There is no full-screen option in the Turtle documentation.
import turtle
from turtle import *
a = turtle.Turtle()
a.speed(10)
a.color('red', 'blue')
# a.begin_fill()
for i in range(90):
a.fd(200)
a.lt(169)
# a.end_fill()
turtle.done()
The width and height arguments to the setup() method take integer (pixel) and floating point (percentage of screen). By supplying 1.0 you'll get the largest window that turtle can make:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=1.0, height=1.0)
a = Turtle()
a.speed('fastest')
a.color('red', 'blue')
# a.begin_fill()
for _ in range(36):
a.forward(200)
a.left(170)
# a.end_fill()
screen.mainloop()
But this isn't necessarily the operating systems sense of Full Screen where the window overlays everything. It's just the largest window turtle can create given available screen real estate.

Draw faster circles with Python turtle

I have an exercise wherein I have to draw a lot of circles with Python turtle. I have set speed(0) and I am using:
from turtle import*
speed(0)
i=0
while i < 360:
forward(1)
left(1)
i+=1
to draw circles. It takes so long. Is there any faster way?
Have you tried turtle.delay() or turtle.tracer() ? See documentation here and here. These set options for screen refreshing which is responsible for most of the delays.
Turtle has a function for drawing circles and the speed of that is much faster than going in a circle one step at a time.
import turtle
tina=turtle.Turtle()
tina.circle(70)
It will look like this
If you want to draw your circle even faster, you can try adding a delay block as well.
import turtle
tina=turtle.Turtle()
tina.delay(1)
tina.speed(0)
You could draw fewer segments, so rather than 360 you go for 120:
while i < 360:
forward(3)
left(3)
i+=3
That will make your circle less smooth, but three times faster to draw.
The circle() method might not be faster, but may be easier to manage:
turtle.circle()
Use Multithread to draw two semi circles simultaneously.
Initially the turtle will be at (0,0) so just clone the turtle and make them both face in opposite direction by 180° then draw semicircles. The code is below:
from threading import Thread
import turtle
t = turtle.Turtle()
t.speed(0)
def semi1(r):
r.circle(50,180)
def semi2(t):
t.circle(50,180)
r = t.clone()
r.rt(180)
a = Thread(target=semi1).start()
b = Thread(target=semi2).start()
This may draw the circle fast.

Categories

Resources