Hi I'm just learning coding so I have a question about the Turtle function.
In my assignment I am trying to use the turtle to move into a spiral not circular but in a square. Basically I am trying to spin the turtle using a while and for loop. Is that not the right move?
I have no coding that works so giving an example might not work. Sorry...
Example
I am not sure what shape do you want to end up with but this following code will paint you a square
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
as you can see I used 2 function here:
the forward function which moves the line forward in the arrow direction
the left function which spins the arrow left in the specified degree(90 for a square angel)
was that helpful? I'm here for more questions (:
#if you want to make square spiral in python then u must use this
import turtle
t = turtle.Turtle()
side = 200
for i in range(100):
t.forward(side)
t.right(90) #Exterior angle of a square is 90 degree
side = side - 2
Related
This is the bycicle i am trying to make but i cant.
I am really sorry if it gets downgrades and i am really new to python turtle.
I am currently trying to animate. I am done with the animation but i am struglling with making the handle of the bycicle.
After writing the code it dosent make any change.
So if possible. Anyone can make the handle and explain how he did it?
Thank you..
import time
def moving_wheel(turtle):
turtle.fillcolor('orange')
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
def moving_handle(turtle):
pos = turtle.pos()
turtle.left(90)
turtle.forward(70)
turtle.left(120)
turtle.forward(110)
turtle.penup()
turtle.goto(pos)
turtle.right(210)
turtle.pendown()
if __name__ == "__main__":
screen = turtle.Screen()
screen.setup(600, 600)
screen.bgcolor('green')
screen.tracer(0)
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t3 = turtle.Turtle()
# set a turtle object color
t1.color('red')
t2.color('red')
t3.color('red')
# set turtle object speed
t1.speed(0)
t2.speed(0)
t3.speed(0)
t1.width(2)
t2.width(2)
t3.width(1.5)
t1.hideturtle()
t2.hideturtle()
t3.hideturtle()
# turtle object in air
t1.penup()
t2.penup()
t3.penup()
# set initial position
t1.goto(-250, 0)
t2.goto(-150,0)
t3.goto(-150,20)
t3.pendown()
# move turtle object to surface
t1.pendown()
t2.pendown()
# infinite loop
while True:
# clear turtle work
t1.clear()
t2.clear()
t3.clear()
# call function to draw ball
moving_wheel(t1)
moving_wheel(t2)
moving_handle(t3)
# update screen
screen.update()
# forward motion by turtle object
t1.forward(0.1)
t2.forward(0.1)
t3.forward(0.1)
There is no error in your code. You have to only sit and add more forward, left/right, penup/pendown, etc. - and this need to test different value and see which gives expected result.
I would split problem into smaller figures with more regular angles and size.
Bottom part looks like 3 triangles and it would be simpler to use regular triangles with angles 60.
Maybe you should even create function triangle(size) for this.
This is what I tried to draw these triangles.
def moving_handle(turtle):
pos = turtle.pos()
# --- first triangle without last side ---
# rotate to start position
turtle.left(120)
for _ in range(2):
turtle.forward(50)
turtle.left(120)
# move without drawing last side
turtle.penup()
turtle.forward(50)
turtle.left(120)
turtle.pendown()
# rotate back to old position
turtle.left(-120)
# --- second triangle
# move to new position without drawing
turtle.penup()
turtle.left(180)
turtle.forward(50)
turtle.left(-180)
turtle.pendown()
# draw trangle
# rotate to start position
turtle.left(120)
for _ in range(3):
turtle.forward(50)
turtle.left(120)
# rotate back to old position
turtle.left(-120)
# --- second triangle
# draw trangle
# rotate to start position
turtle.left(60)
for _ in range(3):
turtle.forward(50)
turtle.left(120)
# rotate back to old position
turtle.left(-60)
# move to the beginning
turtle.penup()
turtle.goto(pos)
turtle.pendown()
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’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:
What would be the easiest method to draw this on the screen including colors?
It's kind of hard to answer this question without code, but if you know what the colored turtles are called, you can use:
[turtle name goes here].forward([distance])
to move that given turtle forward
[turtle name goes here].backward([distance])
to move it backward
[turtle name goes here].right([degrees/radians])
[turtle name goes here].left([degrees/ radians])
to turn the turtle right and left
for example:
#names the turtle Angie
Angie = Turtle()
#changes the color of Angie
Angie.color("blue")
#moves angie forward
Angie.forward(100)
This should work, hope this helps! :-)
def polygon(length, sides, color):
turtle.color(color)
turtle.begin_fill()
for i in range(sides):
turtle.forward(length)
turtle.left(360.0/sides)
turtle.end_fill()
turtle.penup()
turtle.forward(length*1.5)
turtle.pendown()
polygon(100, 6, "green")
polygon(100, 3, "blue")
polygon(100, 4, "red")
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.