
Python自学 - 云代码空间
—— python自学——怎么学习python,自学python要多久?python+人工智能+人工智能+人工智能+大数据分析
网上搜的国旗设计图:

假设国旗中心点为坐标系原点(0,0),每个格子长宽为L):
turtle库:
turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,根据一组函数指令的控制,在平面坐标系中移动,从而在它爬行的路径上绘制了图形。
turtle库常用方法:
# 绘制长方形
def rectangle(center, length_x, length_y, penc, fillc):
turtle.pensize(1)
turtle.fillcolor(fillc)
turtle.pencolor(penc)
turtle.up()
pos = (center[0] - length_x / 2, center[1] + length_y / 2)
turtle.goto(pos)
turtle.begin_fill()
turtle.down()
turtle.seth(0)
turtle.fd(length_x)
turtle.seth(-90)
turtle.fd(length_y)
turtle.seth(180)
turtle.fd(length_x)
turtle.seth(90)
turtle.fd(length_y)
turtle.end_fill()
# 绘制星星
def star(center, angle, length, penc, fillc):
turtle.pensize(1)
turtle.fillcolor(fillc)
turtle.pencolor(penc)
L = length * math.sin(36 * math.pi / 180) / math.sin(54 * math.pi / 180)
turtle.up()
turtle.goto(center)
turtle.seth(90 + angle)
turtle.fd(length)
turtle.seth(180 + 72 + angle)
turtle.down()
turtle.begin_fill()
for _ in range(5):
turtle.fd(L)
turtle.right(72)
turtle.fd(L)
turtle.left(144)
turtle.end_fill()
上面画五角星方法是沿着边重复画,还有另外一种方法,类似平时我们手绘的路径,最后填充
import turtle
import time
turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("yellow")
turtle.begin_fill()
for _ in range(5):
turtle.forward(200)
turtle.right(144)
turtle.end_fill()
time.sleep(2)
turtle.penup()
turtle.goto(-150, -120)
turtle.color("violet")
time.sleep(2)
W = 1200 H = 800 dW = W / 30 dH = H / 20 C0 = (-dW * 10, dH * 5) A0 = 0 #旋转角度,国旗上的4颗小五角星各有一个尖对着大五角星 L0 = dW * 3 #画大五角星 star(C0, A0, L0, 'yellow', 'yellow')
先画大五角星,然后变更坐标点,调整A0旋转角度分别画4颗小五角星。
# coding: utf-8
import turtle
import time
import math
turtle.setup(width=1.0, height=1.0, startx=None, starty=None)
turtle.ht()
# turtle.tracer(0, 0)
turtle.bgcolor("white")
turtle.speed(10)
turtle.pensize(1)
# 绘制星星
def star(center, angle, length, penc, fillc):
turtle.pensize(1)
turtle.fillcolor(fillc)
turtle.pencolor(penc)
L = length * math.sin(36 * math.pi / 180) / math.sin(54 * math.pi / 180)
turtle.up()
turtle.goto(center)
turtle.seth(90 + angle)
turtle.fd(length)
turtle.seth(180 + 72 + angle)
turtle.down()
turtle.begin_fill()
for _ in range(5):
turtle.fd(L)
turtle.right(72)
turtle.fd(L)
turtle.left(144)
turtle.end_fill()
# 绘制长方形
def rectangle(center, length_x, length_y, penc, fillc):
turtle.pensize(1)
turtle.fillcolor(fillc)
turtle.pencolor(penc)
turtle.up()
pos = (center[0] - length_x / 2, center[1] + length_y / 2)
turtle.goto(pos)
turtle.begin_fill()
turtle.down()
turtle.seth(0)
turtle.fd(length_x)
turtle.seth(-90)
turtle.fd(length_y)
turtle.seth(180)
turtle.fd(length_x)
turtle.seth(90)
turtle.fd(length_y)
turtle.end_fill()
W = 1200
H = 800
dW = W / 30
dH = H / 20
# time.sleep(6)
rectangle((0, 0), W, H, 'red', 'red')
C0 = (-dW * 10, dH * 5)
A0 = 0
L0 = dW * 3
star(C0, A0, L0, 'yellow', 'yellow')
C1 = (-dW * 5, dH * 8)
A1 = 90 + math.atan(3 / 5) * 180 / math.pi
L1 = dW
star(C1, A1, L1, 'yellow', 'yellow')
C2 = (-dW * 3, dH * 6)
A2 = 90 + math.atan(1 / 7) * 180 / math.pi
L2 = dW
star(C2, A2, L2, 'yellow', 'yellow')
C3 = (-dW * 3, dH * 3)
A3 = 90 - math.atan(2 / 7) * 180 / math.pi
L3 = dW
star(C3, A3, L3, 'yellow', 'yellow')
C4 = (-dW * 5, dH)
A4 = 90 - math.atan(4 / 5) * 180 / math.pi
L4 = dW
star(C4, A4, L4, 'yellow', 'yellow')
turtle.hideturtle()
turtle.done()
运行效果
