Python作為一種高級編程語言,在繪圖方面表現也是十分出色。在端午節即將到來之際,我們可以使用Python代碼繪制一個可愛的粽子圖案。下面讓我們一起來看一下吧!
import turtle # 設置畫布大小和繪制速度 turtle.setup(800, 600) turtle.speed(5) # 繪制粽子的包裹部分 turtle.color("green") turtle.begin_fill() turtle.penup() turtle.goto(-200, 0) turtle.down() turtle.forward(400) turtle.right(90) turtle.forward(200) turtle.right(90) turtle.forward(400) turtle.right(90) turtle.forward(200) turtle.end_fill() # 繪制粽子上的繩子和掛件 turtle.color("brown") turtle.penup() turtle.goto(-200, 200) turtle.down() turtle.right(60) turtle.forward(150) turtle.right(120) turtle.forward(150) turtle.right(120) turtle.forward(150) turtle.penup() turtle.goto(-170, 170) turtle.right(20) turtle.down() turtle.forward(50) turtle.right(45) turtle.forward(20) turtle.right(90) turtle.forward(20) turtle.right(45) turtle.forward(50) turtle.penup() turtle.goto(170, 170) turtle.setheading(80) turtle.down() turtle.forward(50) turtle.right(45) turtle.forward(20) turtle.right(90) turtle.forward(20) turtle.right(45) turtle.forward(50) # 繪制粽子上的韭菜 turtle.color("green") turtle.penup() turtle.goto(-70, 50) turtle.setheading(0) turtle.down() turtle.circle(30, 180) turtle.penup() turtle.goto(-70, 20) turtle.setheading(0) turtle.down() turtle.circle(20, 180) turtle.penup() turtle.goto(-70, -10) turtle.setheading(0) turtle.down() turtle.circle(10, 180) turtle.penup() turtle.goto(70, 50) turtle.setheading(180) turtle.down() turtle.circle(-30, 180) turtle.penup() turtle.goto(70, 20) turtle.setheading(180) turtle.down() turtle.circle(-20, 180) turtle.penup() turtle.goto(70, -10) turtle.setheading(180) turtle.down() turtle.circle(-10, 180) turtle.done()
以上就是使用Python繪制粽子代碼的相關內容,希望大家可以嘗試用Python創造更多有趣的繪圖作品。