我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用turtle.Turtle()。
def main(): window = turtle.Screen() # creat a screen window.bgcolor("blue") lucy = turtle.Turtle() lucy.shape("turtle") lucy.color("red") lucy.width(5) lucy.speed(0) # Drawing flower flower(lucy, 10, 40, 100, 360) # Drawing pedicel lucy.color("brown") lucy.rt(90) lucy.fd(200) # Drawing leaf lucy.rt(270) lucy.color("green") leaf(lucy, 40, 80, 180) lucy.ht() window.exitonclick() # call the main function to start!
def main(): p = Turtle() p.ht() tracer(75,0) u = doit1(6, Turtle(undobuffersize=1)) s = doit2(7, Turtle(undobuffersize=1)) t = doit3(5, Turtle(undobuffersize=1)) a = clock() while True: done = 0 for b in u,s,t: try: b.__next__() except: done += 1 if done == 3: break tracer(1,10) b = clock() return "runtime: %.2f sec." % (b-a)
def __init__(self, game): self.game = game self.screen = game.screen self.model = game.model self.screen.colormode(255) self.screen.tracer(False) self.screen.bgcolor((240, 240, 255)) self.writer = turtle.Turtle(visible=False) self.writer.pu() self.writer.speed(0) self.sticks = {} for row in range(3): for col in range(MAXSTICKS): self.sticks[(row, col)] = Stick(row, col, game) self.display("... a moment please ...") self.screen.tracer(True)
def __init__(self, text, color, x = 0, y = 0, font_name = "Arial", font_size = 12, font_type = "normal", align = "left"): turtle.Turtle.__init__(self) self.hideturtle() self.penup() self.goto(x, y) self.color(color) self.font = (font_name, font_size, font_type) self.align = align # Attributes self.text = text # Append to master label list Game.labels.append(self)
def main(): windows = turtle.Screen()#???? windows.bgcolor('blue')#???? bran = turtle.Turtle()#???? bran.shape('turtle') bran.color('yellow') bran.speed(2)#???? for i in range(1,5):#???? bran.forward(100) bran.right(45) bran.forward(100) bran.right(45) bran.forward(100) bran.right(45) bran.forward(100) bran.right(45)
def main(): #?????? windows=turtle.Screen() #???? windows.bgcolor('pink') #??????? bran=turtle.Turtle()#???T????? bran.shape('turtle') bran.color('purple') #???? bran.speed(1) for i in range(1,20): bran.forward(100) bran.left(72) bran.forward(100) bran.right(144)
def main(): #?????? windows = turtle.Screen() #???? windows.bgcolor('blue') #???????? bran = turtle.Turtle() bran.shape('turtle') bran.color('yellow') #???? bran.speed(1) #??? for i in range(1,10): bran.forward(100) bran.right(90) bran.forward(150) bran.right(90) bran.forward(100) bran.right(90) bran.forward(150) bran.right(90) #???
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)
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 "Laufzeit: %.3f sec" % (z1+et-at)
def drawtree(root): def height(root): return 1 + max(height(root.left), height(root.right)) if root else -1 def jumpto(x, y): t.penup() t.goto(x, y) t.pendown() def draw(node, x, y, dx): if node: t.goto(x, y) jumpto(x, y-20) t.write(node.val, align='center', font=('Arial', 12, 'normal')) draw(node.left, x-dx, y-60, dx/2) jumpto(x, y-20) draw(node.right, x+dx, y-60, dx/2) import turtle t = turtle.Turtle() t.speed(0); turtle.delay(0) h = height(root) jumpto(0, 30*h) draw(root, 0, 30*h, 40*h) t.hideturtle() turtle.mainloop()
def draw_square(): window = turtle.Screen() window.bgcolor("red") brad = turtle.Turtle() brad.shape("turtle") brad.color("yellow") brad.speed(5) counter = 0 while counter < 36: brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(100) counter += 1 window.exitonclick()
def Init(): global secHand, minHand, hurHand, printer turtle.mode("logo") # ??Turtle??? # ??????Turtle???? mkHand("secHand", 125) mkHand("minHand", 130) mkHand("hurHand", 90) secHand = turtle.Turtle() secHand.shape("secHand") minHand = turtle.Turtle() minHand.shape("minHand") hurHand = turtle.Turtle() hurHand.shape("hurHand") for hand in secHand, minHand, hurHand: hand.shapesize(1, 1, 3) hand.speed(0) # ??????Turtle printer = turtle.Turtle() printer.hideturtle() printer.penup()
def main(): tList = [] head = 0 numTurtles = 10 wn = turtle.Screen() wn.setup(500,500) for i in range(numTurtles): nt = turtle.Turtle() # Make a new turtle, initialize values nt.setheading(head) nt.pensize(2) nt.color(random.randrange(256),random.randrange(256),random.randrange(256)) nt.speed(10) wn.tracer(30,0) tList.append(nt) # Add the new turtle to the list head = head + 360/numTurtles for i in range(100): moveTurtles(tList,15,i) w = tList[0] w.up() w.goto(0,40) w.write("How to Think Like a ",True,"center","40pt Bold") w.goto(0,-35) w.write("Computer Scientist",True,"center","40pt Bold")
def main(): # windows=turtle.Screen() # windows.bgcolor('blue') bran=turtle.Turtle() bran.shape('classic') bran.color('black') bran.speed(2) while True: # bran.forward(10) bran.down() bran.right(60) bran.circle(80) # bran.forward(10) # bran2.left(30) # bran2.goto(0,0) # bran2.forward(10)
def lose(): state.clear() state.color("white") state.write("YOU LOSE!!", font=("Arial",50,"bold")) cordxy=letter[0].pos() temp=turtle.Turtle() temp.ht() temp.speed(0) temp.color("white") temp.penup() temp.setpos(cordxy[0],cordxy[1]+40) temp.pendown() temp.write("This was the movie:",align="left", font=("Arial", 30, "bold")) for i in range(len(movie)): if(movie[i] not in user_list_correct): letter[i].color("red") letter[i].write(movie[i],align="left", font=("Arial", 20, "bold"))
def arc(t, r, angle): """Draws an arc with the given radius and angle. t: Turtle r: radius angle: angle subtended by the arc, in degrees """ arc_length = 2 * math.pi * r * abs(angle) / 360 n = int(arc_length / 4) + 1 step_length = arc_length / n step_angle = float(angle) / n # making a slight left turn before starting reduces # the error caused by the linear approximation of the arc t.lt(step_angle/2) polyline(t, n, step_length, step_angle) t.rt(step_angle/2)
def isosceles(t, r, angle): """Draws an icosceles triangle. The turtle starts and ends at the peak, facing the middle of the base. t: Turtle r: length of the equal legs angle: peak angle in degrees """ y = r * math.sin(angle * math.pi / 180) t.rt(angle) t.fd(r) t.lt(90+angle) t.fd(2*y) t.lt(90+angle) t.fd(r) t.lt(180-angle)
def myInitialize(): #wn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) alex.right(200) alex.write("Initialization done.") print ("Initialization done.")
def drawNodeGraphTurtle(g): 'Draws node map with turtle graphics.' alex = turtle.Turtle() alex.rt(30) #For every node: draw it. for n in g: alex.forward(25) alex.write(n[1]) print ("Initialization done.")
def __init__(self): turtle.Turtle.__init__(self) # first initialize MazeTurtle as a Turtle self.penup() self.shape("turtle") # new properties specific to MazeTurtles self.cell = Cell(0,0) self.dir = STAND self.score = 0 self.lives = 3
def __init__(self, *args, **kwargs): from turtle import Turtle self.turtle = Turtle() super().__init__(*args, **kwargs)
def make_turtle_namespace(): """ Returns a dictionary with the namespace of turtle functions. """ return dict(TurtleNamespace(Turtle))
def __init__( self, screen_width = 800, screen_height = 600, background_color = "black", title = "Simple Game Library by /u/wynand1004 AKA @TokyoEdTech", splash_time = 3): # Setup using Turtle module methods turtle.setup(width=screen_width, height=screen_height) turtle.bgcolor(background_color) turtle.title(title) turtle.tracer(0) # Stop automatic screen refresh turtle.listen() # Listen for keyboard input turtle.hideturtle() # Hides default turtle turtle.penup() # Puts pen up for defaut turtle turtle.setundobuffer(0) # Do not keep turtle history in memory turtle.onscreenclick(self.click) # Game Attributes self.FPS = 30.0 # Lower this on slower computers or with large number of sprites self.SCREEN_WIDTH = screen_width self.SCREEN_HEIGHT = screen_height self.DATAFILE = "game.dat" self.SPLASHFILE = "splash.gif" # Must be in the same folder as game file self.title = title self.gravity = 0 self.state = "showsplash" self.splash_time = splash_time self.time = time.time() # Clear the terminal and print the game title self.clear_terminal_screen() print (self.title) # Show splash self.show_splash(self.splash_time) # Pop ups
def __init__(self, shape, color, x = 0, y = 0): turtle.Turtle.__init__(self) # self.hideturtle() self.penup() # Register shape if it is a .gif file if shape.endswith(".gif"): try: turtle.register_shape(shape) except: Game.logs.append("Warning: {} file missing from disk.".format(shape)) # Set placeholder shape shape = "square" self.shape(shape) self.color(color) self.goto(x, y) #Set click binding self.onclick(self.click) # Append to master button list Game.buttons.append(self)
def draw_circle_with_square(): purple_turtle = turtle.Turtle() purple_turtle.shape("circle") purple_turtle.speed(9) purple_turtle.pensize(3) purple_turtle.hideturtle() for i in range(1,37): if i % 2 == 0: purple_turtle.color("white") else: purple_turtle.color("#732C7B") draw_square(purple_turtle) purple_turtle.right(10) #def draw_circle(): # turtle_two = turtle.Turtle() # turtle_two.shape("arrow") # turtle_two.color("white") # turtle_two.circle(100) # #def draw_triangle(): # turtle_three = turtle.Turtle() # turtle_three.shape("turtle") # turtle_three.color("black") # turtle_three.backward(100) # turtle_three.left(60) # turtle_three.forward(100) # turtle_three.right(120) # turtle_three.forward(100)
def Turtle_Mini_Project_KC(): window = turtle.Screen() window.bgcolor("white") brad = turtle.Turtle() brad.shape("turtle") brad.color("blue") brad.speed(1) draw_K(brad) draw_C(brad) window.exitonclick()
def main(): #?????? windows = turtle.Screen() #???? windows.bgcolor('blue') #???????? bran = turtle.Turtle() bran.shape('turtle') bran.color('yellow') #?????? for i in range(1,3): bran.setposition(300,300) bran.setheading(90) bran.setposition(300,0) bran.setheading(90) bran.setposition(0,0) bran.setheading(90) bran.dot(60,'red') bran.color('black') bran.stamp() bran.forward(-300) bran.right(90) bran.forward(300) bran.left(90) bran.forward(300) bran.clearstamps() for i in range(5): bran.undo()
def main(): #?????? windows = turtle.Screen() #???? windows.bgcolor('blue') #???????? bran = turtle.Turtle() bran.shape('turtle') bran.color('yellow') #??????
def __init__(self, row, col, game): turtle.Turtle.__init__(self, visible=False) self.row = row self.col = col self.game = game x, y = self.coords(row, col) self.shape("square") self.shapesize(HUNIT/10.0, WUNIT/20.0) self.speed(0) self.pu() self.goto(x,y) self.color("white") self.showturtle()
def maketree(): p = Turtle() p.setundobuffer(None) p.hideturtle() p.speed(0) p.getscreen().tracer(30,0) p.left(90) p.penup() p.forward(-210) p.pendown() t = tree([p], 200, 65, 0.6375) for x in t: pass print(len(p.getscreen().turtles()))
def square(): win = turtle.Screen() win.bgcolor("white") jack = turtle.Turtle() for x in range(1,5): jack.forward(100) jack.right(90) win.exitonclick()
def maketree(): p = Turtle() p.setundobuffer(None) p.hideturtle() p.speed(0) p.tracer(30,0) p.left(90) p.penup() p.forward(-210) p.pendown() t = tree([p], 200, 65, 0.6375) for x in t: pass print len(p.getscreen().turtles())
def main(): my_turtle = turtle.Turtle() my_win = turtle.Screen() my_points = [[-100, -50], [0, 100], [100, -50]] sierpinski(my_points, 3, my_turtle) my_win.exitonclick()
def visualize(lines): import turtle wn = turtle.Screen() t = turtle.Turtle() t.speed(0) t.pencolor('red') t.pd() for i in range(0,len(lines)): for p in lines[i]: t.goto(p[0]*640/1024-320,-(p[1]*640/1024-320)) t.pencolor('black') t.pencolor('red') turtle.mainloop()
def mkHand(name, length): # ??Turtle???????Turtle turtle.reset() Skip(-length * 0.1) turtle.begin_poly() turtle.forward(length * 1.1) turtle.end_poly() handForm = turtle.get_poly() turtle.register_shape(name, handForm)
def drawtree(root): def height(root): return 1 + max(height(root.left), height(root.right)) if root else -1 def jumpto(x, y): t.penup() t.goto(x, y) t.pendown() def draw(node, x, y, dx): if node: t.goto(x, y) jumpto(x, y - 20) t.write(node.val, align='center', font=('Arial', 12, 'normal')) draw(node.left, x - dx, y - 60, dx / 2) jumpto(x, y - 20) draw(node.right, x + dx, y - 60, dx / 2) import turtle t = turtle.Turtle() t.speed(0) turtle.delay(0) h = height(root) jumpto(0, 30 * h) draw(root, 0, 30 * h, 40 * h) t.hideturtle() turtle.mainloop()