from turtle import *
o=Turtle()
d=Screen()
o.begin_fill()
o.width("2")
o.color("yellow")
extent1=150**2*3.14
print(extent1)
o.circle(radius=150,extent=extent1,steps=10)
o.end_fill()
#jasonharper's comment is spot-on (+1). To make the filled star appear the same on both architectures, draw (then fill) just the perimeter of the star you want:
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
turtle.color("yellow")
turtle.begin_fill()
for _ in range(11):
turtle.forward(64)
turtle.right(98.2)
turtle.forward(64)
turtle.left(130.91)
turtle.end_fill()
turtle.hideturtle()
screen.exitonclick()
Related
I a currently trying to animate this bicycle in PYTHON TURTLE. I am struggling if anyone can teach me how to animate it with code, I will be grateful for that. I am adding more details as I am only trying to make the bicycle wheels move from one place to another like the bicycle wheels animation and the bicycle moves while the wheels are also moving thank you a lot!
import turtle
turtle.delay(10)
turtle.pensize(20)
# Move the turtle to the right position of the left tyre
turtle.penup()
turtle.setposition(-87.5, -50)
turtle.setheading(90)
# Draw the left tyre
turtle.begin_fill()
turtle.circle(62.5)
turtle.end_fill()
# Move the turtle to the right position of the right tyre
turtle.penup()
turtle.setposition(212.5, -50)
turtle.setheading(90)
# Draw the right tyre
turtle.begin_fill()
turtle.circle(62.5)
turtle.end_fill()
# Draw the handle
turtle.penup()
turtle.setposition(137.5, 0)
turtle.pendown()
turtle.setposition(75, 150)
# Draw the straight part of the handle
turtle.setheading(0)
turtle.setposition(125, 150)
# Draw the circular part of the handle
turtle.penup()
turtle.setposition(125, 100)
turtle.pendown()
turtle.circle(25, 180)
# Draw the seat tube
turtle.penup()
turtle.setposition(12.5, -50)
turtle.pendown()
turtle.setposition(-62.5, 137.5)
# Draw the saddle
turtle.setheading(180)
turtle.setposition(-100, 137.5)
# Draw the top tube
turtle.penup()
turtle.setposition(100, 75)
turtle.pendown()
turtle.setposition(-37.5, 75)
# Draw the down tube
turtle.penup()
turtle.setposition(100, 75)
turtle.pendown()
turtle.setposition(12.5, -50)
# Draw the seat stay
turtle.penup()
turtle.setposition(-37.5, 75)
turtle.pendown()
turtle.setposition(-150, -50)
# Draw the chain stay
turtle.penup()
turtle.setposition(12.5, -50)
turtle.pendown()
turtle.setposition(-150, -50)
Here is the code I came with for the animation of two wheels and some kind of handlebar :
import turtle
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)
You can modify the code for the handle part, I'm not an artist haha
I want to have a race with these four turtles where each turtle has a different button to move it two pixels forward but i can't get it to work and i was wondering in anyone here could help?
from turtle import *
from random import randint
speed(0)
penup()
goto(-140,140)
for step in range(16):
write(step, align='center')
right(90)
forward(10)
pendown()
forward(150)
penup()
backward(160)
left(90)
forward(20)
ada = Turtle()
ada.color('red')
ada.shape("turtle")
ada.penup()
ada.goto(-160, 100)
ada.right(360)
ada.pendown()
bmb = Turtle()
bmb.color('cyan')
bmb.shape("turtle")
bmb.penup()
bmb.goto(-160, 70)
bmb.right(360)
bmb.pendown()
cbc = Turtle()
cbc.color('lawn green')
cbc.shape("turtle")
cbc.penup()
cbc.goto(-160, 40)
cbc.right(360)
cbc.pendown()
dgd = Turtle()
dgd.color('dark violet')
dgd.shape("turtle")
dgd.penup()
dgd.goto(-160, 10)
dgd.right(360)
dgd.pendown()
You can rely on the turtle library itself for this. For example with the onkey function. Just as an example:
from turtle import *
def up():
# ...handle...
onkey(up, 'Up')
You can find much more about this here
I am studying Python Turtle. I know that the color mode should be 255
in order to use RGB mode. I also know that the correct format is
turtle.dot(int,""). But I get the error:
TurtleGraphicsError: bad color arguments: None
How can I implement RGB?
from turtle import Turtle, Screen
import turtle as t
import random
tim = t.Turtle()
t.colormode(255)
# t.colormode(255)
def rndmclr():
r=random.randrange(255)
g=random.randrange(255)
b=random.randrange(255)
t.pencolor((r,g,b))
t.speed("fastest")
def tendots():
for i in range(10):
t.dot(10,rndmclr())
t.penup()
t.fd(30)
t.pendown()
t.fd(0)
for i in range(10):
tendots()
t.left(90)
t.penup()
t.fd(10)
t.pendown()
t.left(90)
tendots()
t.right(90)
t.penup()
t.fd(10)
t.pendown()
t.right(90)
screen = Screen()
screen.exitonclick()
Your primary flaw is here:
def rndmclr():
r=random.randrange(255)
g=random.randrange(255)
b=random.randrange(255)
t.pencolor((r,g,b))
# ...
t.dot(10,rndmclr())
The rndmclr() sets the random color when it should return the color as it's being used as an argument to dot() where it is currently returning None as the color to use. There are other flaws in your program, most significanly the double import of turtle that has you using both the object-oriented API and the functional API which only leads to trouble -- pick one and stick with it:
from turtle import Turtle, Screen
from random import randrange
def rndmclr():
r = randrange(255)
g = randrange(255)
b = randrange(255)
return (r, g, b)
def tendots():
for _ in range(10):
turtle.dot(10, rndmclr())
turtle.penup()
turtle.forward(30)
turtle.pendown()
screen = Screen()
screen.colormode(255)
turtle = Turtle()
turtle.speed('fastest')
for _ in range(10):
tendots()
turtle.left(90)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.left(90)
tendots()
turtle.right(90)
turtle.penup()
turtle.forward(10)
turtle.pendown()
turtle.right(90)
screen.exitonclick()
I need some help. I want to center the hexagon into the larger hexagon but I don't now how to do it. Below I have the source code and an image link to the output.
import turtle
polygon = turtle.Turtle()
num_sides = 6
side_length = 20
move_left = 60
polygon.pensize(2)
polygon.pencolor((245, 176, 66))
for turtle_move in range(num_sides):
polygon.forward(side_length)
polygon.left(move_left)
polygon.penup()
polygon.left(2)
polygon.pendown()
side_length2 = 40
move_left2 = 60
I want to center the hexagon inside the larger hexagons, but I don't know what to do.
for turtle_move in range(num_sides):
polygon.forward(side_length2)
polygon.left(move_left2)
Here is the output:
There are any number of ways to do this if you read about the geometry of hexagons, eg. on Wikipedia:
from turtle import Screen, Turtle
NUM_SIDES = 6
SIDE_LENGTH = 20
ANGLE_LEFT = 60
screen = Screen()
turtle = Turtle()
for _ in range(NUM_SIDES):
turtle.forward(SIDE_LENGTH)
turtle.left(ANGLE_LEFT)
turtle.penup()
turtle.backward(SIDE_LENGTH / 2)
turtle.sety(-SIDE_LENGTH * 3**0.5/2)
turtle.pendown()
for _ in range(NUM_SIDES):
turtle.forward(SIDE_LENGTH*2)
turtle.left(ANGLE_LEFT)
turtle.hideturtle()
screen.exitonclick()
One alternate approach is to use the turtle circle() method to draw the hexagons, then it becomes a matter of centering two circles:
from turtle import Screen, Turtle
NUM_SIDES = 6
SIDE_LENGTH = 20
circumradius = SIDE_LENGTH
screen = Screen()
turtle = Turtle()
for _ in range(2):
turtle.penup()
turtle.sety(-circumradius)
turtle.pendown()
turtle.circle(circumradius, steps=NUM_SIDES)
circumradius *= 2
turtle.hideturtle()
screen.exitonclick()
I'm assuming by your use of pencolor((245, 176, 66)) you're using a site like Repl.it or some other non-standard Python turtle implementation, so you may need to adjust the examples above slightly to suit your environment.
pink square is the taskI am trying to draw a square inside a square using turtle graphics. I couldn't draw exactly like the original one.
Here is my code
import turtle
win = turtle.Screen()
tess = turtle.Turtle()
for two in range(12):
for _ in range(4):
tess.forward(20)
tess.left(90)
tess.penup()
tess.goto(-7,-4)
tess.pendown()
for _ in range(4):
tess.forward(35)
tess.left(90)
tess.penup()
tess.goto(-14,-8)
tess.pendown()
for _ in range(4):
tess.forward(50)
tess.left(90)
tess.penup()
tess.goto(-21,-16)
tess.pendown()
for _ in range(4):
tess.forward(70)
tess.left(90)
A good example for better living through stamping instead of drawing:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
screen = Screen()
screen.bgcolor('lightgreen')
turtle = Turtle()
turtle.shape('square')
turtle.color('pink', 'lightgreen')
for size in range(100, 0, -20):
turtle.shapesize(size / CURSOR_SIZE, outline=3)
turtle.stamp()
screen.exitonclick()
I think, my code solution your problem.
from turtle import *
def draw_square(a,color,x,y):
penup()
goto(x,y)
setheading(90)
backward(a//2)
setheading(0)
backward(a//2)
pendown()
pencolor(color)
for _ in range(4):
forward(a)
left(90)
draw_square(20,"pink",0,0)
draw_square(50,"pink",0,0)
draw_square(70,"pink",0,0)
draw_square(90,"pink",0,0)
draw_square(110,"pink",0,0)