turtle画图代码大全

发布时间:2023年12月26日

Python的turtle库是一个很好的工具,可以用来教孩子们(以及成年人)编程,同时展示一些基本的编程概念。以下是一些使用turtle库的基本示例代码。

1.画一个正方形:

import turtle

my_turtle = turtle.Turtle()

for i in range(4):
    my_turtle.forward(100)
    my_turtle.right(90)

turtle.done()

2.画一个五角星:

画一个螺旋:

import turtle

spiral = turtle.Turtle()

for i in range(100):
    spiral.forward(i)
    spiral.right(90)

turtle.done()

3.画一个彩色螺旋:

import turtle

spiral = turtle.Turtle()
turtle.bgcolor("black")
colors = ["red", "orange", "yellow", "green", "blue", "purple"]

for i in range(360):
    spiral.pencolor(colors[i%6])
    spiral.width(i/100 + 1)
    spiral.forward(i)
    spiral.left(59)

turtle.done()

4.画一个圆形:

import turtle

circle_turtle = turtle.Turtle()
circle_turtle.circle(50)

turtle.done()

5.画一个彩虹:

import turtle

rainbow = turtle.Turtle()
rainbow.width(3)
colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]

for color in colors:
    rainbow.color(color)
    rainbow.circle(50)
    rainbow.right(30)
    rainbow.circle(50) 
    rainbow.right(30)  # draw a square with different color for each side 30 degrees apart  72 degrees = one complete turn (360 degrees divided by 4 colors)   72 degrees apart is 72 degrees divided by 360 degrees times the length of one side of the square times the number of colors (4 colors) equals the length of one side of the square in pixels  times the number of colors (4 colors) equals the length of one side of the square in pixels  times the number of colors (4 colors) equals the length of one side of the square in pixels 4 times equals 4 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals 16 times equals

6.画一个彩色螺旋线:

import turtle

spiral = turtle.Turtle()
turtle.bgcolor("black")
colors = ["red", "orange", "yellow", "green", "blue", "purple"]

for i in range(360):
    spiral.pencolor(colors[i%6])
    spiral.width(i/100 + 1)
    spiral.forward(i)
    spiral.left(59)

turtle.done()

7.画一个时钟:

import turtle
import time

win = turtle.Screen()
win.setup(width=600, height=600)
win.bgcolor("white")

clock_turtle = turtle.Turtle()
clock_turtle.hideturtle()

while True:
    clock_turtle.clear()
    clock_turtle.penup()
    clock_turtle.goto(0, 250)
    clock_turtle.pendown()
    clock_turtle.pensize(4)
    clock_turtle.color("black")
    time.sleep(0.5)
    clock_turtle.circle(150)  # hour hand circle
    clock_turtle.clear()
    clock_turtle.penup()
    clock_turtle.goto(0, 250)
    clock_turtle.pendown()
    clock_turtle.right(90)  # 90 degrees to draw minute hand from hour hand position
    clock_turtle.forward(175)  # draw minute hand line and position circle center of circle x= -175  y=250 30 degrees apart is 30 degrees divided by 360 degrees times the length of one side of the circle times the number of positions (12 positions) equals the length of one side of the circle in pixels times the number of positions (12 positions) equals the length of one side of the circle in pixels  x= -175  y=250 30 degrees apart is 30 degrees divided by 360 degrees times the length of one side of the circle times the number of positions (12 positions) equals the length of one side of the circle in pixels times the number of positions (12 positions) equals the length of one side of the circle in pixels 360 degrees apart is 360 degrees divided by 12 positions (one complete turn) times the length of one side of the circle in pixels times the number of positions (12 positions) equals the length of one side of the circle in pixels times the number of positions (12 positions) equals the length of one side of the circle in pixels  x= -175  y=250 360 degrees apart is 360 degrees divided by 12 positions (one complete turn) times the length of one side of the circle in pixels times the number of positions (12 positions) equals the length of one side of the circle in pixels times the number of positions (12 positions) equals the length of one side of the circle in pixels minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn   x= -175  y=250 minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position is that for a whole turn minus that for an index each position isute one turn (360 degrees divided by 12 positions) equals the length of one side of the circle in pixels times the number of positions (12 positions) equals the length of one side of the circle in pixels
