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.
Related
I thought trying to draw this thing in kenyan colours. Thought it would be simple and quick but ran into errors on the second line.
import turtle
pen=turtle.Turtle()
pen.speed('fastest')
When I take out the second line the error appears on the third line.
What am I doing wrong?
turtle.bgcolor('black')
col=('black','white','red','white','green')
That's what I'm trying to get to.
I've never used turtle myself, but after looking online I found this section of code which compiles:
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
From that I think your issue is how you are importing. Try importing turtle the same way as above, and then do:
color('black','white','red','white','green')
instead of
col=('black','white','red','white','green')
---Update-to-address-comments---
Looking at your code, let me know if I misinterpreted where white space should be.
from turtle import *
pen=turtle.Turtle()
pen.speed('fastest')
turtle.bgcolor('black')
color=('black','white','red','white','green')
for i in range(1,200,2):
t.pencolor(col[i%4])
for x in range(0,10):
t.circle(i)
t.rt(50)
turtle.done()
Your issue isn't the import it's the use of t. instead of pen. New code should look like this:
import turtle
pen=turtle.Turtle()
pen.speed('fastest')
turtle.bgcolor('black')
color=('black','white','red','white','green')
for i in range(1,200,1):
pen.pencolor(color[i%4])
for x in range(0,10):
pen.circle(i)
pen.rt(50)
turtle.done()
The colors don't seem to work though. I'll look into it a bit more in a few minutes.
note the for loop should iterate by 1 not 2 for the colors to switch properly.
This code sets up a background of stamps placed by one turtle. Another turtle, (whose shape is from an imported image file) moves around over the background. But the second turtle is not visible whenever it is positioned over a stamp placed by the first turtle. If I make the moving turtle one of the standard shapes, eg circle, then it stays visible. So there's something odd about using an imported image for the turtle, which causes it to disappear whenever it's on top of one of the stamps.
#!/usr/bin/python3
from turtle import *
from time import sleep
scr = Screen()
scr.register_shape('player.gif')
mover = Turtle()
bgnd = Turtle()
bgnd.color('blue')
mover.shape('player.gif')
bgnd.shape('square')
for i in range(5):
bgnd.goto(i*20,0)
bgnd.stamp()
for i in range(5):
mover.goto((8-i)*20,0)
sleep(1)
The accompanying image is the one I've referred to as player.gif (I can see it at the bottom of the post, but it's very small).
Can anyone help explain why this is and how to get around it?
I'm not sure if this is a bug or subtlety, but stamps aren't pixels on the screen like dot() -- the stamp() function returns an ID that allows selectively removing them. My guess is it's a layering issue with the underlying tkinter. In turtle, if you have a layering issue, sometimes it helps to do things in a different order:
from turtle import Screen, Turtle
from time import sleep
background = Turtle()
background.hideturtle()
background.color('blue')
background.shape('square')
for i in range(5):
background.goto(i * 20, 0)
background.stamp()
screen = Screen()
screen.register_shape('player.gif')
mover = Turtle()
mover.shape('player.gif')
for i in range(5):
mover.goto((8 - i) * 20, 0)
sleep(1)
screen.exitonclick()
In the long run, time.sleep() isn't a friend of event-based turtle. It's fine for examples like this but anything more and it will cause more problems than it solves. Look into the ontimer() method of the screen.
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):
just reviewing for my upcoming midterm. We were given Past midterm problems but no solutions. I am trying to grasp the knowledge best i can.
For this problem, it asks to define a function named equalSigns, pass it values t and length. So, i just need to make my program in turtle graphics, create two parellel line, simple enough i suppose. this is my code that i wrote just for it to correctly output an equal sign of x length. (then of course i would convert it to a function) My question, is there any better way to create this?
import turtle
t=turtle.Turtle()
s=turtle.Screen()
t.forward(200)
t.penup()
t.home()
t.right(90)
t.forward(50)
t.pendown()
t.left(90)
t.forward(200)
'''i suppose i dont have to go home and then down.
instead just continue and go down and forward left.
but either way, is this the best approach to take?
'''
Yes, I think there's a better way. Most of all, I think you turned the wrong way: you need to make a second right turn to come back along the lower line.
You could make a routine that does a half-equals, and then all it twice to get the two lines. Think of this as drawing a rectangle, except that the short sides are invisible.
# Draw long side
t.pendown()
t.forward(x)
t.penup()
t.right(90)
# Move along short side without drawing
t.forward(x/4)
t.right(90)
That gets you to the opposite corner of the rectangle. Call this twice, and you're done ... and back at the starting point.
Perhaps you could have your turtle think outside the shell:
import turtle
import tkinter as _
_.ROUND = _.BUTT
turtle.width(50)
turtle.forward(200)
turtle.color("white")
turtle.width(48)
turtle.backward(200)
turtle.done()
(The vertical gray bars at the two ends are artifacts of GIF conversion and not present when the program is run.)
I am a newbie using python 3.2.3
When I ran the module in the python IDLE, the turtle drew a square without any update on the screen, so the window appeared blank, and after I input any key, the turtle and the square appeared due to the call of turtle.update().
However, when I double-clicked the .py file storing the below code in my document and executed it directly, the square always showed up before I input any key.
The interesting part is that the turtle was not shown but only the square was shown.
It seems that there was a turtle update only for the square after drawing even if I had already set turtle tracer to (0,0).
Is this considered a bug and how can I solve it? Thanks for the help.
import turtle
def drawSquare():
turtle.down();
turtle.begin_fill();
turtle.goto(10, 0);
turtle.goto(10, 10);
turtle.goto(0, 10);
turtle.goto(0, 0);
turtle.end_fill();
turtle.up();
def tUpdate():
turtle.update();
turtle.tracer(0,0);
drawSquare();
input("Not updated. Press any key.");
tUpdate();
print("Updated");
turtle.mainloop();
You've got a couple of things working against you: the poor documenation provided for tracer() and the fact that end_fill() and up() cause updates to occur. tracer() is not really meant to hide things from the user until you're ready to display them -- it's a speed optimization so that the user doesn't have to see every drawing step in a complicated image. You don't have full control over when updates will occur.
Here's a rework of your example that displays the behaviour you want at the cost of it no longer being a filled square. I've swapped your input() trigger for a mouse click on the window instead but your approach will work just as well here too:
from turtle import Turtle, Screen
def drawSquare(turtle):
turtle.goto(100, 0)
turtle.goto(100, 100)
turtle.goto(0, 100)
turtle.goto(0, 0)
screen = Screen()
screen.tracer(0, 0)
screen.onclick(lambda x, y: screen.update())
turtle = Turtle()
drawSquare(turtle)
screen.mainloop()
I also made which are turtle methods, and which are screen methods, more explicit. The beginning programmer friendly design of the turtle library tends to blur these in the interest of ease of use.