def pic(root, visit, posi: bool, t: tt.Turtle, angle, length): if root.get('value', None) is not None: draw(root, t, posi, angle, length) pic(root['leftChild'], visit, RIGHT, t.clone(), angle - 10, length - 10) pic(root['rightChild'], visit, LEFT, t.clone(), angle - 10, length - 10)
def make_field(): global bars bar = Turtle() bar.penup() for i in range(2, 6): bar.shape(f"gif/v{i}.gif") for j in range(1, 11): bar.setpos(-400 + (j * 70), -100 + (i * 80)) a = bar.clone() bars.append(a) bar.stamp() j += 1 bars.append(bar)
def start(n): plist = [] angle = 0 p = Turtle() p.hideturtle() p.getscreen().tracer(30, 0) p.setundobuffer(None) plist.append(p) for i in range(n): q = p.clone() q.right(angle) plist.append(q) angle += (360 / n) star(plist)
def generate_stars(): t = Turtle(visible=False, shape='circle') t.pencolor("white") t.fillcolor("white") t.penup() t.setheading(-90) t.goto(width / 2, randint(-height / 2, height / 2)) stars = [] colors = ['yellow', 'white', 'pink'] for _ in range(125): star = t.clone() s = random() / 3 star.shapesize(s, s) star.speed(int(s * 8)) star.setx(width / 2 + randint(1, width)) star.sety(randint(-height / 2, height / 2)) star.color(colors[randint(0, 2)], colors[randint(0, 2)]) star.begin_fill() star.showturtle() star.end_fill() stars.append(star) return stars
class Plotter: COLORS = ["red", "green", "blue", "magenta", "orange", "cyan"] def __init__(self, size, pos, ranges, graphs, step): self.width = size[0] - 30 self.height = size[1] - 30 self.min = ranges[0] self.max = ranges[1] self.x = pos[0] + 30 self.y = pos[1] self.graphs = graphs self.pens = [] self.i = self.width self.step = step self.lastTime = -1 for i in range(graphs): pen = Turtle() pen.up() pen.speed(0) pen.color(self.COLORS[i]) pen.width(1) pen.hideturtle() self.pens.append(pen) guiPen = Turtle() guiPen.up() guiPen.getscreen().tracer(0, 0) guiPen.hideturtle() guiPen.speed(0) guiPen.color("black") guiPen.setpos(self.x, self.y) guiPen.down() guiPen.setpos(self.x + self.width, self.y) guiPen.setpos(self.x + self.width, self.y + self.height) guiPen.setpos(self.x, self.y + self.height) guiPen.setpos(self.x, self.y) guiPen.up() for i in range(int(self.width / 2)): guiPen.setpos(self.x + i * 2 + 1, self.y + self.height / 2) guiPen.dot(2) for i in range(int(self.width / 4)): guiPen.setpos(self.x + i * 4 + 2, self.y + self.height * 0.75) guiPen.dot(2) for i in range(int(self.width / 4)): guiPen.setpos(self.x + i * 4 + 2, self.y + self.height * 0.25) guiPen.dot(2) for i in range(5): guiPen.setpos(self.x - 2, self.y + self.height * i / 4 - 7) guiPen.write(str((self.max - self.min) * i / 4 + self.min), align="right") guiPen.getscreen().tracer(1, 0) self.numPen = Turtle() self.numPen.up() self.numPen.hideturtle() self.numPen.speed(0) self.numPen = [self.numPen, self.numPen.clone()] def plot(self, data): sets = min(len(data), self.graphs) self.i = self.i + int(self.step * (time.time() * 1000 - self.lastTime) / 50) self.lastTime = time.time() * 1000 if (self.i > self.width): # reset self.i = 0 for i in range(sets): self.pens[i].clear() self.pens[i].up() self.pens[i].setpos(self.getCoordinates(data[i])) self.pens[i].down() for i in range(sets): self.pens[i].setpos(self.getCoordinates(data[i])) numPen = self.numPen[int(self.i / self.step % 2)] numPen.setpos(self.x + self.width, self.y + self.height - i * 20 - 20) numPen.color(self.COLORS[i]) numPen.write(int(data[i]), align="right") self.numPen[int(self.i / self.step + 1) % 2].clear() # fancy double buffering def getCoordinates(self, value): return (self.i + self.x, (0.0 + value - self.min) / (self.max - self.min) * self.height + self.y)
class Plotter2D: COLORS = ["red", "green", "blue", "magenta", "orange", "cyan"] def __init__(self, size, pos, ranges, graphs, step): self.width = size[0] - 10 self.height = size[1] - 10 self.min = ranges[0] self.max = ranges[1] self.x = pos[0] self.y = pos[1] self.graphs = graphs self.pens = [] self.i = self.width self.step = step self.lastTime = -1 for i in range(graphs): pen = Turtle() pen.up() pen.speed(0) pen.color(self.COLORS[i]) pen.width(1) pen.hideturtle() self.pens.append(pen) guiPen = Turtle() guiPen.up() guiPen.getscreen().tracer(0, 0) guiPen.hideturtle() guiPen.speed(0) guiPen.color("black") guiPen.setpos(self.x, self.y) guiPen.down() guiPen.setpos(self.x - self.width / 2, self.y) guiPen.setpos(self.x + self.width / 2 + 10, self.y) guiPen.setpos(self.x + self.width / 2 + 5, self.y - 5) guiPen.setpos(self.x + self.width / 2 + 10, self.y) guiPen.setpos(self.x + self.width / 2 + 5, self.y + 5) guiPen.up() guiPen.setpos(self.x, self.y - self.height / 2) guiPen.down() guiPen.setpos(self.x, self.y + 10 + self.height / 2) guiPen.setpos(self.x + 5, self.y + 5 + self.height / 2) guiPen.setpos(self.x, self.y + 10 + self.height / 2) guiPen.setpos(self.x - 5, self.y + 5 + self.height / 2) guiPen.up() for i in range(5): guiPen.setpos(self.x - self.width / 2 + i / 4 * self.width, self.y - 5) guiPen.down() guiPen.setpos(self.x - self.width / 2 + i / 4 * self.width, self.y + 5) guiPen.up() guiPen.write(str((self.max - self.min) * i / 4 + self.min), align="center") guiPen.setpos(self.x - 5, self.y - self.height / 2 + i / 4 * self.height) guiPen.down() guiPen.setpos(self.x + 5, self.y - self.height / 2 + i / 4 * self.height) guiPen.up() guiPen.setpos(self.x + 10, self.y - self.height / 2 + i / 4 * self.height - 7) guiPen.write(str((self.max - self.min) * i / 4 + self.min), align="left") guiPen.getscreen().tracer(1, 0) self.numPen = Turtle() self.numPen.up() self.numPen.hideturtle() self.numPen.speed(0) self.numPen = [self.numPen, self.numPen.clone()] def clear(self): self.i = 0 for i in range(len(self.pens)): self.pens[i].clear() def plot(self, data): sets = min(len(data), self.graphs) self.i = self.i + int(self.step * (time.time() * 1000 - self.lastTime) / 50) self.lastTime = time.time() * 1000 for i in range(sets): self.pens[i].setpos(self.getCoordinates(data[i][0], data[i][1])) self.pens[i].dot(3) # numPen = self.numPen[self.i/self.step % 2] # numPen.setpos(self.x + self.width, self.y + # self.height - i * 20-20) # numPen.color(self.COLORS[i]) # numPen.write(str(data[i]), align="right") # self.numPen[(self.i/self.step + 1) % # 2].clear() # fancy double buffering def getCoordinates(self, valuex, valuey): return ((0.0 + valuex - self.min) / (self.max - self.min) * self.width - 0.5 * self.width + self.x, (0.0 + valuey - self.min) / (self.max - self.min) * self.height + self.y - 0.5 * self.height)
screen.register_shape(shape, p) t.penup() t.sety(len(turtles)*gap) t.shape(shape) turtles.append(t) import os file_path = os.path.realpath(__file__) dir_path = os.path.dirname(file_path) shape = os.path.join(dir_path, 'b.gif') screen.register_shape(shape) t = Turtle() t.penup() t.sety(len(turtles)*gap) t.shape(shape) turtles.append(t) new_turtles = [] for turtle in turtles: new_turtle = turtle.clone() new_turtle.setx(step) new_turtle.pendown() new_turtle.stamp() new_turtle.forward(step*2) new_turtle.shapesize(2, 2, 2) new_turtle.stamp() new_turtle.forward(step*2) new_turtle.shearfactor(1) screen.exitonclick()
def save_turtle(cls, turtle: Turtle): clone: Turtle = turtle.clone() clone.speed("fastest") clone.hideturtle() yield clone
from random import randint, choice from turtle import Turtle, Screen import time screen = Screen() screen.tracer(0) width, height = screen.window_width() - 30, screen.window_height( ) - 30 # -30 to account for window borders, etc. maker1 = Turtle(visible=False) maker2 = maker1.clone() maker3 = maker1.clone() maker4 = maker1.clone() maker1.goto(50, 50) # top right maker2.goto(50, -50) # bottom right maker3.goto(-50, -50) # bottom left maker4.goto(-50, 50) # top left n = randint(1, 360) m = randint(0, 100) r = randint(0, 50) d = choice([True, False]) def out_of_bounds(turtle): return not (-width // 2 < turtle.xcor() < width // 2 and -height // 2 < turtle.ycor() < height // 2)
""" 第15题答案的python演示代码: 导入turtle模块 你只要知道turtle是python的一个图形界面库(工具箱), 方便你在电脑屏幕上生成一个图形化的程序界面。 """ from turtle import Screen, Turtle screen = Screen() # screen.addshape("./t.gif") t = Turtle() # t.shape("./t.gif") x, y = 0, 0 for i in range(4): for j in range(3): t = t.clone() y -= 100 t.sety(y) x += 100 y = 0 t.setx(x) t.sety(y) t.hideturtle() screen.mainloop()
def preorder(root,visit,posi:bool,t:tt.Turtle,angle): if root.get('value',None) is not None: visit(root,t,posi,angle) preorder(root['leftChild'],visit,LEFT,t.clone(),angle-8) preorder(root['rightChild'],visit,RIGHT,t.clone(),angle-8)
from turtle import Turtle from random import randint import tkinter as _ pic = Turtle() pic.speed(1) bic = pic.clone() bic.speed(40) sic = bic.clone() sic.speed(40) kic = sic.clone() kic.speed(20) while True: colorid = round(randint(100000, 900000)) #get a random color using color id colorid = str(colorid) colors = ['white', '#00CED1'] WIDTH, LENGTH = 25, 125 pic.width(WIDTH) #pic.hideturtle() pic.color("#" + colorid) pic.left(180) pic.backward(90) pic.left(90) pic.forward(90) pic.right(90)
#!/usr/bin/env python # -*- coding: utf-8 -*- from turtle import Turtle, Screen screen = Screen() base_turtle = Turtle() base_turtle.speed(0) for i in range(1,9): new_tut = base_turtle.clone() new_tut.left(45*i) new_tut.forward(200) print("theta: ", round(new_tut.heading()) ,"rad: ", round(new_tut.distance(base_turtle))) screen.mainloop()
# import turtleee from turtle import Screen, Turtle screen = Screen() turtle = Turtle() turtle.speed(5) turtle.shape("turtle") num_sides = 12 side_size = 40 angle = 360 / 12 for side in range(num_sides): turtle.forward(side_size) turtle.clone() turtle.left(180) turtle.forward(side_size) turtle.left(180 - angle) screen.mainloop()
Нажмите Escape для выхода -------------------------- """ from turtle import Turtle, Screen clr = (64, 64, 64) text_list = ['R', 'G', 'B', '', 90, -60, -210, 250] cl_list = ['red', 'green', 'blue', 150, 0, -150] screen = Screen() screen.delay(0) screen.title('Миксер цветов') screen.colormode(255) cl_r = Turtle(shape='circle') cl_g = cl_r.clone() cl_b = cl_r.clone() for t, turtle in enumerate(screen.turtles()): turtle.speed(0) turtle.shapesize(3, 3, 5) turtle.color(cl_list[t]) turtle.width(10) turtle.pu() turtle.setpos(-255, cl_list[3 + t]) turtle.pd() turtle.setx(255) turtle.pu() turtle.setx(0) turtle.pencolor(clr)