Python小乌龟画迷宫
时间: 2023-12-14 16:32:18 浏览: 152
Python:Turtle库绘画小海龟迷宫小游戏
以下是Python小乌龟画迷宫的示例代码:
```python
import turtle
# 设置窗口大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('white')
# 定义迷宫墙壁的函数
def draw_wall():
turtle.color('black')
turtle.pensize(5)
turtle.penup()
turtle.goto(-300, 200)
turtle.pendown()
turtle.forward(600)
turtle.right(90)
turtle.forward(400)
turtle.right(90)
turtle.forward(600)
turtle.right(90)
turtle.forward(400)
turtle.right(90)
# 定义小乌龟的函数
def draw_turtle():
turtle.color('red')
turtle.shape('turtle')
turtle.penup()
turtle.goto(-280, 180)
turtle.pendown()
# 调用函数画出迷宫墙壁和小乌龟
draw_wall()
draw_turtle()
# 控制小乌龟移动
turtle.penup()
turtle.goto(-280, 180)
turtle.pendown()
turtle.speed(1)
turtle.forward(40)
turtle.left(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(40)
turtle.left(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(40)
turtle.right(90)
turtle.forward(40)
turtle.left(90)
turtle.forward(40)
# 隐藏小乌龟
turtle.hideturtle()
# 等待用户关闭窗口
turtle.done()
```
阅读全文