Lanqiao Cup Python Junior Group Test Question: Turtle Drawing 2

Lanqiao Cup Python Junior Group Test Question: Turtle Drawing 2

1. Draw 8 inscribed regular pentagons
Problem description
Insert image description here
Problem-solving ideas
(1 ) There are n=8 regular pentagons in total, so there are 8 cycles, and each rotation angle increases ang=360/n
(2) To draw an inscribed regular pentagon, use circle(raduis =50, steps=5)
(3) Set the brush color using pencolor('blue') and hide the brush hideturtle()

import turtle

n = 8
ang = 360 / n
h = 0
t = turtle.Turtle()
t.hideturtle()
t.pencolor('blue')
t.seth(90)
for i in range(n):
    # 每次递增1个ang角度
    h = ang * i
    # 向左旋转
    t.left(h)
    # 画内接正五边形
    t.circle(radius=50, steps=5)
turtle.done()

2. Draw a square inscribed in a circle
Problem description
Insert image description here
Problem-solving ideas
(1) Use to draw a square goto is the simplest, set the coordinates 100 each time
(2) Use circle(raduis=50) to draw a circle with a diameter of exactly 100, forming an inscribed circle
(3) Set the brush color using pencolor('red'), fill color fillcolor('yellow'), hide the brush hideturtle()
(4) Use begin_fill() to start filling, and end_fill() to end filling.

import turtle

t=turtle.Turtle()
t.hideturtle()
# 画正方形
t.pencolor('red')
t.goto(0,-100)
t.goto(100,-100)
t.goto(100,0)
t.goto(0,0)

# 画圆形
t.penup()
t.goto(50,-100)
t.pendown()
t.fillcolor('yellow')
t.begin_fill()
t.circle(radius=50)
t.end_fill()
turtle.done()

3. Draw triangles and inscribed regular hexagons
Problem description
Insert image description here
Problem-solving ideas
(1 ) In order to simplify the drawing steps, the inscribed regular hexagon is first drawn using circle to inscribe the regular hexagon. The regular hexagon is cut from the center of the circle and is an equilateral triangle, so the radius = side length 150
(2) Move Move the center of the circle to the required position goto(150,0)
(3) Draw an inscribed regular hexagon and fill it with fillcolor('green') using t.circle(radius=150, steps=6) )
(4) Draw 6 external triangles in a loop. This requires drawing the path on the scratch pad in advance
(5) Hide the brush hideturtle()
(6) Use begin_fill() to start filling, and end_fill() to end filling.

import turtle

t=turtle.Turtle()
t.hideturtle()
# 画正六边形
t.penup()
t.goto(150,0)
t.seth(90)
t.pendown()
t.fillcolor('red')
t.begin_fill()
t.circle(radius=150,steps=6)
t.end_fill()
# 画外部三角形
t.seth(0)
t.pencolor('white')
t.fillcolor('green')
for i in range(6):
    t.begin_fill()
    t.seth(60*(i+1))
    t.forward(150)
    t.left(120)
    t.forward(150)
    t.end_fill()
turtle.done()

4. Draw a fan
Problem description
Insert image description here
Problem-solving ideas
(1) Calculate the starting angle first, is (180-120)/2=30°, and ends at 30+120=150. It loops once for each degree. You can use for or while True
(2) Each time you draw, First, lift the pen and retreat 30 to reach the bottom of the fan. Then draw 150+30 to draw a complete line, then retreat 150 and return to the original point
(3) Loop 150 times to draw the fan< /span>
(4) Make full use of forward(30) to move forward, while forward(-30) only goes back without changing direction.

import turtle
t=turtle.Turtle()
# t.speed(speed=0)
t.hideturtle()
# 起始角度为30,每一度画一次起始(180-120)/2=30,结束30+120=150
ang1=30
while ang1<=150:
    t.penup()
    t.seth(ang1)
    # 回退到扇子底部,走30
    t.forward(-30)
    t.pendown()
    # 走到扇子底部,走150+30
    t.forward(180)
    # 抬笔回退150,回到原点
    t.penup()
    t.forward(-150)
    ang1+=1

5. Draw a colored fan
Problem description
Insert image description here
Problem-solving ideas
(1) Use a list to store four types of Color, so that each cycle uses the subscript to get the required fill color
(2) First draw the first time, and then cycle according to the accumulation of 360/4=90 each time a> (5) Use for i in range(4) loops four times, each time it is the initial angle 45+360/4*i for the first time, and the other four sectors can be drawn repeatedly (4) t .circle(radius=100, extent=45) draws a 45-degree fan shape. After drawing, goto(0,0) closes the first fan shape.
(3) Set the initial angle to -45° for the first time, move 100, then turn left 90 degrees, and prepare to draw a fan shape. This requires repeated practice

import turtle

t = turtle.Turtle()
# t.speed(speed=0)
t.hideturtle()
listc = ['red', 'yellow', 'blue', 'green']

for i in range(4):
    # 依次从列表中获取颜色
    t.fillcolor(listc[i])
    t.begin_fill()
    # 设置初始角度为45°,后面每次累加360/4
    t.seth(-45+360/4*i)
    # 直行100
    t.forward(100)
    # 左转90度,以便画扇形,这个是试出来的
    t.left(90)
    # 画45°的扇形
    t.circle(radius=100, extent=45)
    # 封口
    t.goto(0, 0)
    t.end_fill()
turtle.done()

Guess you like

Origin blog.csdn.net/qq_43662503/article/details/124568196