Python turtle color list

Python Turtle Colors + Examples

In this Python tutorial, we will learn How to create colors in Python Turtle and we will also cover different examples related to Turtle colors. And, we will cover these topics.

  • Python turtle color
  • Python turtle colors rgb
  • Python turtle colors fill
  • Python turtle color chart
  • Python turtle color codes
  • Python turtle color random
  • Python turtle color gradient
  • Python turtle color change

Python turtle color

In this section, we will learn about how to create colors in Python Turtle.

Turtle is a feature of Python in which we can draw various shapes and also fill colors in between these shapes. Turtle is working as a drawing board and we can also create classes and define functions.

In the following code, we generate some colors such as “green”, “pink” we fill these colors to make output attractive.

  • begin_fill()is used before drawing the shape to be filled.
  • forward(150)is used to move the cursor in the forwarding direction.
  • end_fill()is used after drawing the shape for termination.
from turtle import * color('green', 'pink') begin_fill() while True: forward(150) left(120) if abs(pos()) < 1: break end_fill() done()

After running the above code we get the following output we see the cursor move forward with green color and make the triangle and fill the triangle with pink color.

Python turtle colors RGB

In this section, we will learn how to create RCB colors in Python turtle.

As we know turtle is a feature used in Python is used as a drawing board we can draw any shape of any color in this. We create an RCB color to make a shape.

in the following code, we import the turtle package as import turtle and give the turtle shape to the cursor and also give the pen color as “RGB”.

  • turtle.shape(“turtle”) gives the shape to the cursor as a turtle.
  • turtle.pencolor() is used for giving the color and make the graphic attractive.
  • turtle.forward(120) is used for moving the turtle in a forwarding direction.
from turtle import * import turtle turtle.shape("turtle") turtle.colormode(255) turtle.forward(120) turtle.pencolor("red") turtle.right(90) turtle.forward(120) turtle.pencolor(("blue")) turtle.right(90) turtle.forward(120) turtle.pencolor("green") turtle.right(90) turtle.forward(120)

In the following output, we see a square is created with three beautiful colors “RGB”. Black is the default color.

Python turtle colors fill

In this section, we will learn how to fill the colors in Python turtle.

Python turtle has some function for moving the turtle i.e forward(), backward() and there are fill functions also for filling the shape choose the color and fill the shape i.e fillcolor().

In the following code, we creating a turtle pen for drawing the hexagonal shape. And also set the fill color that filled in a hexagonal shape.

  • tur = turtle.Turtle()for this we creating a turpel pen.
  • tur.fillcolor(“yellow”) is for set the fill color.
  • tur.begin_fill() for starting of the fill color.
  • tur.end_fill() is used for ending of filling of the color.
from turtle import * import turtle tur = turtle.Turtle() tur.fillcolor("yellow") tur.begin_fill() for _ in range(6): tur.forward(150) tur.right(-60) tur.end_fill()

After running the above code we get the following output in which we see a hexagon is filled with yellow color.

Читайте также:  Закрытие программы python код

Python turtle color chart

In this section, we will learn how to create a color chart in Python turtle.

The chart is defined as giving information in the form of a graph or table. Color char is used for giving the information different colors that are filled inside the chart.

In the following code, we import the turtle package as import turtle and define the function that draws the turtle as def drawbar(tur, height, color): we create a bar chart and filled this bar with color.

  • tur.fillcolor(color) is used to start filling the shape.
  • tur.end_fill() when all the bars are completely filled this is used for stop filling.
import turtle def drawbar(tur, height, color): tur.fillcolor(color) tur.begin_fill() tur.left(90) tur.forward(height) tur.write(str(height)) tur.right(90) tur.forward(40) tur.right(90) tur.forward(height) tur.left(90) # stop filling the shape tur.end_fill() values = [40, 115, 185, 90, 132] colors = ["red", "purple", "brown", "pink", "green"] maxheight = max(values) numbar = len(values) bord = 5 ws = turtle.Screen() ws.setworldcoordinates(0 - bord, 0 - bord, 40 * numbar + bord, maxheight + bord) tur = turtle.Turtle() tur.pensize(3) for i in range(len(values)): drawbar (tur, values[i], colors[i]) ws.exitonclick()

In the following output, we create a bar chart, and this bar chart is filled with different colors shows the color chart.

Python turtle color chart

Python turtle color codes

In this section, we will learn how to create color code in Python turtle.

Color code works on a different platform to provide a beautiful frontend for user sight which helps us to make the view pages user-friendly.

In the following code, we import the package as import turtle and give the color to the turtle when the turtle moves the color change to green and then change to red and we mention these colors in the form of code also.

turtle.forward(50) is used to move forward direction.

from turtle import * import turtle turtle.speed(1) turtle.forward(50) turtle.color("green") turtle.right(90) turtle.forward(50) turtle.pencolor(("blue")) turtle.right(90) turtle.forward(70)

In the following output, we see there is a turtle with the default color black, and then the turtle moves in the forward direction with different beautiful colors.

Python turtle color random

In this section, we will learn how to generate random colors in Python turtle.

Random color is defined as taking some color randomly without using any function. The color is generated randomly which gives the beautiful look to the screen.

