Cara menggunakan turtle colors python

Seorang programmer pemula biasanya lebih suka mempelajari hal-hal yang berbau grafik di awal-awal dia belajar bahasa pemrograman, termasuk saya. Ada banyak cara bagaimana menuliskan program grafik di Python, salah satunya menggunakan modul turtle. Yuk…saatnya kita bersenang-senang dengan si kura-kura (turtle) Python.

Cara menggunakan turtle colors python

{ 1 } Aktifkan Python di mode prompt >>>, bisa lewat IDLE atau Command Line. Ketikkan import modul yang akan kita gunakan, yaitu import turtle.

>>> import turtle # mengimpor modul turtle

{ 2 } Ketikkan perintah untuk menampilkan lokasi terkini dan arah dari si kura-kura.

>>> turtle.showturtle()

Cara menggunakan turtle colors python

Pemrograman grafik menggunakan modul turtle, mirip ketika kita menggambar dengan pena. Mata panah sebagai penanda posisi dan arah dari pena kita. turtlemempunyai posisi awal di tengah-tengah layar.

{ 3 } Ketikkan perintah berikut untuk menggambar sebuah teks.

>>> turtle.write("Selamat Datang di Klinik Python")

Cara menggunakan turtle colors python

{ 4 } Ketikkan perintah berikut untuk menggerakkan mata panah maju sejauh 100 piksel dalam bentuk garis.

>>> turtle.forward(100)

Cara menggunakan turtle colors python

{ 5 } Ketikkan perintah berikut untuk membelokkan arah ke kanan 90 derajat, mengubah warna garis menjadi merah, dan membuat garis sepanjang 50 pixel arah lurus ke depan.

>>> turtle.right(90)
>>> turtle.color('red')
>>> turtle.forward(50)

Cara menggunakan turtle colors python

{ 6 } Sekarang, kita buat perintah agar mata panah berbelok ke kanan 90 derajat, mengubahnya menjadi warna hijau, dan bergerak maju membuat garis sepanjang 100 piksel.

prompt >>>0

Cara menggunakan turtle colors python

{ 7 } Dan akhirnya, kita buat perintah berputar ke kanan 45 derajat, dan membuat garis lurus sepanjang 80 piksel.

prompt >>>1

Cara menggunakan turtle colors python

{ 8 } Kita tutup jendela Turtle dan keluar dari Python.

Mudah bukan? So…Tunggu artikel selanjutnya tentang Turtle Python…tentunya di website kesayangan kita ini http://www.KlinikPython.wordpress.com

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

Table of Contents

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.

Code:

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

Output:

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.

Cara menggunakan turtle colors python
Python turtle colors Output

Read: How to draw a shape in python using Turtle

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.

Code:

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)

Output:

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

Cara menggunakan turtle colors python
Python turtle RGB color Output

Read: Draw colored filled shapes using Python Turtle

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

Code:

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

Output:

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

Cara menggunakan turtle colors python
Python turtle fill color Output

Read: How to Create a Snake game in Python using Turtle

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.

Code:

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

Output:

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

Cara menggunakan turtle colors python
Python turtle color chart Output

Read: Python Turtle Commands 

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.

Code:

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)

Output:

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.

Cara menggunakan turtle colors python
Python turtle color codes Output

Read: How to attach an image in Turtle

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.

Code:

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

Output:

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

Cara menggunakan turtle colors python
Python turtle color random Output

Read: Matplotlib change background color

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.

Code:

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

Output:

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

Cara menggunakan turtle colors python
Python turtle color gradient Output

Read: Python Tkinter Colors

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.

Code:

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)

Output:

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.

Cara menggunakan turtle colors python
Python turtle color change Output

You may like the following Python tutorials:

  • Python Turtle Speed With Examples
  • Python Turtle Circle

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

Cara menggunakan turtle colors python

Bijay Kumar

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.