How can I make rectangle shape in turtle, python? - python
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
Related
Circle shape in turtle with smaller radius
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.
Drawing this pattern using Python’s turtle module. Some squares on top of each other but tilted sort of like a spiral
I’m new to programming and I’m reading a book called How To Think Like A Computer Scientist. In the fourth chapter, it talks about functions. At the end of the chapter, there’s an exercise that asks me to draw the following pattern using Python’s turtle module. I was examining this picture and decided to split it into two: 1) the lines in the middle and 2) the squares that go on top of each other like a spiral. I drew the first part using this code: import turtle wn = turtle.Screen() # Set up the window wn.bgcolor("lightgreen") alex = turtle.Turtle() # Create Alex alex.color("blue") alex.pensize(3) for i in range(20): # Here I start drawing the lines alex.forward(100) alex.backward(100) alex.left(360/20) # Fit 20 lines in the 360 degree circle wn.mainloop() When I run it, it draws this: Then, I created the draw_square function and managed to draw the first square: import turtle def draw_square(turtle, size): for i in range(4): turtle.forward(size) turtle.left(90) wn = turtle.Screen() # Set up the window wn.bgcolor("lightgreen") alex = turtle.Turtle() # Create Alex alex.color("blue") alex.pensize(3) for i in range(20): # Here I start drawing the lines alex.forward(100) alex.backward(100) alex.left(360/20) # Fit 20 lines in the 360 degree circle # In a messy way, using what I've learned, I move Alex to where he's supposed to be now # I'm pretty sure there's a classier way to do this alex.penup() alex.backward(100) alex.right(90) alex.forward(100) alex.left(90) alex.pendown() # Here I get Alex to draw the square draw_square(alex, 200) wn.mainloop() When I run it, it draws this: Now I’m stuck. I don’t know where to go from here. I don’t know how to go about drawing all the other squares. I can’t figure out where to place the turtle, and how many degrees to tilt the square (probably 20, like the lines, but I don’t know how to implement it)… Anyways, you guys got any tips? Any suggestions? I’m trying not to skip any exercises on the book and this one got me.
Excellent attempt, and thanks for the clear images of expected/actual output! The pattern is actually a bit simpler than you may think. A single box is being drawn from the center repeatedly, with the turtle pivoting slightly on the center point on each iteration. The overlaps of the box sides create the illusion of "spokes". As for determining the turn amount in degrees, I took 360 and divided it by the number of spokes shown in the image (20), giving 18 degrees. Here's code that produces the correct output. import turtle def draw_square(turtle, size): for i in range(4): turtle.forward(size) turtle.left(90) if __name__ == "__main__": wn = turtle.Screen() wn.bgcolor("lightgreen") alex = turtle.Turtle() alex.color("blue") alex.pensize(3) boxes = 20 for _ in range(boxes): draw_square(alex, 200) alex.left(360 / boxes) wn.mainloop() Output:
Give Python turtle a rectangular shape for ping pong game
I am trying to give a rectangle shape to the turtle in Python, but nothing works from turtle appearance methods. It says that turtle does not have this attribute. What can I do?
In the turtle module that comes with Python, you can use shapesize() to resize any of the existing turtle shape options, 'square' in this case. You can stretch or shrink it differently in the two dimensions. Here's an example paddle that sits in the middle of the screen that's taller than it is wide and can only be moved up and down the screen: from turtle import Screen, Turtle def drag(_, y): paddle.ondrag(None) paddle.sety(y) paddle.ondrag(drag) screen = Screen() paddle = Turtle('square') paddle.speed('fastest') paddle.shapesize(1, 4) paddle.setheading(90) paddle.penup() paddle.ondrag(drag) screen.mainloop() You can use two turtles to create these on the left and right of the screen for a ping pong game.
turtle.addshape(name, shape=None) There are three different ways to call this function: name is the name of a gif-file and shape is None: Install the corresponding image shape. screen.register_shape("turtle.gif") Note Image shapes do not rotate when turning the turtle, so they do not display the heading of the turtle! name is an arbitrary string and shape is a tuple of pairs of coordinates: Install the corresponding polygon shape. screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)) you should change sizes to have a turtle with the shape of rectangle! :)
You should probably try using: turtle.shape() In the function just put in "square". Feel free to ask any questions!
I had same doubt and here what I've finally done: I got my turtle object to become a "square", turtle.shape("square") and after I printed out it's shape as coordinates: print(turtle.get_shapepoly()) 2.After it, I was twitching the coordinates until I got the desired effect. In essence, the end result was: from turtle import Turtle, Screen, Shape SHAPE = ((10, -40), (10, 40), (-10, 40), (-10, -40)) class Paddle(Turtle) : def __init__(self, contour_color): super().__init__() self.new_shape(contour_color) self.setheading(90) def new_shape(self, contour_color): screen = Screen() s = Shape("compound") poly1 = SHAPE s.addcomponent(poly1, "grey", outline=contour_color) screen.register_shape("paddle", s) self.shape("paddle") I leave here a link to the documentation used. Hope it was of help! https://docs.python.org/3/library/turtle.html#turtle.get_shapepoly Turtle Graphics Library. Description and link to relevant section
How to draw a single point using Turtle Graphic
Is there any way to draw a single point using Turtle library instead of drawing a square? I would love to use less memory and be able to draw it faster. At the moment I am using this: def draw_p(alex, d): # trick to save local state PUSH cwd = alex.position() alex.pendown() alex.forward(d) alex.left(90) alex.forward(d) alex.left(90) alex.forward(d) alex.left(90) alex.forward(d) alex.penup() alex.goto(cwd) # POP retrieve back to enter state
You could use the method penup() instead of pendown() if you don't want to draw while the cursor is moving, then you'll draw only points. Besides, you could change the arrow-style shape for a circle-style shape. I've written a simple code to create a spiral made up of several points. Code (Updated) import turtle turtle.setup(800, 600) wn = turtle.Screen() spiral = turtle.Turtle() spiral.color("blue") spiral.penup() size = 10 for i in range(35): spiral.dot() size = size + 2 spiral.forward(size) spiral.right(24) Plot
Creating multiple colored turtles in one window in editor on Python 3.4
So far I have this and it makes two circles but one is off-screen. I want to center it and have them separate from each other. Right now it does two loops but I want it to do one small circle then go on to make a larger one around the first in the middle of the screen. Both need to be diff. colors. def sun_and_earth(): import turtle #allows me to use the turtles library turtle.Turtle() turtle.Screen() #creates turtle screen turtle.window_height() turtle.window_width() turtle.bgcolor('grey') #makes background color turtle.color("red", "green") turtle.circle(2, 360) #draws a (size, radius) circle turtle.circle(218, 360) turtle.exitonclick() #exits out of turtle window on click of window
I think you may have some misunderstanding with regard to some of the functions in the turtle library. Firstly, turtle.window_height() and turtle.window_width() return the height and width of the window, so (as these values are not being assigned) those two lines do nothing. Similarly, turtle.Screen() returns an object, so again that line does nothing. In order to centre your circles, you need to change where the turtle starts by using the turtle.setpos() function. This will change the x and y coordinates of where your turtle is. If you start the turtle one radius down, this will effectively centre the circle at (0, 0), because the center of the circle is (from the documentation) one radius to the left. Remember to take your pen off the page when you are moving so that you don't draw lines between the two points by accident, and to put the pen back down again when you want to draw again. Try this code: import turtle turtle.Turtle() turtle.bgcolor('grey') # decide what your small circle will look like smallColour = "red" smallRadius = 5 # draw the small circle turtle.color(smallColour) turtle.penup() turtle.setpos(0, -smallRadius) turtle.pendown() turtle.circle(smallRadius) # decide what your large circle will look like largeColour = "white" largeRadius = 100 # draw the large circle turtle.color(largeColour) turtle.penup() print(turtle.pos()) turtle.setpos(0, -largeRadius) print(turtle.pos()) turtle.pendown() turtle.circle(largeRadius) turtle.penup() turtle.setpos(0, 0) I hope this helps, but I think that you have a few misunderstandings about the use of the turtle, it might be a good idea to look at a tutorial or maybe take a look at the documentation Best of luck