溫馨提示×

怎么使用python畫海綿寶寶

小億
258
2023-07-04 14:17:32
欄目: 編程語言

要使用Python畫海綿寶寶,你可以使用Python的圖形庫(如turtle或pygame)來實現(xiàn)。下面是一個使用turtle庫繪制海綿寶寶的示例代碼:

import turtle
# 設置海綿寶寶的顏色
spongebob_color = "yellow"
eyes_color = "blue"
mouth_color = "red"
# 設置海綿寶寶的畫筆屬性
turtle.pensize(3)
turtle.speed(3)
# 繪制海綿寶寶的臉
turtle.penup()
turtle.goto(0, -100)
turtle.pendown()
turtle.fillcolor(spongebob_color)
turtle.begin_fill()
turtle.circle(100)
turtle.end_fill()
# 繪制海綿寶寶的眼睛
turtle.penup()
turtle.goto(-40, 40)
turtle.pendown()
turtle.fillcolor(eyes_color)
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(40, 40)
turtle.pendown()
turtle.fillcolor(eyes_color)
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
# 繪制海綿寶寶的嘴巴
turtle.penup()
turtle.goto(-40, 0)
turtle.pendown()
turtle.setheading(-60)
turtle.circle(40, 120)
turtle.fillcolor(mouth_color)
turtle.begin_fill()
turtle.setheading(60)
turtle.circle(-40, 120)
turtle.end_fill()
# 繪制海綿寶寶的牙齒
turtle.penup()
turtle.goto(-35, -10)
turtle.pendown()
turtle.setheading(-30)
turtle.forward(30)
turtle.setheading(90)
turtle.forward(10)
turtle.setheading(210)
turtle.forward(30)
turtle.penup()
turtle.goto(5, -10)
turtle.pendown()
turtle.setheading(-30)
turtle.forward(30)
turtle.setheading(90)
turtle.forward(10)
turtle.setheading(210)
turtle.forward(30)
# 隱藏海綿寶寶的畫筆
turtle.hideturtle()
# 點擊窗口關閉程序
turtle.done()

運行以上代碼,你將看到一個使用turtle繪制的海綿寶寶。你可以根據(jù)需要調整顏色和位置來繪制不同風格的海綿寶寶。

1