添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
This example is inspired by the Wikipedia article on turtle graphics. (See example wikipedia1 for URLs) First we create (ne-1) (i.e. 35 in this example) copies of our first turtle p. Then we let them perform their steps in parallel. Followed by a complete undo(). from turtle import Screen, Turtle, mainloop from time import perf_counter as clock, sleep def mn_eck(p, ne,sz): turtlelist = [p] #create ne-1 additional turtles for i in range(1,ne): q = p.clone() q.rt(360.0/ne) turtlelist.append(q) p = q for i in range(ne): c = abs(ne/2.0-i)/(ne*.7) # let those ne turtles make a step # in parallel: for t in turtlelist: t.rt(360./ne) t.pencolor(1-c,0,c) t.fd(sz) def main(): s = Screen() s.bgcolor("black") p=Turtle() p.speed(0) p.hideturtle() p.pencolor("red") p.pensize(3) s.tracer(36,0) at = clock() mn_eck(p, 36, 19) et = clock() z1 = et-at sleep(1) at = clock() while any(t.undobufferentries() for t in s.turtles()): for t in s.turtles(): t.undo() et = clock() return "runtime: %.3f sec" % (z1+et-at) if __name__ == '__main__': msg = main() print(msg) mainloop()

bytedesign.py

#!/usr/bin/env python3
"""      turtle-example-suite:
        tdemo_bytedesign.py
An example adapted from the example-suite
of PythonCard's turtle graphics.
It's based on an article in BYTE magazine
Problem Solving with Logo: Using Turtle
Graphics to Redraw a Design
November 1982, p. 118 - 134
-------------------------------------------
Due to the statement
t.delay(0)
in line 152, which sets the animation delay
to 0, this animation runs in "line per line"
mode as fast as possible.
from turtle import Turtle, mainloop
from time import perf_counter as clock
# wrapper for any additional drawing routines
# that need to know about each other
class Designer(Turtle):
    def design(self, homePos, scale):
        self.up()
        for i in range(5):
            self.forward(64.65 * scale)
            self.down()
            self.wheel(self.position(), scale)
            self.up()
            self.backward(64.65 * scale)
            self.right(72)
        self.up()
        self.goto(homePos)
        self.right(36)
        self.forward(24.5 * scale)
        self.right(198)
        self.down()
        self.centerpiece(46 * scale, 143.4, scale)
        self.getscreen().tracer(True)
    def wheel(self, initpos, scale):
        self.right(54)
        for i in range(4):
            self.pentpiece(initpos, scale)
        self.down()
        self.left(36)
        for i in range(5):
            self.tripiece(initpos, scale)
        self.left(36)
        for i in range(5):
            self.down()
            self.right(72)
            self.forward(28 * scale)
            self.up()
            self.backward(28 * scale)
        self.left(54)
        self.getscreen().update()
    def tripiece(self, initpos, scale):
        oldh = self.heading()
        self.down()
        self.backward(2.5 * scale)
        self.tripolyr(31.5 * scale, scale)
        self.up()
        self.goto(initpos)
        self.setheading(oldh)
        self.down()
        self.backward(2.5 * scale)
        self.tripolyl(31.5 * scale, scale)
        self.up()
        self.goto(initpos)
        self.setheading(oldh)
        self.left(72)
        self.getscreen().update()
    def pentpiece(self, initpos, scale):
        oldh = self.heading()
        self.up()
        self.forward(29 * scale)
        self.down()
        for i in range(5):
            self.forward(18 * scale)
            self.right(72)
        self.pentr(18 * scale, 75, scale)
        self.up()
        self.goto(initpos)
        self.setheading(oldh)
        self.forward(29 * scale)
        self.down()
        for i in range(5):
            self.forward(18 * scale)
            self.right(72)
        self.pentl(18 * scale, 75, scale)
        self.up()
        self.goto(initpos)
        self.setheading(oldh)
        self.left(72)
        self.getscreen().update()
    def pentl(self, side, ang, scale):
        if side < (2 * scale): return
        self.forward(side)
        self.left(ang)
        self.pentl(side - (.38 * scale), ang, scale)
    def pentr(self, side, ang, scale):
        if side < (2 * scale): return
        self.forward(side)
        self.right(ang)
        self.pentr(side - (.38 * scale), ang, scale)
    def tripolyr(self, side, scale):
        if side < (4 * scale): return
        self.forward(side)
        self.right(111)
        self.forward(side / 1.78)
        self.right(111)
        self.forward(side / 1.3)
        self.right(146)
        self.tripolyr(side * .75, scale)
    def tripolyl(self, side, scale):
        if side < (4 * scale): return
        self.forward(side)
        self.left(111)
        self.forward(side / 1.78)
        self.left(111)
        self.forward(side / 1.3)
        self.left(146)
        self.tripolyl(side * .75, scale)
    def centerpiece(self, s, a, scale):
        self.forward(s); self.left(a)
        if s < (7.5 * scale):
            return
        self.centerpiece(s - (1.2 * scale), a, scale)
def main():
    t = Designer()
    t.speed(0)
    t.hideturtle()
    t.getscreen().delay(0)
    t.getscreen().tracer(0)
    at = clock()
    t.design(t.position(), 2)
    et = clock()
    return "runtime: %.2f sec." % (et-at)
if __name__ == '__main__':
    msg = main()
    print(msg)
    mainloop()

 clock.py:实现turtle做的时钟效果

#!/usr/bin/env python3
# -*- coding: cp1252 -*-
"""       turtle-example-suite:
             tdemo_clock.py
