how to make lined circle with turtle in python - python

How to make draw like this with turtle?
right now my code looks like that:
class OperationsOnSets():
def __init__(self):
self.font_style = ("Times New Roman", 40, "bold")
def move_turtle_pos(self, x, y, turtle):
turtle.up()
turtle.setpos(x, y)
turtle.down()
def set_d(self):
turtle = Turtle()
turtle.pensize(2)
turtle.speed(2)
self.move_turtle_pos(-100, -50, turtle)
turtle.circle(200)
self.move_turtle_pos(100, -50, turtle)
turtle.circle(200)
sleep(5)
turtle.mainloop()
example = OperationsOnSets()
example.set_d()
and here is result
I though about pasting image, or make algorithm that would draw a lines, but I don`t know how to realize it. So I hope that someobody of you will help me with it...

My philosophy of turtles, is avoid the math by getting the turtle to do the hard work for you. Not quite perfect, but pretty close:
from turtle import Screen, Turtle
RADIUS = 200
NUMBER_LINES = 14
class OperationsOnSets():
def __init__(self):
self.turtle = Turtle()
self.turtle.pensize(2)
self.turtle.fillcolor(screen.bgcolor())
def move_turtle_pos(self, x, y):
self.turtle.penup()
self.turtle.setpos(x, y)
self.turtle.pendown()
def set_d(self):
self.move_turtle_pos(-RADIUS/2, -RADIUS)
points = []
for _ in range(NUMBER_LINES):
self.turtle.circle(RADIUS, 180 / NUMBER_LINES)
points.append(self.turtle.position())
for _ in range(NUMBER_LINES):
self.turtle.circle(RADIUS, 180 / NUMBER_LINES)
position = self.turtle.position()
self.turtle.goto(points.pop())
self.turtle.goto(position)
self.move_turtle_pos(RADIUS/2, -RADIUS)
self.turtle.begin_fill()
self.turtle.circle(RADIUS)
self.turtle.end_fill()
self.move_turtle_pos(-RADIUS/2, -RADIUS)
self.turtle.circle(RADIUS, 180)
self.turtle.hideturtle()
screen = Screen()
example = OperationsOnSets()
example.set_d()
screen.mainloop()
We make the turtle stop along its way around the circle to record points we'll need to finish the drawing.

Related

drawing multiple circles with turtle in python

I am having problems drawing circles and fitting the equation together I want to draw 3 circles in random locations that I have set with random radiuses like I set up but I can't fit the circumference equation in the draw class for it to work. like this
import turtle
from turtle import RawTurtle
class circle:
def __init__(self, R, D, turtle):
self._a = R
self._b = D
self._turtle = turtle
turtle.up()
def set_initial_position(self, x: float, y: float):
self._turtle.setpos(x, y)
def draw(self):
t = self._turtle
for _ in range(int(draw)):
turtle.circle(25)
turtle.forward(10)
t.up()
def hide(self):
self._turtle.clear()
r1 = circle(5, 5, RawTurtle(turtle.getscreen()))
r2 = circle(5, 10, RawTurtle(turtle.getscreen()))
r3 = circle(10, 20, RawTurtle(turtle.getscreen()))
r2.set_initial_position(-50, 100)
r3.set_initial_position(60, 200)
r1.draw()
r2.draw()
r3.draw()
r2.hide()
turtle.mainloop()
I can't tell what your program is trying to do from your explanation and code but below I've reworked it into something that runs from which you should be able to build what you really want:
from turtle import Screen, Turtle
class circle:
def __init__(self, radius, degrees, turtle):
self._r = radius
self._d = degrees
self._turtle = turtle
self._turtle.penup()
def set_initial_position(self, x, y):
self._turtle.setposition(x, y)
def draw(self):
self._turtle.pendown()
self._turtle.circle(self._r, extent=self._d)
self._turtle.penup()
def hide(self):
self._turtle.clear()
screen = Screen()
r1 = circle(150, 45, Turtle())
r2 = circle(15, 180, Turtle())
r3 = circle(100, 360, Turtle())
r2.set_initial_position(-50, 100)
r3.set_initial_position(60, 200)
r1.draw()
r2.draw()
r1.hide()
r3.draw()
screen.mainloop()
You seem to be using the tkinter embedded turtle API for a standalone turtle program so I've switched the method calls. And as the Python error notes, you use a draw variable which you fail to define. (And you shouldn't define one as you already have a draw function -- choose a different name for the variable.)

Turtle Graphics Hexagon Centering

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.

Draw a square using two clicks, one to set start, and another for edge length

How can I code turtle to draw a square like in paint, using two clicks -- one to set the starting point and a second click for the defined edge length?
I wanna: click once to set the center of a square; move the mouse to define the edge-length of the square; click a second time to draw the square with the defined edge-length and center point. But I couldn't manage it; do you have any ideas? I also tried to create a color palette so I can change the drawing color of my turtle if I click on one of the colors but it doesn't work.
Here's my code:
import turtle
beni=turtle.Screen()
beni.setup(900,700,)
t=turtle.Turtle()
#color palette
t1=turtle.Turtle()
t1.shape("circle")
t1.color("blue")
t1.penup()
t1.setposition(250, 300)
t2=turtle.Turtle()
t2.shape("circle")
t2.color("red")
t2.penup()
t2.setposition(220,300)
t3=turtle.Turtle()
t3.shape("circle")
t3.color("green")
t3.penup()
t3.setposition(280, 300)
def funktion(x,y):
t.color("green")
t.pencolor("green")
t3.onclick(funktion)
def funktion(x, y):
t.color("blue")
t.pencolor("blue")
t2.onclick(funktion)
def funktion(x, y):
t.color("red")
t.pencolor("red")
t1.onclick(funktion)
#freehandmode
def freehandmode(x, y):
t.ondrag(None)
t.goto(x, y)
t.ondrag(freehandmode)
t.ondrag(freehandmode)
#linemode
class Drawer:
def __init__(self):
self.drawing = False
def click(self, x, y):
if self.drawing:
turtle.down()
turtle.goto(x, y)
self.drawing = False
else:
turtle.up()
turtle.goto(x, y)
self.drawing = True
d = Drawer()
beni.onclick(d.click)
#squaremode
turtle.up()
turtle.mainloop()
There are several ways to go about this, I'm going to do it by swapping event handlers so the first and second clicks have different meanings. I'm going to use circles (pick the center, then the edge) to keep my example simple and leave some geometry for you to ponder:
from turtle import Screen, Turtle
def draw_circle(x, y):
screen.onclick(None)
center = turtle.position()
turtle.setposition(x, y)
turtle.setheading(turtle.towards(center) - 90)
turtle.pendown()
turtle.circle(turtle.distance(center))
turtle.penup()
turtle.clearstamps()
screen.onclick(pick_center)
def pick_center(x, y):
screen.onclick(None)
turtle.setposition(x, y)
turtle.stamp()
screen.onclick(draw_circle)
turtle = Turtle()
turtle.hideturtle()
turtle.shape('circle')
turtle.shapesize(0.5)
turtle.penup()
screen = Screen()
screen.onclick(pick_center)
screen.mainloop()

Python turtle mouse does not follow directly

How do you make turtle move without using turtle.goto(x,y) but turtle.speed(speed) and turtle.heading(angle)? I need this for a game I am making. Where the mouse is, I want to make it go in that direction. But when I change it, it goes to that place then to my mouse:
import turtle
screen = turtle.Screen()
screen.title("Test")
screen.bgcolor("white")
screen.setup(width=600, height=600)
ship = turtle.Turtle()
ship.speed(1)
ship.shape("triangle")
ship.penup()
ship.goto(0,0)
ship.direction = "stop"
ship.turtlesize(3)
turtle.hideturtle()
def onmove(self, fun, add=None):
if fun is None:
self.cv.unbind('<Motion>')
else:
def eventfun(event):
fun(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale)
self.cv.bind('<Motion>', eventfun, add)
def goto_handler(x, y):
onmove(turtle.Screen(), None)
ship.setheading(ship.towards(x, y)) #this is where you use the x,y cordinates and I have seat them to got to x,y and set heading
ship.goto(x,y)
onmove(turtle.Screen(), goto_handler)
onmove(screen, goto_handler)
If you only setheading and speed it just turns that way and does not move. If you try this code it works -- it is just that I use ship.goto(x, y) which makes it go to (x, y). But when you change your mouse when it is moving, it first goes to (x, y) then to your new mouse position. I pretty much just want it to just follow the mouse but I can not do that.
I believe the code below gives you the motion you desire. It only uses onmove() to stash the target's position and uses an ontimer() to aim and move the turtle. It also stops when the target has been enveloped:
from turtle import Screen, Turtle, Vec2D
def onmove(self, fun, add=None):
if fun is None:
self.cv.unbind('<Motion>')
else:
def eventfun(event):
fun(Vec2D(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale))
self.cv.bind('<Motion>', eventfun, add)
def goto_handler(position):
global target
onmove(screen, None)
target = position
onmove(screen, goto_handler)
def move():
if ship.distance(target) > 5:
ship.setheading(ship.towards(target))
ship.forward(5)
screen.ontimer(move, 50)
screen = Screen()
screen.title("Test")
screen.setup(width=600, height=600)
ship = Turtle("triangle")
ship.turtlesize(3)
ship.speed('fast')
ship.penup()
target = (0, 0)
onmove(screen, goto_handler)
move()
screen.mainloop()

Multiple Turtles, multiple random movements

I have an assignment where I have to make a game in python with turtles.
I am able to generate a grid like I want to, and I am able to generate multiple different turtles. my
- I have no clue where to start in terms of figuring out how to have each of my three different turtles each move throughout the board randomly, while staying on the grid line.
- the "game" must stop after two of the turtles have hit the edge of the screen, I do not know how to do this, either.
here is what I have so far:
import turtle
import random
turtle.pensize(2)
turtle.penup()
turtle.goto(-160,-160)
turtle.pendown()
turtle.color("green")
turtle.speed(0)
for i in range(4):
turtle.forward(320)
turtle.left(90)
for y in range(-160,160,80):
for x in range(-160,160,80):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
for k in range(4):
turtle.forward(40)
turtle.left(90)
turtle.end_fill()
for y in range(-120,160,80):
for x in range(-120,160,80):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
for k in range(4):
turtle.forward(40)
turtle.left(90)
billy = turtle.Turtle()
billy.shape("turtle")
billy.color("red")
billy.penup()
billy.left(90)
rick = turtle.Turtle()
rick.shape("turtle")
rick.color("orange")
rick.penup()
mike = turtle.Turtle()
mike.shape("turtle")
mike.color("purple")
mike.penup()
turtle.done()
strong guidance would be VERY helpful.
You could take advantage of object orientation. Here is my version of your turtle script:
import turtle
import random
# I added randint.
from random import randint
# Let's define a class so we can create turtles
class MyTurtle():
# Here's the constructor. Where we give some initial values.
def __init__(self, name, color):
self.name = name
self.color = color
self.turtle = turtle.Turtle()
self.turtle.shape("turtle")
self.turtle.color(self.color)
self.turtle.speed(1)
self.turtle.pd()
# We need to know if this turtle is off the board.
def off_board(self):
x = self.turtle.xcor()
y = self.turtle.ycor()
return x < -160 or 160 < x or y < -160 or 160 < y
# calling this will move the turtle in a random direction.
def move(self):
turn = randint(0,2)
if turn == 1:
self.turtle.lt(45)
elif turn == 2:
self.turtle.rt(45)
self.turtle.forward(5)
# Put your code for drawing the grid
# Let's create some turtles and start moving them.
turtles = []
turtles.append( MyTurtle('billy', 'red'))
turtles.append( MyTurtle('chris', 'blue'))
turtles.append( MyTurtle('lilly', 'pink'))
turtles.append( MyTurtle('kevin', 'green'))
ok = True
while ok:
for t in turtles:
t.move()
if t.off_board():
print ("Turtle %s wins!!!" % (t.name))
ok = False
turtle.done()
Come on! Just one upvote for this awesome turtle script?

Categories

Resources