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()
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.
The fillcolor() is not working at all in this function -- I can not figure out why. It worked in all of my other functions:
from turtle import Turtle
from random import randint
t = Turtle()
def rocks():
for i in range(5):
t.penup()
t.goto(randint(-300,0), randint(-200,0))
for x in range(40):
t.pendown()
t.fillcolor("gray")
t.fillcolor()
t.begin_fill()
t.forward(5)
t.left(25)
t.right(27)
t.forward(5)
t.right(20)
t.end_fill()
t.speed("fastest")
rocks()
The problem is that you have the begin_fill() & end_fill() inside your loop which means they are trying to fill short line segments. You need them around your loop to fill the entire shape:
from turtle import Turtle, Screen
from random import randint
def rocks(t):
t.fillcolor('gray')
for _ in range(5):
t.penup()
t.goto(randint(-300, 0), randint(-200, 0))
t.begin_fill()
for _ in range(15):
t.pendown()
t.forward(5)
t.right(2)
t.forward(5)
t.right(22)
t.end_fill()
screen = Screen()
turtle = Turtle()
turtle.speed('fastest')
rocks(turtle)
turtle.hideturtle()
screen.exitonclick()
I am trying to move the square to right and draw 36 squares to make a circle out of it:
def draw_art(x,y):
print("Started the op")
window = turtle.Screen()
window.bgcolor("blue")
print("start the drwaing")
c =0
brad = turtle.Turtle()
brad.shape("turtle")
brad.color("green")
brad.speed(3)
print("enter loop")
for i in range(1,37):
draw_square(x,y)
brad.right(10)
window.exitonclick()
draw_art(200,90)
This might get you closer to what you're trying to do. It defines the missing draw_square() function and then draws 36 squares in a circle:
from turtle import Screen, Turtle
def draw_square(turtle):
for _ in range(4):
turtle.forward(50)
turtle.right(90)
def draw_art(turtle, x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for _ in range(36):
draw_square(turtle)
turtle.right(10)
print("Started the app")
window = Screen()
window.bgcolor('blue')
brad = Turtle()
brad.shape('turtle')
brad.color('green')
brad.speed('fastest') # because I have no patience
print("Start the drawing")
draw_art(brad, 200, 90)
brad.hideturtle()
window.exitonclick()
When going through the book "How to think like...", I got stuck with exercise 4.9.2.
The question is: "Write a program to draw this. Assume the innermost square is 20 units per side, and each successive square is 20 units bigger, per side, than the one inside it"
The following code represents how far I got till now:
import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
def draw_square(t,size):
for i in range(4):
tess.forward(size)
tess.left(90)
size = 20
for j in range(3):
tess.pensize(3)
draw_square(tess,size)
size = size + 20
tess.penup()
tess.goto(-20, -20)
tess.pendown()
wn.mainloop()
Can someone be so kind and show me the right direction please?
Thanks!
Swen
The problem is here:
tess.goto(-20, -20)
You have two issues. First, if each square is 20 units bigger and you offset each square by (-20, -20) all squares would share one corner. Instead you want to offset the corner of the square by (-10, -10) so that the inner square is offset by 10 units on all sides.
The second issue is that .goto(x, y) sets an absolute position, not an offset. To move to an offset you need to calculate a new absolute position based on the offset:
tess.goto(tess.xcor()-10, tess.ycor()-10)
Or
tess.goto(tess.pos() + (-10, -10))
Sometimes when you're stuck, a good approach with turtle graphics is to think outside the square. If we consider the desired result as badly drawn concentric circles then the problem reduces to:
from turtle import Turtle, Screen
HYPOTENUSE = (2 ** 0.5) / 2
screen = Screen()
screen.bgcolor("lightgreen")
tess = Turtle()
tess.pencolor("red")
tess.setheading(45)
tess.width(3)
for radius in range(20, 20 * 5 + 1, 20):
tess.penup()
tess.goto(radius/2, -radius/2)
tess.pendown()
tess.circle(radius * HYPOTENUSE, steps=4)
screen.exitonclick()
OUTPUT
import turtle
def drawSquare (t, size):
for i in range (4):
t.forward(size)
t.left(90)
def main():
wn = turtle.Screen()
wn.bgcolor('black')
pat = turtle.Turtle()
pat.pensize(2)
pat.speed(10)
pat.color('blue')
space = -10
for i in range(20, 105, 20):
drawSquare(pat,i)
pat.up()
pat.goto(space, space)
pat.down()
space = space - 10
wn.exitonclick()
main()
import turtle
def draw_sqr(name,size):
for i in range(4):
name.forward(size)
name.left(90)
name.penup()
name.backward(10)
name.right(90)
name.forward(10)
name.left(90)
name.pendown()
window = turtle.Screen()
window.bgcolor('lightgreen')
window.title("conc_sqr")
x = turtle.Turtle()
x.color('hotpink')
x.pensize(3)
for i in range(5):
draw_sqr(x,20 + 20*i)
window.mainloop()
def draw_rectangle(animal, width, height):
for _ in range(2):
animal.forward(width)
animal.left(90)
animal.forward(height)
animal.left(90)
def draw_square(animal, size):
draw_rectangle(animal, size, size)
def make_window(back_color,title):
window = turtle.Screen()
window.bgcolor(back_color)
window.title(title)
return window
def make_turtle(color, size):
animal = turtle.Turtle()
animal.color(color)
animal.pensize(size)
return animal
import turtle
window = make_window("lightgreen","Squares")
tess = make_turtle("hotpink",3)
size_sqr=20
adjustment = 10
for _ in range(5):
draw_square(tess,size_sqr)
size_sqr += 20
tess.penup()
tess.backward(20/2)
tess.right(90)
tess.forward(20/2)
tess.left(90)
tess.pendown()
import turtle
def drawsquare(t,sz): #(turtle, size)
for i in range(4):
t.fd(sz)
t.right(90)
def main():
wn = turtle.Screen()
your_turtle = turtle.Turtle()
your_turtle.pensize(3)
wn.bgcolor("light green")
your_turtle.pencolor("hot pink")
for i in range(1,6):
drawsquare(your_turtle,20*i)
your_turtle.up()
your_turtle.left(135)
your_turtle.fd(14.142) #the distance between each square
your_turtle.right(135)
your_turtle.down()
main()
wn.exitonclick()