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()
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.
How to make the cursor go to a random place on the screen with randint() using Python turtle graphics:
stars = Turtle()
drawings = [Trees, Moon, BlueSky, NightSky, Sun, Sunset, stars]
def penup():
for x in drawings:
x.penup()
penup()
def Stars():
for x in range(5):
penup()
stars.goto((randint(-100,0), randint(0,100)))
pendown()
stars.left(36)
stars.forward(10)
stars.left(36)
penup()
Stars()
I'm trying to make a program that draws stars at the top of the screen, with random spots to where it goes in the night sky. Could someone help with this randint() function? It's not going to a random position and it's acting weird.
Your code is a bit of a mess. Having both stars and Stars is risky coding. You use your penup() function as if it's turtle's penup() function, which it isn't. Your code doesn't even draw one star, let alone many. And the star you're attempting to draw looks more like a pentagon than a star.
Here's a rework of your code that does what you describe:
from turtle import Turtle, Screen
from random import randint
def stars():
for _ in range(20):
star.penup()
star.goto(randint(-200, 200), randint(100, 200))
star.pendown()
for _ in range(5):
star.left(144)
star.forward(10)
screen = Screen()
star = Turtle()
star.speed('fastest') # because I have no patience
stars()
star.hideturtle()
screen.exitonclick()
python how can i make the shapes?
This is what I did
import turtle,random,sys
turtle.title("SHOT")
turtle.setup (width=800, height=600)
turtle.bgcolor("light green")
turtle.screensize(10, 400)
class boxes:
box_A = turtle.getturtle()
box_B = turtle.getturtle()
box_A.hideturtle()
box_B.hideturtle()
box_A.setposition(-300,0)
box_A.setposition(300,0)
box_A.showturtle()
box_B.showturtle()
And my computer draw line... I don't want line...
so I want to erase the line and
I want to make a rectangle boxes(two)
I tried to make rectangle with
box_A.shape("rectangle")
Definitely, it wasn't working;;;
guys plz help me
You can create a custom shape by using coordinates. First, do the normal statements.
import turtle
t = turtle.Turtle()
s = turtle.Screen() # The Screen is VERY important for this project!
Ok! Now to change t's shape, we have to enter a set of coordinates. Here are a few presets you can use I made:
Pointer: ((15,0),(0,25),(-15,0),(0,3))
Noel Star: ((0,20),(-5,5),(-20,0),(-5,-5),(0,-20),(5,-5),(20,0),(5,5))
X: ((15,15),(-15,-15),(0,0),(15,-15),(-15,15),(0,0))
Cross: ((-7.5,-15),(7.5,-15),(7.5,-7.5),(15,-7.5),(15,7.5),(7.5,7.5),(7.5,15),(-7.5,15),(-7.5,7.5),(-15,7.5),(-15,-7.5),(-7.5,-7.5))
Right Triangle: ((20,0),(0,-20),(0,0))
Arrow: ((-15,7.5),(-10,0),(-15,-7.5),(5,-7.5),(15,0),(5,7.5))
Parallelogram: ((-30,20),(-40,-20),(30,-20),(40,20))
Rhombus: ((0,-20),(-10,0),(0,20),(10,0))
Trapezoid: ((-20,20),(20,20),(30,0),(-30,0))
Pentagon: ((-5,10),(-10,0),(-5,-10),(5,-10),(10,0),(5,10))
Hexagon: ((-10,20),(-20,0),(-10,-20),(10,-20),(20,0),(10,20))
Octagon: ((-10,20),(10,20),(20,10),(20,-10),(10,-20),(-10,-20),(-20,-10),(-20,10))
The rectangle's coordinates are: ((-20,10),(20,10),(20,-10),(-20,-10)). To register this as a custom shape, use the register_shape() function. The first argument is what you will name the shape; 'rectangle', in this case. The second argument is the coordinates, so save them as a variable.
rectCors = ((-20,10),(20,10),(20,-10),(-20,-10));
s.register_shape('rectangle',rectCors);
Ok. Now just tell the turtle that its shape is a rectangle.
t.shape('rectangle');
Done! In total, thats:
import turtle
t = turtle.Turtle();
s = turtle.Screen();
rectCors = ((-20,10),(20,10),(20,-10),(-20,-10));
s.register_shape('rectangle',rectCors);
t.shape('rectangle');
Note:
There is no way to change the shape from horizontal to vertical, if you were wondering, except t.setheading(90) or t.setheading(-90).
just do
object.shape("square")
object.shapesize(X, Y)
this will make a rectangle based on it's pixels
or do this
object.shape("square")
object.shapesize(stretch_wid=5, stretch_len=1)
this will produce a rectangle based on a ratio of 5:1, each unit is a square
Draw four lines:
box_A.setposition(-300,0)
box_A.setposition(-300,200)
box_A.setposition(0,200)
box_A.setposition(0,0)
I think this is what you're trying too do
import turtle
wn = turtle.Screen()
wn.tracer()
bird = turtle.Turtle()
bird.shape("square")
You have to do square, rect isn't a shape in turtle
bird.shapesize(stretch_wid=5, stretch_len=10)
Then just stretch it too a rectangle by making the width longer than height
bird.color("green")
bird.setpos(0, 0)
bird.penup()
Penup is for if you're moving it, it won't draw a line behind it
bird.speed(0)
The speed just makes the animation instant if you do that
wn.exitonclick()
Hope this helped