Python turtle 模块,goto() 实例源码
我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用turtle.goto()。
def draw_canvas(state):
'''Draw all the cities in the current state in a canvas. Indicate the start
city with a description and the current city by the turtle pointer head
'''
turtle.clear()
turtle.hideturtle()
turtle.up()
turtle.pencolor("blue")
current_city = state.current_city
for city in state.cities:
x = city.position[0]
y = city.position[1]
turtle.goto(x, y)
if city.is_start:
turtle.write('{}, Start'.format(city.name), align="center", font=("Arial", 12, "bold"))
elif city == current_city:
turtle.write('{}, Current'.format(city.name), align="center", font=("Arial", 12, "bold"))
else:
turtle.write('{}'.format(city.name), align="center", font=("Arial", 12, "bold"))
turtle.goto(current_city.position[0], current_city.position[1])
def draw(self):
"draws the lsystem on the screen"
stack = []
tt.penup()
tt.setpos(0, -200)
tt.seth(90)
tt.pendown()
print "Drawing the lsystem ..."
for i, codebit in enumerate(self.generation[-1]):
if codebit in ['F', 'A', 'B']:
tt.forward(self.length)
print '[ FRWD ] ', codebit
elif codebit == '+':
tt.right(self.angle)
print '[ RGHT ] ', codebit
elif codebit == '-':
tt.left(self.angle)
print '[ LEFT ] ', codebit
elif codebit == '[':
stack.append((tt.pos(), tt.heading()))
print '[ PUSH ] ', (tt.pos(), tt.heading())
elif codebit == ']':
position,heading = stack.pop()
print '[ POP ] ', (position, heading)
tt.penup()
tt.goto(position)
tt.seth(heading)
tt.pendown()
else:
print '[ NOP ] ', codebit
if self.save_every_frame:
self.save(frame=i)
print "Done drawing"
print "Saving file as %s.jpg" % self.name,
self.save()
print "Done"
def line(x1, y1, x2, y2):
t.up()
t.setx(x1)
t.sety(y1)
t.down()
t.goto(x2, y2)
# print ("Line drawn from (",x1,", ",y1,") to (",x2,", ",y2,").")
def line(x1, y1, x2, y2):
t.up()
t.setx(x1)
t.sety(y1)
t.down()
t.goto(x2, y2)
# print ("Line drawn from (",x1,", ",y1,") to (",x2,", ",y2,").")
def draw_final_path(state):
'''Draw the TSP path given the cities in the correct order'''
if not state:
return None
states = []
while state:
states.append(state)
state = state.parent
cities = [state.current_city for state in states]
rest = deepcopy(cities)
rest = rest[1:]
rest.reverse()
cities = [cities[0]] + rest
turtle.clear()
turtle.hideturtle()
turtle.up()
turtle.pensize(1)
for city in cities:
x = city.position[0]
y = city.position[1]
turtle.pencolor("red")
turtle.goto(x, y)
turtle.pencolor("black")
if city.is_start:
turtle.write('{}-Start'.format(city.name), align="center", font=("Arial", 11, "bold"))
else:
turtle.write('{}'.format(city.name), align="center", font=("Arial", 11, "bold"))
turtle.down()
turtle.pencolor("red")
turtle.goto(cities[0].position[0], cities[0].position[1])
def draw_star(x, y, side):
star_angle = 360.0/6
left_angle = star_angle * 2
tu.penup()
tu.goto(x, y)
tu.pendown()
for i in range(6):
tu.forward(side)
tu.right(star_angle)
tu.forward(side)
tu.left(left_angle)
def draw_round_world():
init()
turtle.goto(0, 300)
turtle.hideturtle()
draw_world(5)
def draw_floor(floornum: int):
global current_floor
current_floor = floornum
node_coords = utils.get_node_coords(utils.Direction.left)
if floornum == len(full_floors):
rooms = []
# All edges with rooms
print('Showing everything')
edges = chain.from_iterable(chain(((True, e) for e in edges[0]), ((False, e) for e in edges[1])) for _, edges in full_floors)
# Edges with elevation changes
#edges = set(chain.from_iterable((edge(v, n) for n in b.values() if set(['u', 'd']) & set(utils.get_graph().edgedata[edge(v, n)]['rooms'])) for v, b in enumerate(utils.get_graph().branches)))
# All edges
#...
elif floornum >= 0 and floornum < len(full_floors):
rooms, edges = full_floors[floornum]
print(edges)
edges = chain(((True, e) for e in edges[0]), ((False, e) for e in edges[1]))
print(rooms)
else:
return
turtle.showturtle()
turtle.speed(0)
turtle.clear()
written_nodes = set()
for edge_dir, (a, b) in edges:
turtle.penup()
x, y, _ = node_coords[a]
turtle.goto(x / SHRINK + X_OFFSET, y / SHRINK + Y_OFFSET)
if a not in written_nodes:
turtle.write(a)
written_nodes.add(a)
turtle.pendown()
if edge_dir:
if edge_lengths[edge(a, b)] <= 0:
turtle.pencolor('red')
else:
turtle.pencolor('black')
else:
if edge_lengths[edge(a, b)] <= 0:
turtle.pencolor('blue')
else:
turtle.pencolor('green')
x, y, _ = node_coords[b]
turtle.goto(x / SHRINK + X_OFFSET, y / SHRINK + Y_OFFSET)
turtle.pencolor('black')
if b not in written_nodes:
turtle.write(b)
written_nodes.add(b)
turtle.hideturtle()
turtle.done()