2016-11-16 591 views
0

是否可以在視覺上移動已經繪製的圖像? 每次移動它都不必重繪它? 因此,顯示廣場完好無損並朝某個方向移動。移動已經繪製的圖像python turtle

from turtle import * 
def drawing(): 
    forward(50) 
    left(90) 
    forward(50) 
    left(90) 
    forward(50) 
    left(90) 
    forward(50) 
drawing() 
done() 
+0

你不能用龜來移動它。你必須重繪它。 'Turtle'使用'Tkinter',它使用'Canvas'功能'move'在畫布上移動對象,但是很難訪問畫布並訪問畫布上的對象。 – furas

回答

1

turtle沒有移動所有元素的特殊功能。

但是turtle使用tkinter它使用Canvas對象來顯示元素。
Canvas具有獲取所有顯示元素並移動它們的功能。

turtle可以訪問畫布(get_canvas()),但後來您必須知道tkinter才能使用畫布和元素進行操作。

本示例繪製元素,然後移動(300, 50)
您也可以點擊烏龜並拖動它來移動所有元素。

import turtle 

t = turtle.Turtle() 

# --- move all elements --- 

def move(offset_x, offset_y): 

    canvas = turtle.getcanvas() # `turtle`, not `t` 
    for element_id in canvas.find_all(): 
     canvas.move(element_id, offset_x, offset_y) 

# --- move all on draging turtle --- 

old_x = 0 
old_y = 0 

# get mouse current position 
def on_click(x, y): 
    global old_x, old_y 

    old_x = x 
    old_y = y 

# move elements 
def on_drag(x, y): 
    global old_x, old_y 

    move(x-old_x, old_y-y) 

    old_x = x 
    old_y = y 

t.onclick(on_click) 
t.ondrag(on_drag) 

# --- example --- 

# draw something 

for a in range(8): 
    for _ in range(8): 
     t.left(45) 
     t.fd(20) 
    t.right(45) 
    t.up() 
    t.fd(60) 
    t.down() 

# move 

move(300, 50) 

# --- 

turtle.done() # `turtle`, not `t` 
0

你可以用龜移動它沒有下降到了tkinter水平,如果你需要移動很簡單,填充多邊形的集合(儘管你可以用白色填充顏色與非模仿的未填充的多邊形)你可以用你的形狀創建一個自定義的烏龜,並在屏幕上移動龜:

from turtle import Turtle, Screen, Shape 

screen = Screen() 

turtle = Turtle(visible=False) 
turtle.speed("fastest") 
turtle.penup() 

shape = Shape("compound") 

for octogon in range(8): 
    turtle.begin_poly() 
    for _ in range(8): 
     turtle.left(45) 
     turtle.fd(20) 
    turtle.end_poly() 
    shape.addcomponent(turtle.get_poly(), "blue", "red") 

    turtle.right(45) 
    turtle.fd(60) 

screen.register_shape("octogons", shape) 

octopus = Turtle(shape="octogons") 
octopus.penup() 
octopus.speed("slowest") 

octopus.goto(300, 200) 
octopus.goto(-200, 200) 

screen.exitonclick()