py用turtle画小黄人的代码
时间: 2023-08-31 12:23:18 浏览: 374
使用python画小黄人,几行代码.md
### 回答1:
以下是使用 Python 的 turtle 库画小黄人的代码:
```python
import turtle
# 设置画笔大小和颜色
turtle.pensize(4)
turtle.pencolor("black")
# 画身体
turtle.fillcolor("yellow")
turtle.begin_fill()
turtle.circle(100)
turtle.end_fill()
# 画眼睛
turtle.penup()
turtle.goto(-40, 120)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(40, 120)
turtle.pendown()
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.goto(-30, 130)
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
turtle.penup()
turtle.goto(30, 130)
turtle.pendown()
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
# 画嘴巴
turtle.penup()
turtle.goto(-60, 80)
turtle.pendown()
turtle.pencolor("red")
turtle.right(60)
turtle.circle(80, 120)
# 画手
turtle.penup()
turtle.goto(-120, )
turtle.pendown()
turtle.pencolor("black")
turtle.right(30)
turtle.forward(100)
turtle.right(120)
turtle.forward(100)
turtle.right(120)
turtle.forward(100)
turtle.penup()
turtle.goto(120, )
turtle.pendown()
turtle.right(180)
turtle.forward(100)
turtle.right(120)
turtle.forward(100)
turtle.right(120)
turtle.forward(100)
turtle.done()
```
希望对你有帮助!
### 回答2:
import turtle
# 设置画布大小
turtle.setup(800, 600)
# 创建画笔
pen = turtle.Turtle()
pen.speed(3)
pen.pensize(3)
# 定义小黄人的身体颜色和头部颜色
body_color = "#FED043"
face_color = "#F9E000"
# 画身体
pen.penup()
pen.goto(-50, -100)
pen.pendown()
pen.begin_fill()
pen.fillcolor(body_color)
pen.seth(45)
pen.circle(100, 90)
pen.seth(135)
pen.circle(100, 90)
pen.end_fill()
# 画头部
pen.penup()
pen.goto(-50, 0)
pen.pendown()
pen.begin_fill()
pen.fillcolor(face_color)
pen.seth(45)
pen.circle(50, 90)
pen.seth(135)
pen.circle(50, 90)
pen.end_fill()
# 画眼睛
pen.penup()
pen.goto(-20, 50)
pen.pendown()
pen.begin_fill()
pen.fillcolor("white")
pen.circle(20)
pen.end_fill()
pen.penup()
pen.goto(-10, 60)
pen.pendown()
pen.begin_fill()
pen.fillcolor("black")
pen.circle(10)
pen.end_fill()
pen.penup()
pen.goto(-80, 50)
pen.pendown()
pen.begin_fill()
pen.fillcolor("white")
pen.circle(20)
pen.end_fill()
pen.penup()
pen.goto(-70, 60)
pen.pendown()
pen.begin_fill()
pen.fillcolor("black")
pen.circle(10)
pen.end_fill()
# 画眉毛
pen.penup()
pen.goto(-40, 80)
pen.pendown()
pen.pensize(12)
pen.seth(180)
pen.circle(30, 60)
pen.penup()
pen.goto(-50, 80)
pen.pendown()
pen.seth(0)
pen.circle(-30, 60)
# 画嘴巴
pen.penup()
pen.goto(-60, 10)
pen.pendown()
pen.pensize(8)
pen.seth(-30)
pen.circle(60, 120)
# 画手臂
pen.penup()
pen.goto(-130, -30)
pen.pendown()
pen.pensize(15)
pen.seth(-110)
pen.circle(100, 20)
pen.penup()
pen.goto(20, -30)
pen.pendown()
pen.seth(-70)
pen.circle(-100, 20)
# 画腿
pen.penup()
pen.goto(-70, -180)
pen.pendown()
pen.pensize(20)
pen.seth(-90)
pen.forward(100)
pen.penup()
pen.goto(20, -180)
pen.pendown()
pen.seth(-90)
pen.forward(100)
# 完成作画
turtle.done()
### 回答3:
下面是用Python的turtle模块画小黄人的代码:
```python
import turtle
def draw_face():
# 画小黄人的脸部
turtle.begin_fill()
turtle.circle(50)
turtle.color("yellow")
turtle.end_fill()
def draw_eyes():
# 画小黄人的眼睛
turtle.penup()
turtle.goto(-20, 60)
turtle.pendown()
turtle.begin_fill()
turtle.circle(10)
turtle.color("white")
turtle.end_fill()
turtle.penup()
turtle.goto(20, 60)
turtle.pendown()
turtle.begin_fill()
turtle.circle(10)
turtle.color("white")
turtle.end_fill()
def draw_mouth():
# 画小黄人的嘴巴
turtle.penup()
turtle.goto(-25, 40)
turtle.pendown()
turtle.right(90)
turtle.circle(25, 180)
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
def draw_glasses():
# 画小黄人的眼镜
turtle.penup()
turtle.goto(-50, 60)
turtle.pendown()
turtle.right(110)
turtle.forward(100)
turtle.left(90)
turtle.forward(5)
turtle.left(90)
turtle.forward(43)
turtle.left(90)
turtle.forward(5)
turtle.left(90)
turtle.forward(43)
turtle.left(90)
turtle.forward(5)
turtle.left(90)
turtle.forward(43)
turtle.left(90)
turtle.forward(5)
turtle.left(90)
turtle.forward(43)
turtle.penup()
turtle.goto(115, 60)
turtle.pendown()
turtle.right(180)
turtle.forward(100)
def main():
turtle.speed(2) # 画图的速度
turtle.penup()
turtle.goto(-50, 0)
turtle.pendown()
draw_face()
draw_eyes()
draw_mouth()
draw_glasses()
turtle.done()
if __name__ == "__main__":
main()
```
运行以上代码会在一个窗口中显示出用turtle模块画的小黄人。其中,draw_face函数用于画脸部,draw_eyes函数用于画眼睛,draw_mouth函数用于画嘴巴,draw_glasses函数用于画眼镜。
阅读全文