Python是一種流行的編程語言,可以用它來制作許多有趣的應用,包括圖形繪制。Python里一個非常有趣的圖形繪制包是turtle,它可以讓我們繪制出各種漂亮的圖案。
# 導入turtle庫 import turtle # 設置畫布大小和初始位置 turtle.setup(600, 600) turtle.penup() turtle.goto(-150, 150) turtle.pendown() # 繪制皮卡丘 def draw_pikachu(): turtle.circle(60, 90) turtle.fd(60) turtle.circle(60, 90) turtle.fd(60) turtle.circle(45, 180) turtle.fd(30) turtle.circle(20, 180) turtle.fd(10) turtle.circle(45, 180) turtle.fd(30) turtle.circle(20, 180) turtle.penup() turtle.goto(-75, 75) turtle.pendown() turtle.begin_fill() turtle.circle(15) turtle.end_fill() turtle.penup() turtle.goto(75, 75) turtle.pendown() turtle.begin_fill() turtle.circle(15) turtle.end_fill() turtle.penup() turtle.goto(0, 0) turtle.pendown() turtle.color('#feba32') turtle.begin_fill() turtle.circle(100) turtle.end_fill() # 繪制皮卡丘的眼睛和嘴巴 def draw_face(): turtle.penup() turtle.goto(-40, 110) turtle.pendown() turtle.color('black') turtle.begin_fill() turtle.circle(4) turtle.end_fill() turtle.penup() turtle.goto(40, 110) turtle.pendown() turtle.begin_fill() turtle.circle(4) turtle.end_fill() turtle.penup() turtle.goto(0, 60) turtle.pendown() turtle.width(6) turtle.color('black') turtle.right(60) turtle.fd(50) turtle.penup() turtle.goto(-30, 30) turtle.pendown() turtle.right(48) turtle.fd(38) turtle.penup() turtle.goto(30, 30) turtle.pendown() turtle.right(84) turtle.fd(38) # 調用函數,繪制皮卡丘和表情 draw_pikachu() draw_face() # 隱藏畫筆 turtle.hideturtle() # 等待關閉 turtle.done()
以上就是用Python turtle包繪制皮卡丘的全部代碼。我們可以通過調用turtle的各種函數,像玩樂高積木一樣構建出我們想要的圖案,非常有趣!