首页 > 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进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