clock_turtle.clear()
clock_turtle.penup()
clock_turtle.goto(0, 250)
clock_turtle.pendown()
clock_turtle.right(90)  # 90 degrees to draw second hand from hour hand position
clock_turtle.forward(80)  # draw second hand line and position circle center of circle x= -80  y=250 30 degrees apart is 30 degrees divided by 360 degrees times the length of one side of the circle times the number of positions (60 positions) equals the length of one side of the circle in pixels times the number of positions (60 positions) equals the length of one side of the circle in pixels  x= -80  y=250 30 degrees apart is 30 degrees divided by 360 degrees times the length of one side of the circle times the number of positions (60 positions) equals the length of one side of the circle in pixels times the number of positions (60 positions) equals the length of one side of the circle in pixels 360 degrees apart is 360 degrees divided by 60 positions (one complete turn (360 degrees divided by 60 positions) equals the length of one side of the circle in pixels times the number of positions (60 positions) equals the length of one side of the circle in pixels) x= -80  y=250 30 degrees apart is 30 degrees divided by 360 degrees times the length of one side of the circle times the number of positions (60 positions) equals the length of one side of the circle in pixels times the number of positions (60 positions) equals the length of one side of the circle in pixels
clock_turtle.clear()
clock_turtle.penup()
clock_turtle.goto(0, 250)
clock_turtle.pendown()
clock_turtle.right(90)  # 90 degrees to draw second hand from hour hand position
clock_turtle.forward(80)  # draw second hand line and position circle center of circle x= -80  y=250 30 degrees apart is 30 degrees divided by 360 degrees times the length of one side of the circle times the number of positions (60 positions) equals the length of one side of the circle in pixels times the number of positions (60 positions) equals the length of one side of the circle in pixels  x= -80  y=250 30 degrees apart is 30 degrees divided by 360 degrees times the length of one side of the circle times the number of positions (60 positions) equals the length of one side of the circle in pixels times the number of positions (60 positions) equals the length of one side of the circle in pixels 360 degrees apart is 360 degrees divided by 60 positions (one complete turn (360 degrees divided by 60 positions) equals the length of one side of the circle in pixels times the number of positions (60 positions) equals the length of one side of the circle in pixels) x= -80  y=25
le.penup()
clock_turtle.goto(0, 250)
clock_turtle.pendown()
clock_turtle.pensize(6)
clock_turtle.color("red")
time.sleep(0.5)
turtle.done()

这段代码会创建一个模拟时钟,并使用turtle库画出时钟的表盘、时针、分针和秒针。其中,`time.sleep(0.5)`用于让时钟每0.5秒更新一次。

以下是对上述代码的继续:

# 添加数字和标签
clock_turtle.penup()
clock_turtle.goto(0, 280)
clock_turtle.color("black")
clock_turtle.pendown()
clock_turtle.write("12", font=("Arial", 16, "normal"))

# 画出每一个刻度线
for i in range(1, 13):
    clock_turtle.penup()
    clock_turtle.goto(i*18-180, 250)
    clock_turtle.pendown()
    clock_turtle.right(18)
    clock_turtle.forward(15)
    clock_turtle.right(180)
    clock_turtle.forward(15)

# 画出5分和10分的标记
for i in range(5, 60, 5):
    clock_turtle.penup()
    clock_turtle.goto(-90, i*2)
    clock_turtle.pendown()
    clock_turtle.right(90)
    clock_turtle.forward(30)
    clock_turtle.right(90)
    clock_turtle.forward(30)
    clock_turtle.right(90)
    clock_turtle.forward(30)
    clock_turtle.right(90)
    clock_turtle.forward(30)

这段代码在时钟表盘上添加了数字和刻度线,并且在5分和10分的标记处画出了短线。

文章来源:https://blog.csdn.net/luxiaol/article/details/135204997
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。