0

the below code is one part of my code. I would like to print random colored circles. But it does not work. Can someone please help me to correct this code? circles shouldnt be overalapped!! ERROR is bad color sequence: (164, 13, 120)

 from random import randint
from svg_turtle import SvgTurtle
import turtle
from turtle import Turtle
import json


def fiber_circle(fiber, width, height):
    fiber_r=25
    fiber_num = 100
    cursor_size = 20
    fiber.hideturtle()
    fiber.screen.bgcolor("white")
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)
    fiber.color(r,g,b)
    fiber.screen.colormode(255)
    fiber.shape("circle")
    fiber.shapesize(fiber_r / cursor_size)
    fiber.speed("fastest")
    fiber.penup()
    fibers = [] 
    
    for _ in range(fiber_num):
        fiberr = fiber.clone()
        fiberr.setposition(
            randint(-width / 2, width / 2),
            randint(-height / 2, height / 2),
        )

        
ggorlen
  • 44,755
  • 7
  • 76
  • 106
dilara
  • 19
  • 9
  • How are you calling this function? What does "does not work" entail exactly? Do you get an error? – ggorlen Sep 30 '22 at 21:50
  • it does not give random colors. And also it gives error: TurtleGraphicsError: bad color sequence: (120, 158, 58). when i define it like fiber.color("black"), t gives black circles. But when ı want to do random colors, it doesnot work. @ggorlen – dilara Sep 30 '22 at 22:07
  • Thanks for clarifying that. Did you see [what does bad color sequence mean in python turtle?](https://stackoverflow.com/questions/16778324/what-does-bad-color-sequence-mean-in-python-turtle) and [How can I make the turtle a random color?](https://stackoverflow.com/questions/46091442/how-can-i-make-the-turtle-a-random-color)? – ggorlen Sep 30 '22 at 22:09
  • I saw it. i added turtle.colormode(255). now it doesnot print anything. @ggorlen – dilara Sep 30 '22 at 22:41
  • Okay, good--you're heading in the right direction. I'm still not seeing the full/updated code though. Can you [edit] the post to show it? Thanks. – ggorlen Sep 30 '22 at 22:57
  • BTW, `turtle.colormode(255)` is white, which likely blends in with your background, so you probably want to try some of the code from the [How can I make the turtle a random color?](https://stackoverflow.com/questions/46091442/how-can-i-make-the-turtle-a-random-color) thread. Big hint: `from random import randint as rnd; fiber.color(rnd(255), rnd(255), rnd(255))`. – ggorlen Sep 30 '22 at 23:10
  • @ggorlen, `turtle.colormode(255)` doesn't specify the color "white", it specifies which color system is in control (0 - 255 vs. 0.0 - 1.0 [default]). I think you're confusing it with `turtle.color(255, 255, 255)`. – cdlane Oct 04 '22 at 21:08
  • @cdlane thanks for the correction, you're right – ggorlen Oct 04 '22 at 21:10
  • I editedn@ggorlen – dilara Oct 06 '22 at 09:54
  • Now the code works but I do not know why it gives all purple or all red. It did not give random color. could yu please check it? @ggorlen – dilara Oct 15 '22 at 18:52
  • You should probably be asking a new question instead of moving the goalpost. As a Q&A site, we're trying to curate a resource for future visitors with a clear problem and answer. If you change the question, it becomes a confusing situation because the existing accepted answer no longer pertains to the question. I rolled back your edit. Thanks. – ggorlen Oct 15 '22 at 19:08

1 Answers1

0

Your program, if it ran, would simply change the turtle cursor into different colors, but in one location. If you want to draw randomly colored circles all over your window, you can try something like the following which uses dot() to draw them:

from turtle import Screen, Turtle
from random import randint

FIBER_RADIUS = 25
FIBER_NUMBER = 100
CURSOR_SIZE = 20

def fiber_circle(fiber):
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)

    x = randint(FIBER_RADIUS - width//2, width//2 - FIBER_RADIUS)
    y = randint(FIBER_RADIUS - height//2, height//2 - FIBER_RADIUS)

    fiber.color(r, g, b)
    fiber.goto(x, y)
    fiber.dot(FIBER_RADIUS * 2)  # dot() takes a diameter

screen = Screen()
screen.colormode(255)

width, height = screen.window_width(), screen.window_height()

fiber = Turtle()
fiber.hideturtle()
fiber.speed("fastest")
fiber.penup()

for _ in range(FIBER_NUMBER):
    fiber_circle(fiber)

screen.exitonclick()

Alternatively, you can use your original approach of shaping, sizing and coloring the turtle itself, but then use stamp() to leave behind a circle while moving the turtle randomly around the screen:

...

def fiber_circle(fiber):
    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)

    x = randint(FIBER_RADIUS - width//2, width//2 - FIBER_RADIUS)
    y = randint(FIBER_RADIUS - height//2, height//2 - FIBER_RADIUS)

    fiber.color(r, g, b)
    fiber.goto(x, y)
    fiber.stamp()

screen = Screen()
screen.colormode(255)

width, height = screen.window_width(), screen.window_height()

fiber = Turtle()
fiber.hideturtle()
fiber.shape('circle')
fiber.shapesize(FIBER_RADIUS * 2 / CURSOR_SIZE)  # CURSOR_SIZE is effectively a diameter
fiber.speed('fastest')
fiber.penup()

for _ in range(FIBER_NUMBER):
    fiber_circle(fiber)

...
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • I did it worked but circles overlaped with each other. I also edited my main code, you can check it. how can i prevent overlapping'? – dilara Oct 06 '22 at 10:17
  • @dilara, one approach is you keep a list of positions of each circle. When you go to add a new circle, you use the `distance()` method to make sure that you're no closer than a radius to any other circle. If too close, generate a new random position and try again. – cdlane Oct 06 '22 at 15:55
  • Unfortunately, it does not work at all again- I edited question and rewrite the code. Could you please check it'? I cannit see that where I am wrong. @cdlane – dilara Oct 10 '22 at 07:08