用python的turtle库画一只猫,写出代码
时间: 2023-09-26 09:06:37 浏览: 171
Python的turtle库绘制
5星 · 资源好评率100%
### 回答1:
from turtle import * penup() goto(-200, 100) pendown() circle(50) #头penup() goto(-150, 70) pendown() circle(20) #右耳penup() goto(-230, 70) pendown() circle(20) #左耳penup() goto(-200, 0) pendown() goto(-150, 0) goto(-100, 100) #身体penup() goto(-100, 50) pendown() goto(-50, 50) goto(-50, 0) goto(-100, 0) #尾巴penup() goto(-30, 0) pendown() goto(-30, -50) #右腿penup() goto(-20, 0) pendown() goto(-20, -50) #左腿penup() goto(-50, -20) pendown() goto(-60, -50) #右眼penup() goto(-170, 10) pendown() circle(5) #左眼penup() goto(-220, 10) pendown() circle(5)
### 回答2:
当使用Python的turtle库画一只猫的时候,可以使用以下代码:
```python
import turtle
# 创建画布和画笔
canvas = turtle.Screen()
pen = turtle.Turtle()
# 绘制猫脸
pen.fillcolor("gray")
pen.begin_fill()
pen.circle(100)
pen.end_fill()
# 绘制猫耳朵
pen.penup()
pen.goto(-70, 130)
pen.pendown()
pen.fillcolor("black")
pen.begin_fill()
pen.right(140)
pen.forward(50)
pen.left(120)
pen.forward(50)
pen.right(140)
pen.forward(120)
pen.left(150)
pen.forward(150)
pen.left(120)
pen.forward(120)
pen.right(140)
pen.forward(50)
pen.left(120)
pen.forward(50)
pen.end_fill()
# 绘制猫眼
pen.penup()
pen.goto(-25, 40)
pen.pendown()
pen.fillcolor("white")
pen.begin_fill()
pen.circle(20)
pen.end_fill()
pen.penup()
pen.goto(-35, 45)
pen.pendown()
pen.fillcolor("black")
pen.begin_fill()
pen.circle(10)
pen.end_fill()
# 绘制猫鼻子
pen.penup()
pen.goto(10, 0)
pen.pendown()
pen.fillcolor("black")
pen.begin_fill()
pen.circle(15)
pen.end_fill()
# 绘制猫嘴巴
pen.penup()
pen.goto(15, -20)
pen.pendown()
pen.right(90)
pen.forward(25)
pen.right(220)
pen.forward(50)
pen.right(140)
pen.forward(50)
# 绘制猫胡须
pen.penup()
pen.goto(-35, 5)
pen.pendown()
pen.right(75)
pen.forward(90)
pen.penup()
pen.goto(-35, -15)
pen.pendown()
pen.right(90)
pen.forward(90)
pen.penup()
pen.goto(25, 5)
pen.pendown()
pen.left(75)
pen.forward(90)
# 绘制猫身体
pen.penup()
pen.goto(-55, -120)
pen.pendown()
pen.fillcolor("gray")
pen.right(150)
pen.forward(150)
pen.left(90)
pen.forward(50)
pen.left(90)
pen.forward(70)
pen.left(90)
pen.forward(50)
pen.left(90)
pen.forward(70)
pen.left(90)
pen.forward(50)
# 绘制猫脚
pen.penup()
pen.goto(-55, -195)
pen.pendown()
pen.left(45)
pen.forward(40)
pen.left(180)
pen.forward(40)
pen.left(135)
pen.forward(60)
pen.left(180)
pen.forward(60)
pen.penup()
pen.goto(95, -195)
pen.pendown()
pen.right(45)
pen.forward(40)
pen.left(180)
pen.forward(40)
pen.right(135)
pen.forward(60)
# 绘制猫尾巴
pen.speed(4)
pen.penup()
pen.goto(125, -135)
pen.pendown()
pen.right(30)
pen.forward(80)
pen.right(120)
pen.forward(80)
pen.right(150)
pen.forward(60)
pen.right(180)
pen.forward(60)
# 绘制猫颈部
pen.speed(1)
pen.penup()
pen.goto(0, -50)
pen.pendown()
pen.fillcolor("gray")
pen.begin_fill()
pen.left(45)
pen.forward(30)
pen.right(135)
pen.forward(30)
pen.right(45)
pen.forward(30)
pen.right(135)
pen.forward(30)
pen.end_fill()
# 隐藏画笔
pen.hideturtle()
# 关闭画布
canvas.exitonclick()
```
运行以上代码后,会在turtle图形窗口中绘制一只微笑的猫。你可以根据需要对猫的图形进行进一步调整和美化。
### 回答3:
import turtle
# 设置画笔的颜色和填充颜色
def set_color(pen_color, fill_color):
turtle.pencolor(pen_color)
turtle.fillcolor(fill_color)
# 画一个实心圆
def draw_circle(radius):
turtle.begin_fill()
turtle.circle(radius)
turtle.end_fill()
# 画一个实心矩形
def draw_rectangle(width, height):
turtle.begin_fill()
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
turtle.end_fill()
# 画猫的头
def draw_cat_head():
turtle.penup()
turtle.goto(0, -100)
turtle.pendown()
set_color("black", "grey")
draw_circle(100)
# 画猫的耳朵
def draw_cat_ears():
turtle.penup()
turtle.goto(-70, 50)
turtle.pendown()
set_color("black", "grey")
draw_circle(50)
turtle.penup()
turtle.goto(70, 50)
turtle.pendown()
set_color("black", "grey")
draw_circle(50)
# 画猫的眼睛
def draw_cat_eyes():
turtle.penup()
turtle.goto(-40, 30)
turtle.pendown()
set_color("black", "black")
draw_circle(20)
turtle.penup()
turtle.goto(40, 30)
turtle.pendown()
set_color("black", "black")
draw_circle(20)
# 画猫的嘴巴
def draw_cat_mouth():
turtle.penup()
turtle.goto(-20, -10)
turtle.pendown()
turtle.width(3)
set_color("black", "black")
turtle.right(90)
turtle.circle(20, 180)
# 画猫的身体
def draw_cat_body():
turtle.penup()
turtle.goto(-100, -200)
turtle.pendown()
set_color("black", "grey")
draw_rectangle(200, 200)
# 画猫的四条腿
def draw_cat_legs():
turtle.penup()
turtle.goto(-100, -200)
turtle.pendown()
set_color("black", "grey")
turtle.goto(-140, -300)
turtle.goto(-160, -370)
turtle.penup()
turtle.goto(100, -200)
turtle.pendown()
set_color("black", "grey")
turtle.goto(140, -300)
turtle.goto(160, -370)
# 画猫的尾巴
def draw_cat_tail():
turtle.penup()
turtle.goto(160, -350)
turtle.pendown()
set_color("black", "grey")
turtle.left(30)
turtle.circle(200, 60)
# 画猫的喵喵叫
def draw_cat_meow():
turtle.penup()
turtle.goto(0, -100)
turtle.pendown()
turtle.width(1)
set_color("black", "black")
turtle.write("喵喵叫~", align="center", font=("Arial", 16, "bold"))
# 设置画笔的速度和窗口大小
def set_pen_speed_and_window_size(speed, width, height):
turtle.speed(speed)
turtle.setup(width, height)
# 主函数,调用各个函数来画一只猫
def main():
set_pen_speed_and_window_size(5, 800, 600)
draw_cat_head()
draw_cat_ears()
draw_cat_eyes()
draw_cat_mouth()
draw_cat_body()
draw_cat_legs()
draw_cat_tail()
draw_cat_meow()
turtle.done()
# 调用主函数
if __name__ == "__main__":
main()
阅读全文