Python turtle模块小黄人程序

本文通过Python的turtle模块,详细介绍了如何绘制一个小黄人的图形。从身体、眼睛到嘴巴,逐步展示了如何使用turtle的各种函数来实现。难点在于理解turtle在绘制过程中的方向变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

讲解Python初级课程的turtle模块,简单粗暴的编写了小黄人的程序。程序还需要进一步优化。难点就是要搞清楚turtle在绘制图形过程中的方向变化。

 

 

 

 

import turtle
t = turtle.Turtle()
wn = turtle.Screen()
turtle.colormode(255)
t.hideturtle()
t.speed(0)
t.penup()
t.pensize(4)
t.goto(100,0)
t.pendown()
t.left(90)
t.color((0,0,0),(255,255,0))
#身体绘制上色
t.begin_fill()
t.forward(200)
t.circle(100,180)
t.forward(200)
t.circle(100,180)
t.end_fill()
#右眼睛绘制上色
t.pensize(12)
t.penup()
t.goto(-100,200)
t.pendown()
t.right(100)
t.circle(500,23)

t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()

t.penup()
t.goto(15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()

t.penup()
t.goto(35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()
#左眼睛绘制上色
t.pensize(3)
t.penup()
t.goto(0,200)
t.pendown()
t.seth(90)
t.color("black","white")
t.begin_fill()
t.circle(30)
t.end_fill()

t.penup()
t.goto(-15,200)
t.pendown()
t.color("black","black")
t.begin_fill()
t.circle(15)
t.end_fill()

t.penup()
t.goto(-35,205)
t.color("black","white")
t.begin_fill()
t.circle(5)
t.end_fill()

#嘴绘制上色
t.penup()
t.goto(-20,100)
t.pendown()
t.seth(270)
t.color("black","white")
t.begin_fill()
t.circle(20,180)
t.left(90)
t.forward(40)
t.end_fill()

#裤子绘制上色
t.penup()
t.goto(-100,0)
t.pendown()
t.seth(0)
t.color("black","blue")
t.begin_fill()
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(160)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.seth(270)
t.penup()
t.goto(-100,0)
t.circle(100,180)
t.end_fill()

#左裤子腰带
t.penup()
t.goto(-70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(45)
t.forward(15)
t.left(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.left(40)
t.forward(50)
t.end_fill()
t.left(180)
t.goto(-70,30)
t.dot()

#右裤腰带
t.penup()
t.goto(70,20)
t.pendown()
t.color("black","blue")
t.begin_fill()
t.seth(135)
t.forward(15)
t.right(90)
t.forward(60)
t.seth(270)
t.forward(15)
t.right(40)
t.forward(50)
t.end_fill()

t.left(180)
t.goto(70,30)

t.dot()



#脚

t.penup()
t.goto(4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.left(90)
t.forward(40)
t.seth(20)
t.circle(10,180)
t.circle(400,2)
t.seth(90)
t.forward(20)
t.goto(4,-100)
t.end_fill()

t.penup()
t.goto(-4,-100)
t.pendown()
t.seth(270)
t.color("black","black")
t.begin_fill()
t.forward(30)
t.right(90)
t.forward(40)
t.seth(20)
t.circle(10,-225)
t.circle(400,-3)
t.seth(90)
t.forward(21)
t.goto(-4,-100)
t.end_fill()




#左手
t.penup()
t.goto(-100,50)
t.pendown()
t.seth(225)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.left(90)
t.forward(35)
t.seth(90)
t.forward(50)
t.end_fill()
#右手
t.penup()
t.goto(100,50)
t.pendown()
t.seth(315)
t.color("black","yellow")
t.begin_fill()
t.forward(40)
t.right(90)
t.forward(36)
t.seth(90)
t.forward(50)
t.end_fill()

#
t.penup()
t.goto(0,-100)
t.pendown()
t.forward(30)

#
t.penup()
t.goto(0,-20)
t.pendown()
t.color("yellow")
t.begin_fill()
t.seth(45)
t.forward(20)
t.circle(10,180)
t.right(90)
t.circle(10,180)
t.forward(20)
t.end_fill()

#
t.penup()
t.color("black")
t.goto(-100,-20)
t.pendown()
t.circle(30,90)

t.penup()
t.goto(100,-20)
t.pendown()
t.circle(30,-90)
#头顶
t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(135)
t.circle(100,40)
t.end_fill()

t.penup()
t.goto(2,300)
t.pendown()
t.begin_fill()
t.seth(45)
t.circle(100,40)
t.end_fill()

转载于:https://www.cnblogs.com/jennyzhangeducation/p/10704190.html

### 回答1: 以下是使用 Pythonturtle 库画小黄的代码: ```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: 下面是用Pythonturtle模块小黄的代码: ```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函数用于画眼镜。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值