畫五星紅旗其實主要的工作就是繪制五角星,我們思路就是定義一個繪制五角星的方法,然后確定每個五角星的位置,開始繪制即可。

一、scratch繪制五角星的思路:因為scratch中沒有方法可以直接填充圖案,所以我們就不斷地用畫筆繪制五角星的輪廓,邊長不斷地減小,直到邊長變成0,這樣就把五角星繪制完成并進行了填充。

如何用scrtch和python繪制五星紅旗

如何用scrtch和python繪制五星紅旗


以上程序由網友(遼寧~Steven提供)

二、使用Python繪制五角星的思路:如下圖的方式畫五角星,然后用黃色填充。重復執行5次,每次向前移動同樣的距離,然后右轉144度。

如何用scrtch和python繪制五星紅旗

-------------------python源碼----------------------

import turtle

# 定義一個函數,可以畫五角星

def picture(x1,y1,x,y):

   turtle.up()

   turtle.goto(x1, y1)

   turtle.down()

#用于填充顏色

   turtle.begin_fill()

   turtle.fillcolor("yellow")

   for i in range(5):

       turtle.forward(x)

       turtle.right(y)

   turtle.end_fill()

# 速度為3

turtle.speed(3)

# 背景紅色

turtle.bgcolor("red")

# 畫筆顏色


turtle.color("yellow")

picture(-350,170,100,144)

turtle.setheading(30)

picture(-181,290,60,144)

turtle.setheading(15)

picture(-130,220,60,144)

turtle.setheading(0)

picture(-130,120,60,144)

turtle.setheading(-15)

picture(-180,50,60,144)

turtle.done()


本站內容未經許可,禁止任何網站及個人進行轉載。