Python 是一種強大的編程語言,它不僅易學易用,而且可以用來完成各種有趣的任務。比如,你可以用 Python 來畫出一個有趣的海綿寶寶圖案。
import turtle # 設置畫布大小 turtle.setup(width=800, height=600) # 設置畫筆大小和顏色 turtle.pensize(4) turtle.pencolor('yellow') # 畫海綿寶寶的頭部 turtle.fillcolor('yellow') turtle.begin_fill() turtle.circle(150) turtle.end_fill() # 畫海綿寶寶的眼睛 turtle.penup() turtle.goto(-70, 170) turtle.pendown() turtle.fillcolor('white') turtle.begin_fill() turtle.circle(30) turtle.end_fill() turtle.penup() turtle.goto(70, 170) turtle.pendown() turtle.fillcolor('white') turtle.begin_fill() turtle.circle(30) turtle.end_fill() turtle.pencolor('black') turtle.penup() turtle.goto(-70, 180) turtle.pendown() turtle.fillcolor('black') turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.penup() turtle.goto(70, 180) turtle.pendown() turtle.fillcolor('black') turtle.begin_fill() turtle.circle(10) turtle.end_fill() # 畫海綿寶寶的鼻子 turtle.pencolor('black') turtle.penup() turtle.goto(0, 130) turtle.pendown() turtle.fillcolor('red') turtle.begin_fill() turtle.circle(20) turtle.end_fill() # 畫海綿寶寶的嘴巴 turtle.penup() turtle.goto(-100, 20) turtle.pendown() turtle.pencolor('black') turtle.right(60) turtle.circle(100, 120) turtle.circle(100, -60) # 畫海綿寶寶的牙齒 turtle.penup() turtle.goto(-40, 10) turtle.pendown() turtle.pencolor('white') turtle.right(120) turtle.forward(25) turtle.right(90) turtle.forward(10) turtle.right(90) turtle.forward(25) turtle.penup() turtle.goto(40, 10) turtle.pendown() turtle.right(180) turtle.forward(25) turtle.right(90) turtle.forward(10) turtle.right(90) turtle.forward(25) # 完成畫圖 turtle.done()
以上就是用 Python 畫海綿寶寶圖案的代碼。我們可以通過調(diào)整畫筆顏色、畫布大小等參數(shù)來定制自己個性化的海綿寶寶圖案。讓我們一起來探索 Python 的神奇之處吧!