1

我想在python中創建一個分形樹。我已經制作了樹,但我想要有2只或更多的海龜來一次繪製我的分形樹。有沒有辦法做到這一點?我尋找解決方案,但沒有一個是我真正想要的。這裏是我的代碼:如何在Python3中一次移動2個海龜

import turtle 
tree = turtle.Turtle() 
tree.ht() 
tree.penup() 
tree.sety(-200) 
tree.left(90) 

import turtle 
tree0 = turtle.Turtle() 
tree0.ht() 
tree0.penup() 
tree0.sety(-200) 
tree0.left(90) 

startx = tree.xcor() 
starty = tree.ycor() 
startx = tree0.xcor() 
starty = tree0.ycor() 

def fractalright(angle, length, x, y): 
    tree.speed(0) 
    tree.setx(x) 
    tree.sety(y) 
    tree.pendown() 
    tree.forward(length) 
    tree.right(angle) 
    length = length - 20 
    x = tree.xcor() 
    y = tree.ycor() 
    if length < 0: 
     return 
    tree.penup() 
    fractalright(angle, length, x, y) 
    tree.penup() 
    tree.setx(x) 
    tree.sety(y) 
    tree.left(angle) 
    fractalright (-angle, length, x, y) 
def fractalleft(angle, length, x, y): 
    tree0.speed(0) 
    tree0.setx(x) 
    tree0.sety(y) 
    tree0.pendown() 
    tree0.forward(length) 
    tree0.right(angle) 
    length = length - 20 
    x = tree0.xcor() 
    y = tree0.ycor() 
    if length < 0: 
     return 
    tree0.penup() 
    fractalleft(angle, length, x, y) 
    tree0.penup() 
    tree0.setx(x) 
    tree0.sety(y) 
    tree0.left(angle) 
    fractalleft (-angle, length, x, y) 

我使用python 3,請讓我知道如果你知道一個解決方案。謝謝!!

+0

你提到了[multiprocessing](https://docs.python.org/2/library/multiprocessing.html),你看過那個文檔,另一個選項是[threading](https:/ /docs.python.org/2/library/threading.html),我會建議看看他們,然後嘗試一個,然後如果你仍然有問題回來這些。 –

回答

0

使用模塊threadingturtle的關鍵是不能讓額外的線程來操縱海龜 - 他們,而不是排隊的龜請求,並讓他們的主線程處理:我們正在使用

import queue 
from threading import Thread, active_count 
from turtle import Turtle, Screen 

def forward(turtle, distance): 
    graphics.put((turtle.forward, distance)) 

def right(turtle, angle): 
    graphics.put((turtle.right, angle)) 

def left(turtle, angle): 
    graphics.put((turtle.left, angle)) 

def fractalright(turtle, angle, length): 
    forward(turtle, length) 

    if length - 20 > 0: 
     right(turtle, angle) 
     fractalright(turtle, angle, length - 20) 
     left(turtle, angle) 
     fractalright(turtle, -angle, length - 20) 

    forward(turtle, -length) 

def fractalleft(turtle, angle, length): 
    forward(turtle, length) 

    if length - 20 > 0: 
     left(turtle, angle) 
     fractalleft(turtle, angle, length - 20) 
     right(turtle, angle) 
     fractalleft(turtle, -angle, length - 20) 

    forward(turtle, -length) 

def process_queue(): 
    while not graphics.empty(): 
     action, argument = graphics.get() 
     action(argument) 

    if active_count() > 1: 
     screen.ontimer(process_queue, 100) 

START_X, START_Y = 0, -200 

screen = Screen() 
screen.mode('logo') # make starting direction 0 degrees towards top 

tree1 = Turtle(visible=False) 
tree1.color('green') 
tree1.penup() 
tree1.goto(START_X, START_Y) 
tree1.pendown() 

tree2 = Turtle(visible=False) 
tree2.color('dark green') 
tree2.penup() 
tree2.goto(START_X, START_Y) 
tree2.pendown() 

graphics = queue.Queue(1) # size = number of hardware threads you have - 1 

def fractal1(): 
    fractalright(tree1, 30, 100) 

def fractal2(): 
    fractalleft(tree2, 30, 100) 

thread1 = Thread(target=fractal1) 
thread1.daemon = True # thread dies when main thread (only non-daemon thread) exits. 
thread1.start() 

thread2 = Thread(target=fractal2) 
thread2.daemon = True # thread dies when main thread (only non-daemon thread) exits. 
thread2.start() 

process_queue() 

screen.exitonclick() 

用於線程安全通信的隊列模塊。我重寫了您的fractalright()fractalleft()函數,以儘量減少它們所需的各種圖形操作。

enter image description here

如果一切正常,你應該看到在同一時間獨立地繪製樹的淺綠色和深綠色的部分。您的計算機需要至少有幾個硬件線程可用。

1

蒸餾掉你如何繪製樹的細節,基本的任務是執行一些繪製函數的兩個實例並行使用不同的參數來指定一個「左樹」和一個「右樹」。這種基本結構可實現如下:

from multiprocessing.dummy import Pool 
import time 


def draw_function(current_tree): 
    # Replace the following lines with what you need to do with turtles 
    print("Drawing tree {}".format(current_tree)) 
    time.sleep(1) 


# Replace this with list of tuples of arguments to draw_function specifying 
# angle, length, x, y, left vs right, etc. 
list_of_trees = ["left_tree", "right_tree"] 

my_pool = Pool(2) 

results = my_pool.map(draw_function, list_of_trees) 

my_pool.close() 
my_pool.join() 

在你的情況,我對fractalleftfractalright之間的區別有點不清楚,因爲他們似乎是相同的,但邏輯應該成爲你的draw_function的基礎。您應該爲draw_function的每次執行創建一個單獨的烏龜,但請注意,您不需要重新導入烏龜。

+0

我有一個問題,你是什麼意思將它替換爲draw_function指定角度,長度,x,y,左,右等參數的元組列表。 –

+0

假設您的draw_function結果需要參數angle,length,x ,y和一個布爾值,指定它是「左」型樹還是「右」型樹。如果你有兩棵樹可以繪製,你可以創建一個由兩個元組組成的列表,每個元組包含其中一棵樹的適當參數。然後,draw_function將接受並分析該元組以確定如何繪製樹。可能更好的做法是讓draw_function接受一個自定義Tree類實例的單個參數,該實例指定繪製樹所需的所有內容。 – wphicks