脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 如何用Python画一些简单形状你知道吗

如何用Python画一些简单形状你知道吗

2021-12-21 00:08我帅的是不是无可救药 Python

这篇文章主要介绍了用Python作图的一个简单实例,通过turtle模块实现作图,具有一定参考价值,需要的朋友可以了解下希望能给你带来帮助

进入主题

1.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import turtle as t
import math
t.pensize(3)
t.tracer(10)
t.hideturtle()
start_x = -200
for y in range(-150,150,5):
    t.penup()
    t.goto(start_x,y)
    t.pendown()
    for x in range(-200,200,1):
        if ((y-50*(math.cos(0.05*x)) <= 80) and
            (y-50*(math.cos(0.05*x)) >= 60)):
            t.pencolor('yellow')
        elif ((y-50*(math.cos(0.05*x)) <= 40) and
            (y-50*(math.cos(0.05*x)) >= -20)):
            t.pencolor('blue')
        elif ((y-50*(math.cos(0.05*x)) <= -20) and
            (y-50*(math.cos(0.05*x)) >= -80)):
            t.pencolor('red')
        elif ((y-50*(math.cos(0.05*x)) <= -60) and
            (y-50*(math.cos(0.05)) <= -80)):
            t.pencolor('green')
        else:
            t.pencolor('black')
        t.setx(x)
t.update()
t.done()

如何用Python画一些简单形状你知道吗

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import turtle as t
t.speed(0)
t.tracer(20)
t.hideturtle()
t.colormode(255)
angle = 90
for x in range(255,0,-5):
    for n in range(360//angle):
        t.pencolor((x,255,255))
        t.fillcolor((25,x,255))
        t.begin_fill()
        for i in range(2):
            t.forward(x)
            t.right(angle)
            t.forward(x)
            t.right(180-angle)
        t.end_fill()
        t.right(angle)
t.update()
t.done()

如何用Python画一些简单形状你知道吗

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import turtle as t
t.speed(0)
t.tracer(20)
t.colormode(255)
angle = 60
angle2 = 3
for x in range(255,0,-5):
    for n in range(360//angle):
        t.pencolor((x,255,255))
        t.fillcolor((255,x,255))
        t.begin_fill()
        for i in range(2):
            t.forward(x)
            t.right(angle)
            t.forward(x)
            t.right(180-angle)
        t.end_fill()
        t.right(angle)
    t.right(angle2)
t.update()
t.done()

如何用Python画一些简单形状你知道吗

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from turtle import *      
colormode(255)
tracer(5)
a1=39
a2=1
for x in range(255,0,-5):
    pencolor(x,255,255)
    fillcolor(255,x,255)
    for y in range(360//a1):
        begin_fill()
        for z in range(2):
            fd(x)
            rt(a1)
            fd(x)
            rt(180-a1)
        end_fill()
        rt(a1)
    rt(a2)
update()
ht()
done()

如何用Python画一些简单形状你知道吗

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import turtle as t
t.speed(0)
t.hideturtle()
t.penup()
t.setx(-200)
t.pendown()
r = 20
i = 6
for x in range(10):
    if x % 2 == 0:
        t.fillcolor("skyblue")
        t.begin_fill()
        t.circle(r)
        t.end_fill()
        add = 0
    else:
        t.fillcolor("green")
        t.begin_fill()
        for n in range(4):
            t.forward(r*2)
            t.left(90)
        t.end_fill()
        add = r*2
    t.penup()
    t.forward(r+i+add)
    t.pendown()
t.done()

如何用Python画一些简单形状你知道吗

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import turtle as t
t.pensize(5)
t.tracer(10)
t.hideturtle()
start_x = -200
for y in range(-150,150,20):
    t.penup()
    t.goto(start_x,y)
    t.pendown()
    for x in range(-200,200,1):
        if ((x < 100 and x > 0) and
            (y < 80 and y > 0)):
            t.pencolor('yellow')
        elif ((x < 100 and x > 0) and
            (y < 0 and y > -80)):
            t.pencolor('blue')
        elif ((x < 0 and x > -100) and
            (y < 80 and y > 0)):
            t.pencolor('red')
        elif ((x < 0 and x > -100) and
            (y < 0 and y > -80)):
            t.pencolor('orange')
        else:
            t.pencolor('green')
        t.setx(x)
t.update()
t.done()

如何用Python画一些简单形状你知道吗

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import turtle as t
t.pensize(5)
t.tracer(10)
t.hideturtle()
start_x = -200
for y in range(-150,150,20):
    t.penup()
    t.goto(start_x,y)
    t.pendown()
    for x in range(-200,200,1):
        if ((y-x <= 40) and
            (y-x >= -40)):
            t.pencolor('yellow')
        elif ((y+x <= 40) and
            (y+x >= -40)):
            t.pencolor('blue')
        else:
            t.pencolor('green')
        t.setx(x)
t.update()
t.done()

如何用Python画一些简单形状你知道吗

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import turtle as t
t.speed(0)
t.tracer(20)
t.hideturtle()
t.colormode(255)
angle = 60
for x in range(255,0,-5):
    for n in range(360//angle):
        t.pencolor((x,255,255))
        t.fillcolor((255,x,255))
        t.begin_fill()
        for i in range(2):
            t.forward(x)
            t.right(angle)
            t.forward(x)
            t.right(180-angle)
        t.end_fill()
        t.right(angle)
t.update()
t.done()

如何用Python画一些简单形状你知道吗

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!

原文链接:https://blog.csdn.net/m0_60636930/article/details/119729343

延伸 · 阅读

精彩推荐