Enhanced clock-program, showing date
and time
  ------------------------------------
   Press STOP to exit the program!
  ------------------------------------
from turtle import *
from datetime import datetime
def jump(distanz, winkel=0):
    penup()
    right(winkel)
    forward(distanz)
    left(winkel)
    pendown()
def hand(laenge, spitze):
    fd(laenge*1.15)
    rt(90)
    fd(spitze/2.0)
    lt(120)
    fd(spitze)
    lt(120)
    fd(spitze)
    lt(120)
    fd(spitze/2.0)
def make_hand_shape(name, laenge, spitze):
    reset()
    jump(-laenge*0.15)
    begin_poly()
    hand(laenge, spitze)
    end_poly()
    hand_form = get_poly()
    register_shape(name, hand_form)
def clockface(radius):
    reset()
    pensize(7)
    for i in range(60):
        jump(radius)
        if i % 5 == 0:
            fd(25)
            jump(-radius-25)
        else:
            dot(3)
            jump(-radius)
        rt(6)
def setup():
    global second_hand, minute_hand, hour_hand, writer
    mode("logo")
    make_hand_shape("second_hand", 125, 25)
    make_hand_shape("minute_hand",  130, 25)
    make_hand_shape("hour_hand", 90, 25)
    clockface(160)
    second_hand = Turtle()
    second_hand.shape("second_hand")
    second_hand.color("gray20", "gray80")
    minute_hand = Turtle()
    minute_hand.shape("minute_hand")
    minute_hand.color("blue1", "red1")
    hour_hand = Turtle()
    hour_hand.shape("hour_hand")
    hour_hand.color("blue3", "red3")
    for hand in second_hand, minute_hand, hour_hand:
        hand.resizemode("user")
        hand.shapesize(1, 1, 3)
        hand.speed(0)
    writer = Turtle()
    #writer.mode("logo")
    writer.ht()
    writer.pu()
    writer.bk(85)
def wochentag(t):
    wochentag = ["Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday", "Sunday"]
    return wochentag[t.weekday()]
def datum(z):
    monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
             "July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
    j = z.year
    m = monat[z.month - 1]
    t = z.day
    return "%s %d %d" % (m, t, j)
def tick():
    t = datetime.today()
    sekunde = t.second + t.microsecond*0.000001
    minute = t.minute + sekunde/60.0
    stunde = t.hour + minute/60.0
        tracer(False)  # Terminator can occur here
        writer.clear()
        writer.home()
        writer.forward(65)
        writer.write(wochentag(t),
                     align="center", font=("Courier", 14, "bold"))
        writer.back(150)
        writer.write(datum(t),
                     align="center", font=("Courier", 14, "bold"))
        writer.forward(85)
        tracer(True)
        second_hand.setheading(6*sekunde)  # or here
        minute_hand.setheading(6*minute)
        hour_hand.setheading(30*stunde)
        tracer(True)
        ontimer(tick, 100)
    except Terminator:
        pass  # turtledemo user pressed STOP
def main():
    tracer(False)
    setup()
    tracer(True)
    tick()
    return "EVENTLOOP"
if __name__ == "__main__":
    mode("logo")
    msg = main()
    print(msg)
    mainloop()

关于此模块的更多详情,可以参考Python turtledemo官方文档 

