首页 > Python资料 博客日记
python-海龟库(turtle)
2025-01-16 20:30:06Python资料围观11次
这篇文章介绍了python-海龟库(turtle),分享给大家做个参考,收藏Python资料网收获更多编程知识
趁着有空的时间到了一家少儿编程培训当了一段编程老师,讲的就是turtle的使用
发现turtle还是有点意思
1:安装
这里也可以直接安装海龟编辑器(Python编辑器),安装完直接有环境了
手动安装 如下:
1> 环境 win10
2>python最新版,直接安装 网上一堆
这里为 Python 3.12.6
3>海龟库 turtle-0.0.2 下载地址 https://pypi.org/project/turtle/#files
下载解压 修改setup.py文件
打开setup.py文件,第40行修改为:except (ValueError, ve):
Python2的写法,没有括号,python3需要加()
4>用pip安装:pip install -e turtle-0.0.2
turtle-0.0.2为setup.py 所在的目录
5> pycharm-community-2024.2.1(其他版本也可以) 网上教程一堆,自行安装
2:文档
https://docs.python.org/zh-cn/3/library/turtle.html
3:贪吃蛇
#import turtle
#turtle.circle(60,360,7) #5边形
#turtle.mainloop()
import turtle
import copy
from random import randrange
snake = [[0, 0], [0, 10], [0, 20]]
aim = [0, 10]
food = [-10, 0]
def change_direction(x, y):
aim[0] = x
aim[1] = y
def square(x, y, size, color):
# 抬起画笔
turtle.penup()
# 画笔痕迹
turtle.goto(x, y)
# 放下画笔
turtle.pendown()
# 进行渲染
turtle.color(color)
turtle.begin_fill()
# 在画布上画四笔转一圈生成一个方块
for i in range(4):
turtle.forward(size)
turtle.left(90)
turtle.end_fill()
def inside(head):
return -250 < head[0] < 250 and -250 < head[1] < 250
# 定义蛇的移动的函数
def sanke_move():
head = copy.deepcopy(snake[-1])
head = [head[0] + aim[0], head[1] + aim[1]]
print(head[0], head[1])
# 判断是否发生了碰撞
if head in snake or not inside(head):
print("Game Over!")
square(head[0], head[1], 10, "red")
return
# 判断蛇碰到食物后的操作
if head == food:
food[0] = randrange(-15, 15) * 10
food[1] = randrange(-15, 15) * 10
else:
snake.pop(0)
snake.append(head)
turtle.clear()
square(food[0], food[1], 10, "blue")
for body in snake:
square(body[0], body[1], 10, "black")
turtle.update()
turtle.ontimer(sanke_move, 300)
# 设置屏幕的大小
turtle.setup(500, 500)
# 去除一个一个画方块的动画
turtle.tracer(False)
# 去掉箭头(画画用的画笔)
turtle.hideturtle()
turtle.listen()
turtle.onkey(lambda: change_direction(0, 10), "Up")
turtle.onkey(lambda: change_direction(0, -10), "Down")
turtle.onkey(lambda: change_direction(-10, 0), "Left")
turtle.onkey(lambda: change_direction(10, 0), "Right")
sanke_move()
# 不让屏幕立马消失
turtle.done()
4:多边形
import turtle
import time
import math
def fun_draw(steplength,angle,count):
i = 0
while i< count :
turtle.forward(steplength)
if i+1 < count :
turtle.right(angle)
i += 1
def fun_out(angle,distance):
turtle.left(angle)
turtle.forward(distance)
turtle.right(angle)
turtle.forward(distance)
turtle.right(angle)
def fun_draw2(steplength,angle,count):
i = 0
while i< count :
if i == 0 :
turtle.backward(int(steplength/2))
turtle.forward(steplength)
turtle.right(angle)
i += 1
def fun_out2(angle,distance):
turtle.backward(distance)
turtle.left(angle)
turtle.forward(distance)
turtle.right(angle)
def draw_count(count,border,angle,steplength):
i = 0
dis = 0
# v1 = 360 / angle
v1 = 360/angle
anglereal = (180/int(v1+1))* math.pi /180
v = math.tan(anglereal)
# movdis = int(steplength/2 / v)
while i < count :
turtle.color("red")
# dis = turtle.distance(0,0)
turtle.home()
turtle.pendown()
turtle.color("orange")
turtle.left(90)
newsteplength = int((steplength+i*border*2)/2/v)
turtle.forward(newsteplength)
turtle.right(90)
turtle.color("red")
fun_draw2(steplength+i*border*2,angle,int(360/angle))
# turtle.penup()
turtle.color("green")
fun_out2(angle, border)
i += 1
def draw_count2(count,border,angle,steplength):
i = 0
dis = 0
# anglereal = angle * math.pi / 180
# sinv = math.sin(anglereal)
anglereal = 180/(180/angle)/2*math.pi / 180
tanv = math.tan(anglereal)
#tan30 = 0.57735026919
# movedis = tanv * steplength
while i < count:
if i % 2 == 0 :
turtle.color("red")
else:
turtle.color("blue")
turtle.home()
turtle.pendown()
# turtle.color("orange")
turtle.left(90)
steplength2 = steplength + i * border * 2
movedis = steplength2/2 /tanv
print("h=",movedis," steplength2=",steplength2)
turtle.forward(movedis)
# turtle.forward(movedis+i*border)
turtle.right(90)
# turtle.color("red")
fun_draw2(steplength2, angle, int(360 / angle))
# turtle.penup()
# turtle.color("green")
# fun_out2(angle, border)
i += 1
def draw_count3(count,border,angle,steplength):
i = 0
dis = 0
# anglereal = angle * math.pi / 180
# sinv = math.sin(anglereal)
anglereal = angle/2 *math.pi / 180
tanv = math.tan(anglereal)
#tan30 = 0.57735026919
# movedis = tanv * steplength
while i < count:
if i % 2 == 0 :
turtle.color("red")
else:
turtle.color("blue")
turtle.home()
turtle.pendown() #这句不加就隐藏了多余线条
# turtle.color("orange")
turtle.left(90)
steplength2 = steplength + i * border * 2
movedis = steplength2/2 /tanv
print("h=",movedis," steplength2=",steplength2)
turtle.forward(movedis)
# turtle.forward(movedis+i*border)
turtle.right(90)
# turtle.color("red")
fun_draw2(steplength2, angle, int(360 / angle))
# turtle.penup()
# turtle.color("green")
# fun_out2(angle, border)
i += 1
def write_text(text):
turtle.penup()
turtle.setpos(0, 300)
turtle.pendown()
turtle.write(text, False, align="center", font=("Arial", 20, "normal"))
turtle.penup()
def fun_draw_shape(list,steplist,borderlist):
n = 0
while n < len(list):
text = "第" + str(n + 1) + "个"+str(list[n])+"度"+str(int(360/list[n]))+"边形"
write_text(text)
# turtle.penup()
# turtle.setpos(0,300)
# turtle.pendown()
# turtle.write(text, False, align="center",font= ("Arial", 20, "normal"))
# turtle.penup()
time.sleep(1)
turtle.pensize(3)
draw_count3(3, borderlist[n], list[n], steplist[n])
time.sleep(1)
while turtle.undobufferentries():
turtle.undo()
n += 1
turtle.home()
#turtle.color("red")
turtle.pensize(3)
turtle.penup()
list = [5,10,15,30,45,60,72,90,120]
steplist = [10,20,30,50,60,70,80,90,100]
borderlist = [2,4,6,8,10,12,14,16,18]
nn = 0
while nn <1000:
fun_draw_shape(list,steplist,borderlist)
nn += 1
write_text("The End!")
# turtle.pendown()
#
# fun_draw(100,90,4)
#
# turtle.penup()
# fun_out(90,25)
# turtle.pendown()
# fun_draw(150,90,4)
# turtle.penup()
# fun_out(90,25)
# turtle.pendown()
# fun_draw(200,90,4)
turtle.done()
运行效果 (可以开启关闭多余轨迹)
多边形自行增加修改,当前是9个
5:如果觉得有用,麻烦点个赞,加个收藏
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- 光流法结合深度学习神经网络的原理及应用(完整代码都有Python opencv)
- Python 图像处理进阶:特征提取与图像分类
- 大数据可视化分析-基于python的电影数据分析及可视化系统_9532dr50
- 【Python】入门(运算、输出、数据类型)
- 【Python】第一弹---解锁编程新世界:深入理解计算机基础与Python入门指南
- 华为OD机试E卷 --第k个排列 --24年OD统一考试(Java & JS & Python & C & C++)
- Python已安装包在import时报错未找到的解决方法
- 【Python】自动化神器PyAutoGUI —告别手动操作,一键模拟鼠标键盘,玩转微信及各种软件自动化
- Pycharm连接SQL Sever(详细教程)
- Python编程练习题及解析(49题)
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Anaconda版本和Python版本对应关系(持续更新...)
- Python与PyTorch的版本对应
- Windows上安装 Python 环境并配置环境变量 (超详细教程)
- Python pyinstaller打包exe最完整教程