0

I have this code, using turtle graphics in Python:

import turtle
import time as wait
turtle.setup(500,500)
ws = turtle.Screen()
ws.title('epic turtl')
ws.bgcolor('black')
turtle.tilt(73)
turtle.color('red','yellow')
turtle.turtlesize(5,5,7)
turtle.shape('circle')
turtle.penup()
i = 1
color_r = 1.0
color_b = 1.0
color_g = 1.0
while i <= 360:
    turtle.forward(1)
    turtle.left(1)
    i += 1
    color_r += 1.0
    color_b += 1.0
    color_g += 1.0

    turtle.bgcolor(color_r, color_g, color_b)
wait.sleep(10)

I'm trying to change the color_r, color_g and color_b values by 1 each time. However, I get an error: bad color sequence: (2.0, 2.0, 2.0)

Why does this occur, and how do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
cycooYT
  • 11
  • 3
  • Just to make sure, because it wasn't clear from your original post: you do want to change the *background colour* of the *window*, yes? As far as I can tell, the colour of the turtle itself cannot be changed. – Karl Knechtel Jun 03 '22 at 04:04
  • @KarlKnechtel, both the outline color and fill color of the turtle itself *can* be changed at any time. But here it is clearly the *window* background color that's the issue, as it moves from night to day. – cdlane Jun 03 '22 at 15:42
  • @cdlane I know there is a pen color and a fill colour for what is drawn; but I thought OP might have meant the image that is used to represent the turtle itself. There are a few images to choose from, but I don't see a way to recolour it. – Karl Knechtel Jun 03 '22 at 17:51
  • 1
    @KarlKnechtel, setting the outline and fill color changes the image representing the turtle itself to have that color outline or fill, just as it changes what it draws. – cdlane Jun 03 '22 at 20:14
  • Ah, I didn't realize. – Karl Knechtel Jun 03 '22 at 21:52

2 Answers2

2

By default, the colormode for the window is 1.0, meaning that that is the maximum value for each of the red, green and blue components of the colour. By adding 1.0, the values become 2.0, which is out of range.

The colormode may be set to either 1.0 (meaning that floating-point values between 0.0 and 1.0 should be used), or 255 (meaning that integers from 0 to 255 inclusive should be used). Either way, you need to think about logic that will pick valid values, every time through the loop. Also make sure that you understand how RGB colour works, so that you can get colours that look the way you want. Changing all the values to be equal will give you shades of grey.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
0

Combining @KarlKnechtel's explanation of turtle color modes (+1) with a rewrite of your code for clarity, I get:

from turtle import Screen, Turtle

screen = Screen()
screen.setup(500, 500)
screen.title('epic turtle')
screen.bgcolor('black')

turtle = Turtle()
turtle.shape('circle')
turtle.turtlesize(5, 5, 7)
turtle.color('red', 'yellow')
turtle.penup()

for angle in range(360):
    turtle.forward(1)
    turtle.left(1)

    color_r = angle/360
    color_g = angle/360
    color_b = angle/360
    screen.bgcolor(color_r, color_g, color_b)

screen.exitonclick()

Here we're moving the gray color from 0.0 to 1.0 via the steps of the for loop, 1/360th each iteration.

cdlane
  • 40,441
  • 5
  • 32
  • 81