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()
Related
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()
I have been trying to make a size difference but am unable to. Do I add or edit a line? Help is very much appriciated. Here is my code.
import turtle
#first turtle
my_turtle1 = turtle.Turtle()
my_turtle1.shape("square")
my_turtle1.screen.bgcolor("white")
my_turtle1.color('black')
my_turtle1.setheading(90)
#second turtle
my_turtle2 = turtle.Turtle()
my_turtle2.setposition(50,0)
my_turtle2.shape("square")
my_turtle2.color('black')
my_turtle2.setheading(90)
#3rd turtle
my_turtle3 = turtle.Turtle()
my_turtle3.setposition(100,0)
my_turtle3.shape("square")
my_turtle3.color('black')
my_turtle3.setheading(90)
#4th turtle
my_turtle4 = turtle.Turtle()
my_turtle4.setposition(150,0)
my_turtle4.shape("square")
my_turtle4.color('black')
my_turtle4.setheading(90)
turtle.done()
You can do this:
import turtle
#first turtle
my_turtle1 = turtle.Turtle()
my_turtle1.turtlesize(90, 90, 1) # pass, stretch width, stretch_len, outline, to this function like in the docs
turtle.done()
Link to docs: https://docs.python.org/3.3/library/turtle.html?highlight=turtle#turtle.turtlesize
Not sure what you are trying to say by making turtle bigger. Is it the size of the turtle or the shape.
For making the turtle bigger-use turtlesize()
For the shape,use shapesize()
I hope you find it helpful : )
This is my first question on StackOverflow, so bear with me. I am trying to have my turtle's shape be a small circle. However, the default circle is too big for my project. Is there a way to make it smaller? My company won't let me load a gif file to be the shape of a turtle.
What I've tried so far is with a gif file where I do:
import turtle
screen = turtle.Screen()
screen.register_shape('circle1', 'circle.gif')
t = turtle.Turtle()
t.shape('circle1')
t.forward(10)
This works but uses a gif file, which my company doesn't allow. Is there a way I can do this without a gif?
Working with the set of cursors that Python provides, including 'circle', you can adjust the size of the cursor using the shapesize() method (aka turtlesize):
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
turtle.shape('circle')
turtle.shapesize(0.5) # make the cursor half the default size
turtle.forward(100)
screen.exitonclick()
Above, we resized it relative to it's default size. If we want to size it to a specific pixel size, we start with the knowledge that the provided cursors are based on a 20px by 20px square, and adjust accordingly:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
screen = Screen()
turtle = Turtle()
turtle.shape('circle')
turtle.shapesize(15 / CURSOR_SIZE) # make the cursor 15 pixels diameter
turtle.color('black', 'white') # make it an open circle this time
turtle.forward(100)
screen.exitonclick()
What you can do is input the polynomial corresponding to the size of the circle you want.
register_shape takes in more than just a file name, it can also take in coordinates.
i.e. screen.register_shape(name, coordinates) is another way of using that function.
If you want to make the circle smaller, draw the circle with the corresponding size and append it to a list. Then convert that list into a tuple and that can be your new shape.
In general you can draw any shape, append the coordinates and use that.
Here is an example for a circle:
Specific Example
import turtle
def drawCircle(radius,iterations, t): # returns tuples
coords = []
for i in range(iterations):
t.forward(radius)
t.right(360.0/iterations)
coords.append((t.xcor(), t.ycor()))
return tuple(coords)
t = turtle.Turtle()
screen = turtle.Screen()
screen.register_shape('myCircle', drawCircle(1, 72, t))
t.shape('myCircle')
General Example
import turtle
def drawShape(t):
coords = []
while # turtle move
coords.append((t.xcor(), t.ycor()))
return tuple(coords)
t = turtle.Turtle()
screen = turtle.Screen()
screen.register_shape('myShape', drawShape(t))
t.shape('myShape')
Note that when you do have a custom shape, then turtle will automatically fill it in for you. So this won't be a circle, but a filled-in circle instead.
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.
I am following a tutorial for Python learning and I can't get the screen to open to draw. I don't get an error, it just shows that the program finish running.
Maybe I missed something, can someone point it out?
import turtle #acutally called turtle to draw a turtle beautiful also used
to draw other stuff
# to draw a square or eventually a turtle you need to do this things below
# to draw a square you want to : move forward,turn right,move forward,turn
right,move forward turn right
def draw_square(): #draw square for turtles
window = turtle.Screen() #this is the background where the turtle will
move
window.bgcolor("red") # the color of the screen
brad = turtle.Turtle() #this is how to start drawing like time.sleep you use turtle.Turtle
brad.forward(100)#move turtle forward takes in a number which is the distance to move forward
brad.forward(90)# moves right
brad.forward(100)
brad.forward(90)
brad.forward(100)
brad.forward(90)
brad.forward(100)
brad.forward(90)
window.exitonclick() #click the screen to close it
draw_square()
Your primary error is these two lines are in the wrong order:
window.exitonclick() #click the screen to close it
draw_square()
The exitonclick(), or mainloop(), or done() should be the last thing your turtle code does as they turn control over to Tk's event loop. A rework of your code for the above and style issues:
import turtle
# to draw a square, or eventually a turtle, you need to do the things below
def draw_square():
""" draw square for turtles """
# to draw a square you want to : move forward, turn right,
# move forward, turn right,move forward turn right
brad = turtle.Turtle()
brad.forward(100) # forward takes a number which is the distance to move
brad.right(90) # turn right
brad.forward(100)
brad.right(90)
brad.forward(100)
brad.right(90)
brad.forward(100)
brad.right(90)
window = turtle.Screen()
# this is the background where the turtle will move
window.bgcolor("red") # the color of the window
draw_square()
window.exitonclick() # click the screen to close it
To make the Turtle screen stay after the code is executed, you have to either use the turtle.done() or window.exitonclick().
I have created a sample Python program for that, just have a look:
import turtle
me = turtle.Turtle() # creating the turtle object
def drawSquare(size):
for i in range(4):
me.fd(size)
me.rt(90)
me.pencolor('white') # making the pencolor white, so that it is visible in black screen
window = turtle.Screen()
window.bgcolor('black') # creating black background
me.penup()
# repositioning the turtle
me.bk(10)
me.rt(90)
me.bk(50)
# putting the pen down to start working
me.pendown()
# calling the draw square method with the edge length for the square
drawSquare(100)
# window.exitonclick() # exits the screen when clicked
turtle.done() # you have to cross the window to close it
import turtle
t = turtle.Turtle()
t.begin_fill()
for i in range(4):
t.fd(100)
t.lt(90)
t.end_fill()
t.done()
This is the easiest way to draw a square. You begin by importing the turtle, and letting it begin fill. You then go into a for loop which makes the code repeat itself for as many times as you would like (in this case 4). fd is an abbreviation for forward, and the number entered is in pixels. Again, lt is an abbreviation that means left turn, and the number in parentheses is the degrees to be turned. The end_fill command finalizes the fill-in, and t.done keeps the window open until the user closes it.
import turtle
def draw_square():
window = turtle.Screen()
window.bgcolor("green")
bob = turtle.Turtle()
bob.forward(100)
bob.right(90)
bob.forward(100)
bob.right(90)
bob.forward(100)
bob.right(90)
bob.forward(100)
bob.right(90)
window.exitonclick()
draw_square()