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 thisenter image description here

    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()

Upvotes: 1

Views: 415

Answers (1)

cdlane
cdlane

Reputation: 41872

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.)

Upvotes: 1

Related Questions