Use simple and beautiful Python Turtle graphics design

Take a look at these beautiful pictures in this article. It is this sort of thing makes me fall in love with the turtle. Good programmers will think turtle is "backward primitive," but I beg to differ.

turtle Another great use is to teach kids basic programming. Young people prefer a visually appealing design, rather than a boring text.

Now let's get started.

Finally, download all the source code to see

Use simple and beautiful Python Turtle graphics design

Step 1: Python

In this tutorial, I will be using Python 3.6.8.

Even if you do not know Python, the next must also understand, we must try to learn the language. In my opinion, it is a very simple and powerful language.

Turtle is a built-in module, simply put, the module is easy to call a collection of programming functions, namely a specific set of steps to follow to get the computer results of the command.

Step 2: The first design: Code and Description

# Python behind the content is invisible.

from turtle import * # import modules turtle,

* On behalf of all

speed (0) # 0 is set to the drawing speed, which is the fastest

Code:

# python看不到#之后的内容。 这些是注释。 :)
from turtle import * # 导入模块turtle,
                    #* 代表所有,这使事情更容易

speed(0) # 将绘制速度设置为0,这是最快的
pencolor('red') #  将笔/线的颜色设置为红色
bgcolor('black') # 将背景/画布的颜色设置为黑色

x = 0 # 创建一个值为0的变量x
up() # 抬起笔,所以没有画线

#nota fd()表示向前移动,bk()表示向后移动
# rt() 或 lt()表示向右倾斜一定角度

rt(45) 
fd(90) 
rt(135)

down() # 放下笔,以便乌龟可以画画
while x < 120: # 当x的值小于120时,
                 #不断地这样做:
    fd(200)    
    rt(61)
    fd(200)
    rt(61)
     fd(200)
    rt(61)
    fd(200)
    rt(61)
    fd(200)
     rt(61)
    fd(200)
    rt(61)

    rt(11.1111))
    x = x+1 # adds 1 to the value of x,
            #  所以每次循环后都接近120

exitonclick() # 当您单击时,乌龟退出。

#that's it! Try a custom script!

The effect is as follows:

Use simple and beautiful Python Turtle graphics design

Code is very simple speed (), pencolor (), fd (), rt (), lt () are functions like. They are basically the Python command to follow.

We might as well try to use simple logic to create a more beautiful design.

Step 3: Secondary Design: cool spiral pattern

What should I say? The beauty of this code that generates a unique color combination each time it runs.

code show as below:

from turtle import *

from random import randint # 从random  模块导入函数randint
#turtle是一个模块,请提前阅读以使用

speed(0)

bgcolor('black')

x = 1

while x < 400:

    r = randint(0,255) # 使变量r,g,b为整型数,
    g = randint(0,255)  # 在0到255之间。它是随机的
    b = randint(0,255) # 每次循环运行都会改变

    colormode(255) # 这个关系不大


    pencolor(r,g,b))# 将笔的颜色更改为RGB坐标
                    #  由变量r,g,b每次改变获得

    fd(50 + x)
    rt(90.911)


    x = x+1

exitonclick()

# Again, try to customize :)

Renderings:

Use simple and beautiful Python Turtle graphics design

Step 4: Summary

At this point, beginners may feel proud, but you do not touch the surface. People use Python to make things as complicated as the fractal tree.

If you like the turtle, in future we will share more documentation about the turtle. There are many ways, such as filling and round, do experiments to find the fun.

I hope this can inspire some people, let them continue to learn Python. In my opinion, it is the simplest but most powerful programming language.

To further deepen your impression, take a look at the rainbow triangle codes and renderings.

import turtle
turtle.setup(width=600,  height=500)
turtle.reset()
turtle.hideturtle()
turtle.speed(0)

turtle.bgcolor('black')

c = 0
x = 0