In the following code, we import the module from random import randint for generating random colors which attract the viewer’s eyes.

  • pensize(12) is used to gives the size to the pen.
  • colormode(255) is used to show every type of color.
  • color(randint(0, 255) randint will have random color based on every randint color will be called.
  • begin_fill() it will begin to fill the circle with color.
  • circle(10) generate the circle.
  • end_fill() it will end to fill the color.
from turtle import * from random import randint speed(0) pensize(12) colormode(255) while True: color(randint(0, 255), randint(0, 255), randint(0, 255)) begin_fill() circle(10) end_fill() penup() goto(randint(-300, 300), randint(-200, 170)) pendown()

After running the above code, we get the following output in which we see some random circle is generated which looks very pretty.

Читайте также:  2017 php id com

Python turtle color gradient

In this section, we will learn about how to create color gradients in Python turtle.

Color gradient identifies a range of positions in which the color is used to fill the region. The gradient is also known as a continuous color map.

In the following code, we define some colors and set a target we also create a screen and give width and height in which the color gradient identifies the range of position in which the color is used to fill the region.

from turtle import * from turtle import Screen, Turtle COL = (0.60156, 0, 0.99218) TAR = (0.86328, 0.47656, 0.31250) screen = Screen() screen.tracer(False) WID, HEIG = screen.window_width(), screen.window_height() delt = [(red - COL[index]) / HEIG for index, red in enumerate(TAR)] tur = Turtle() tur.color(COL) tur.penup() tur.goto(-WID/2, HEIG/2) tur.pendown() direction = 1 for distance, y in enumerate(range(HEIG//2, -HEIG//2, -1)): tur.forward(WID * direction) tur.color([COL[i] + delta * distance for i, delta in enumerate(delt)]) tur.sety(y) direction *= -1 screen.tracer(True) screen.exitonclick()

In the following output, we see different colors are shown on the screen as a continuous color map.

Python turtle color gradient

Python turtle color change

In this section, we will learn how to change the color of the Python turtle.

We change the color of the things to give an attractive look. In turtle, we change the color by pencolor() it changes the color of the ink of the turtle and the default color is black.

In the following code, we know the default color of the turtle is black we change the turtle color black to “purple”.

  • turtle.speed(1) is used to slow the speed of the turtle.
  • turtle.forward(80) is used to move forward.
  • turtle.color(“purple”) is used to change the color of the turtle.
from turtle import * import turtle turtle.speed(1) turtle.forward(80) turtle.color("purple") turtle.forward(80)

After running the above code, we get the following output in which we see the arrow move forward they have the default color black and after in few seconds, the color changes into purple.

You may like the following Python tutorials:

So, in this tutorial, we discussed Python turtle colors and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python turtle color
  • Python turtle colors rgb
  • Python turtle colors fill
  • Python turtle color chart
  • Python turtle color codes
  • Python turtle color random
  • Python turtle color gradient
  • Python turtle color change

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Читайте также:  Python concurrent futures threadpoolexecutor

Источник

turtle — Turtle graphics¶

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967.

Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an import turtle , give it the command turtle.forward(15) , and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. Give it the command turtle.right(25) , and it rotates in-place 25 degrees clockwise.

Turtle can draw intricate shapes using programs that repeat simple moves.

../_images/turtle-star.png

from turtle import * color('red', 'yellow') begin_fill() while True: forward(200) left(170) if abs(pos())  1: break end_fill() done() 

By combining together these and similar commands, intricate shapes and pictures can easily be drawn.

The turtle module is an extended reimplementation of the same-named module from the Python standard distribution up to version Python 2.5.

It tries to keep the merits of the old turtle module and to be (nearly) 100% compatible with it. This means in the first place to enable the learning programmer to use all the commands, classes and methods interactively when using the module from within IDLE run with the -n switch.

The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses tkinter for the underlying graphics, it needs a version of Python installed with Tk support.

The object-oriented interface uses essentially two+two classes:

  1. The TurtleScreen class defines graphics windows as a playground for the drawing turtles. Its constructor needs a tkinter.Canvas or a ScrolledCanvas as argument. It should be used when turtle is used as part of some application. The function Screen() returns a singleton object of a TurtleScreen subclass. This function should be used when turtle is used as a standalone tool for doing graphics. As a singleton object, inheriting from its class is not possible. All methods of TurtleScreen/Screen also exist as functions, i.e. as part of the procedure-oriented interface.
  2. RawTurtle (alias: RawPen ) defines Turtle objects which draw on a TurtleScreen . Its constructor needs a Canvas, ScrolledCanvas or TurtleScreen as argument, so the RawTurtle objects know where to draw. Derived from RawTurtle is the subclass Turtle (alias: Pen ), which draws on “the” Screen instance which is automatically created, if not already present. All methods of RawTurtle/Turtle also exist as functions, i.e. part of the procedure-oriented interface.

The procedural interface provides functions which are derived from the methods of the classes Screen and Turtle . They have the same names as the corresponding methods. A screen object is automatically created whenever a function derived from a Screen method is called. An (unnamed) turtle object is automatically created whenever any of the functions derived from a Turtle method is called.

To use multiple turtles on a screen one has to use the object-oriented interface.

In the following documentation the argument list for functions is given. Methods, of course, have the additional first argument self which is omitted here.

Источник

Оцените статью