python3 turtle模块画正方形、矩形、正方体、五角星、奥运五环

  • 环境:python3
  • 模块:turtle
  • 编辑工具:pycharm

引入turtle模块

import turtle

正方形

turtle.title('正方形')
turtle.begin_fill()
turtle.fillcolor('#069')
for x in range(4):
    turtle.forward(100)
    turtle.right(90)
turtle.end_fill()
turtle.hideturtle()

turtle.done()

效果图:
python3 turtle模块画正方形、矩形、正方体、五角星、奥运五环_第1张图片

矩形

turtle.title('矩形')
turtle.begin_fill()
turtle.fillcolor('#069')
for x in range(1, 5):
    if x % 2 == 1:
        n = 100
    else:
        n = 50
    turtle.forward(n)
    turtle.right(90)
turtle.end_fill()
turtle.hideturtle()

turtle.done()

效果图:
python3 turtle模块画正方形、矩形、正方体、五角星、奥运五环_第2张图片

正方体

turtle.title('正方体')
n = 100  # 边长

# 正面
turtle.begin_fill()
turtle.fillcolor('#069')
for x in range(4):
    turtle.forward(n)
    turtle.right(90)
turtle.end_fill()
# 上面
turtle.begin_fill()
turtle.fillcolor('#ccc')
turtle.left(55)
turtle.forward(n * 0.6)
turtle.right(55)
turtle.forward(n)
turtle.right(125)
turtle.forward(n * 0.6)
turtle.end_fill()
# 右侧
turtle.up()
turtle.goto(n, -n)
turtle.down()
turtle.left(180)
turtle.forward(n*0.6)
turtle.left(35)
turtle.forward(n)

turtle.hideturtle()
turtle.done()

效果图:
python3 turtle模块画正方形、矩形、正方体、五角星、奥运五环_第3张图片

五角星

turtle.title('五角星')
deg = 36
n = 100

turtle.pencolor('#069')
turtle.begin_fill()
turtle.fillcolor('#069')
turtle.left(deg)
for i in range(5):
    turtle.forward(n)
    turtle.left(180 - deg)
turtle.end_fill()

turtle.hideturtle()
turtle.done()

效果图:
python3 turtle模块画正方形、矩形、正方体、五角星、奥运五环_第4张图片

奥运五环

turtle.title('奥运五环')
x = -200
y = 50
r = 50

turtle.speed(5)
# 第一个圈,蓝色
turtle.up()
turtle.goto(x, y)
turtle.down()
turtle.pensize(5)
turtle.pencolor('blue')
turtle.circle(r)

# 第二个圈,黑色
turtle.up()
turtle.goto(x + 2.5 * r, y)
turtle.down()
turtle.pensize(5)
turtle.pencolor('black')
turtle.circle(r)

# 第三个圈,红色
turtle.up()
turtle.goto(x + (2.5 * r) * 2, y)
turtle.down()
turtle.pensize(5)
turtle.pencolor('red')
turtle.circle(r)

# 第四个圈,黄色
turtle.up()
turtle.goto(x + (2.5 * r) * 0.5, y -  r)
turtle.down()
turtle.pensize(5)
turtle.pencolor('yellow')
turtle.circle(r)

# 第五个圈,绿色
turtle.up()
turtle.goto(x + (2.5 * r)*1.5, y - r)
turtle.down()
turtle.pensize(5)
turtle.pencolor('green')
turtle.circle(r)

turtle.hideturtle()
turtle.done()

效果图:
python3 turtle模块画正方形、矩形、正方体、五角星、奥运五环_第5张图片

围棋棋盘

turtle.title('围棋棋盘')
g = 20
num_x = 18
num_y = 18
r = 4
j = 3


turtle.speed(10)
# 绘制横线
ruler_y = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一',  '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九']
for x in range(num_x + 1):
    if (x == 0) or (x == num_x):
        turtle.pensize(3)
    else:
        turtle.pensize(1)
    #  画线
    turtle.up()
    turtle.goto(-num_y / 2 * g, num_x / 2 * g - g * x)
    turtle.down()
    turtle.forward(num_y * g)
    # 写字
    turtle.up()
    turtle.goto(-num_y / 2 * g - g, num_x / 2 * g - g * x - g / 2)
    turtle.write(ruler_y[x], align="center", font=("Courier", 14, "bold"))
    turtle.down()

# 绘制竖线
turtle.right(90)
for x in range(num_y + 1):
    if (x == 0) or (x == num_y):
        turtle.pensize(3)
    else:
        turtle.pensize(1)
    # 画竖线
    turtle.up()
    turtle.goto(-num_y / 2 * g + g * x, num_x / 2 * g)
    turtle.down()
    turtle.forward(num_x * g)

    # 写字
    turtle.up()
    turtle.goto(-num_y / 2 * g + g * x, num_x / 2 * g + g / 2)
    turtle.write(x + 1, align="center", font=("Courier", 14, "bold"))
    turtle.down()

#  绘画棋盘圆点
turtle.pensize(1)
for x in range(3):
    for y in range(3):
        turtle.up()
        turtle.begin_fill()
        turtle.fillcolor('black')
        turtle.goto(-num_y / 2 * g + (j * g) * 2 * y + (j * g) - r, num_x / 2 * g - (j * g) - (j * g) * 2 * x)
        turtle.down()
        turtle.circle(r)
        turtle.end_fill()

turtle.hideturtle()
turtle.done()

效果图:
python3 turtle模块画正方形、矩形、正方体、五角星、奥运五环_第6张图片

你可能感兴趣的:(Python3.x,turtle)