colors = [
#reddish colors
(1.00, 0.00, 0.00),(1.00, 0.03, 0.00),(1.00,  0.05, 0.00),(1.00, 0.07, 0.00),(1.00, 0.10, 0.00),(1.00, 0.12, 0.00),(1.00,  0.15, 0.00),(1.00, 0.17, 0.00),(1.00, 0.20, 0.00),(1.00, 0.23, 0.00),(1.00,  0.25, 0.00),(1.00, 0.28, 0.00),(1.00, 0.30, 0.00),(1.00, 0.33, 0.00),(1.00,  0.35, 0.00),(1.00, 0.38, 0.00),(1.00, 0.40, 0.00),(1.00, 0.42, 0.00),(1.00,  0.45, 0.00),(1.00, 0.47, 0.00),
#orangey colors
(1.00, 0.50, 0.00),(1.00,  0.53, 0.00),(1.00, 0.55, 0.00),(1.00, 0.57, 0.00),(1.00, 0.60, 0.00),(1.00,  0.62, 0.00),(1.00, 0.65, 0.00),(1.00, 0.68, 0.00),(1.00, 0.70, 0.00),(1.00,  0.72, 0.00),(1.00, 0.75, 0.00),(1.00, 0.78, 0.00),(1.00, 0.80, 0.00),(1.00,  0.82, 0.00),(1.00, 0.85, 0.00),(1.00, 0.88, 0.00),(1.00, 0.90, 0.00),(1.00,  0.93, 0.00),(1.00, 0.95, 0.00),(1.00, 0.97, 0.00),
#yellowy colors
(1.00,  1.00, 0.00),(0.95, 1.00, 0.00),(0.90, 1.00, 0.00),(0.85, 1.00, 0.00),(0.80,  1.00, 0.00),(0.75, 1.00, 0.00),(0.70, 1.00, 0.00),(0.65, 1.00, 0.00),(0.60,  1.00, 0.00),(0.55, 1.00, 0.00),(0.50, 1.00, 0.00),(0.45, 1.00, 0.00),(0.40,  1.00, 0.00),(0.35, 1.00, 0.00),(0.30, 1.00, 0.00),(0.25, 1.00, 0.00),(0.20,  1.00, 0.00),(0.15, 1.00, 0.00),(0.10, 1.00, 0.00),(0.05, 1.00,  0.00),
#greenish colors
(0.00, 1.00, 0.00),(0.00, 0.95, 0.05),(0.00, 0.90,  0.10),(0.00, 0.85, 0.15),(0.00, 0.80, 0.20),(0.00, 0.75, 0.25),(0.00, 0.70,  0.30),(0.00, 0.65, 0.35),(0.00, 0.60, 0.40),(0.00, 0.55, 0.45),(0.00, 0.50,  0.50),(0.00, 0.45, 0.55),(0.00, 0.40, 0.60),(0.00, 0.35, 0.65),(0.00, 0.30,  0.70),(0.00, 0.25, 0.75),(0.00, 0.20, 0.80),(0.00, 0.15, 0.85),(0.00, 0.10,  0.90),(0.00, 0.05, 0.95),
#blueish colors
(0.00, 0.00, 1.00),(0.05, 0.00,  1.00),(0.10, 0.00, 1.00),(0.15, 0.00, 1.00),(0.20, 0.00, 1.00),(0.25, 0.00,  1.00),(0.30, 0.00, 1.00),(0.35, 0.00, 1.00),(0.40, 0.00, 1.00),(0.45, 0.00,  1.00),(0.50, 0.00, 1.00),(0.55, 0.00, 1.00),(0.60, 0.00, 1.00),(0.65, 0.00,  1.00),(0.70, 0.00, 1.00),(0.75, 0.00, 1.00),(0.80, 0.00, 1.00),(0.85, 0.00,  1.00),(0.90, 0.00, 1.00),(0.95, 0.00, 1.00)
]

while x < 1000:
    idx = int(c)
    color = colors[idx]
     turtle.color(color)
    turtle.forward()
    turtle.right(98)
    x = x  + 1
    c = c + 0.1

turtle.exitonclick()

Renderings:

Use simple and beautiful Python Turtle graphics design

Guess you like

Origin www.linuxidc.com/Linux/2020-01/162144.htm