Python库中的turtledemo模块,里面有非常多的turtle实例。rosette.py""" turtle-example-suite: tdemo_wikipedia3.pyThis example isinspired by the Wikipedia article on turtlegraphics. (See example wikipedia1 for URLs)First we create (ne-1) (i.e. 35 in
Python里,海龟不仅可以画简单的黑线,还可以用它画更复杂的几何形,用不同的颜色,甚至还可以给形状填色。 一、从基本的正方形开始 引入turtle模块并创建Pen对象: >>> import turtle >>> t = turtle.Pen() 前面我们用来创建正方形的代码如下: >>> t.forward(50) >>> t.left(90) >>> t.forward(50) >>> t.left(90) >>> t.forward(50) >>> t.left(90) >>> t,forward(50) 此段代码太长,我们可以用for循环进行优化: >>> t.reset(
最近学习python刚入门,有java编程经验,就跳过基础语法学习,直接做一些小程序来玩 目前学到turtle,这个乌龟画太有意思了,分享几个画,代码如下,原理不解释。 #SquareSpiral1.py import turtle t = turtle.Pen() turtle.bgcolor("black") sides=6 colors=["red","
from PIL import Image, ImageTk from time import time, sleep from random import choice, uniform, randint from math import sin, cos, radians # 模拟重力 GRAVITY = 0.05 # 颜色选项(随机或者按顺... 2. 绘制分形树:使用递归函数和角度控制,可以绘制出分形树的形状,让画更加生动。 3. 绘制迷宫:使用随机数和循环语句,可以绘制出各种形状的迷宫,让人们在探索中获得乐趣。 4. 绘制动态形:使用循环语句和延迟函数,可以绘制出动态的形,例如旋转的星星、跳跃的球等等。 总之,Python Turtle是一个非常有趣和强大的绘工具,可以让我们创造出各种想象中的画。 ### 回答2: Python Turtle是一种基于Python绘制工具,它提供了一种简单而有趣的方式来学习编程,特别是对初学者来说,能够让他们快速地创建出有趣且复杂的画。 绘制复杂画的过程并不简单,需要考虑的因素很多。下面介绍一下通过Python Turtle绘制复杂画的一般步骤。 第一步是先确定要绘制什么样的画,包括画的样式、外形、颜色等。这可以是一个草或者一个基本的设计,以便你能够大致了解要创建的画的整体外观。 第二步是编写代码,开始创建画。在编写代码之前,需要了解一些基本的概念和语法,包括如何定义变量、控制结构、函数以及如何使用Python Turtle的基本命令等。同时,需要根据前面制定的画样式和风格来编写代码,这需要对Python Turtle的各种基本命令有一定的了解和掌握。 第三步是开始画。在画的过程中,需要一步一步地编写代码,以确定下一个线条或形状的位置、方向和颜色等。这需要一定的创意和想象力,而且还需要耐心和坚持不懈地努力。Python Turtle提供了很多命令和参数,以便你能够创建各种各样的形状和线条,例如画直线、曲线、圆形、椭圆形、正方形、矩形等。 第四步是优化和调整你的画。对于复杂的画来说,你可能需要多次修改代码和调整参数,以达到最终的效果。这需要更多的练习和经验,以及对于Python语言和Python Turtle的更深入的理解和掌握。 Python Turtle是一个很好的绘工具,它能够帮助你创建各种各样的画,帮助你了解和学习编程的基础概念。但是,要想绘制出复杂的画,需要耐心和坚持不懈地努力,以及对编程语言的深入了解和掌握。 ### 回答3: PythonTurtle是一个用于通过Python语言编写并控制绘制的库,它能够实现简单至复杂的绘制,例如绘制花朵、星星、螺旋线等等。 要想绘制复杂画,我们首先需要了解PythonTurtle的基本知识和语法。在PythonTurtle中,我们可以使用turtle函数来控制海龟的移动和绘制形形状。例如: import turtle t = turtle.Turtle() t.circle(50) 上述代码可以在屏幕中央绘制一个半径为50的圆形。 在掌握了基本函数的使用后,我们可以开始尝试绘制复杂的画。以绘制一个太阳花为例,我们可以使用turtle函数来绘制每个花瓣、叶子、花蕊和花梗。代码示例如下: import turtle t = turtle.Turtle() t.speed(10) def draw_flower(): t.color("red", "yellow") t.begin_fill() while True: t.forward(200) t.left(170) if abs(t.pos()) < 1: break t.end_fill() def draw_stem(): t.right(90) t.forward(200) t.right(90) t.color("brown") t.width(10) t.forward(30) def draw_leaf(): t.width(1) t.color("green") t.begin_fill() t.left(45) t.forward(100) t.right(90) t.forward(100) t.right(45) t.forward(50) t.right(105) t.forward(70) t.end_fill() draw_flower() draw_stem() draw_leaf() t.hideturtle() turtle.done() 上述代码中,我们定义了三个函数分别用于绘制花瓣、花梗和叶子。最后在main函数中调用这些函数绘制整个太阳花。 除了太阳花外,还可以通过使用for循环来绘制更复杂的形,例如绘制一个由红、绿、蓝三种颜色组成的彩虹: import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] t = turtle.Turtle() t.speed(10) t.width(10) for i in range(6): t.color(colors[i]) t.forward(100) t.right(60) t.hideturtle() turtle.done() 上述代码中,我们首先定义了颜色列表和turtle对象,然后使用for循环循环绘制6段不同颜色的线段,最终形成了彩虹的形状。 绘制复杂画的关键在于对turtle函数和for循环的理解和掌握。只有通过实践不断探索,才能更好地发挥PythonTurtle的功能,创造出更加惊艳的形和艺术品。 ============================================ Python项目虚拟环境(超详细讲解):https://blog.csdn.net/weixin_50296259/article/details/130738875 资源下载合集:https://blog.csdn.net/weixin_50296259/article/details/130674430 Python GUI文章合集:https://blog.csdn.net/weixin_50296259/article/details/130674350 Oracle数据库文章合集:https://blog.csdn.net/weixin_50296259/article/details/130739064 Python pygame(GUI编程)模块最完整教程(6) Python-ZZY: 用游戏手柄的玩家没那么多 Python pygame(GUI编程)模块最完整教程(6) Py__12345: 我没有游戏手柄表情包,考虑到玩家,我该怎么办表情包???