简体   繁体   English

Python Turtle:我想制作一个函数,该函数可以根据先前执行的函数执行另一个函数

[英]Python Turtle: I want to make a function that executes another function depending on what the previously executed function was

I'm drawing my name using turtles and I have a bunch of different functions for each of the letters 我正在用乌龟画我的名字,每个字母都有很多不同的功能

like so (for letter r) 像这样(对于字母r)

def letter_r(t):

    def letter_r_top(t):
        turtle.lt(90)
        turtle.fd(150)
        turtle.rt(90)
        turtle.circle(-37.5,180)
        turtle.lt(130)

    def letter_r_leg(t):
        csquare = ((75**2) + (37.5**2)) 
        sidec = math.sqrt(csquare)
        turtle.fd(sidec)

    letter_r_top(rob)
    letter_r_leg(rob)

After each letter, i need to move the turtle into the right place to setup for the next letter. 每个字母之后,我需要将乌龟移到正确的位置以设置下一个字母。 Because each of the letters are different sizes I need to make custom movements depending on what the previous letter is but I dont want to make separate functions for each of those movements. 因为每个字母的大小都不同,所以我需要根据上一个字母进行自定义动作,但是我不想为每个动作分别设置功能。

At the end of my code I have the list of functions to be called in the correct order to spell my name 在代码的末尾,我具有正确拼写我的名字的要调用的函数列表

 letter_t(rob)
 letter_setup(rob)
 letter_r(rob)
 letter_setup(rob)
 .....

Is there a way that I can do something like this so that I will only need 1 setup function.(Not real code, just a conceptualization of what I'm thinking 有没有办法我可以做这样的事情,所以我只需要1个设置函数(不是真正的代码,只是我在想什么的概念化

def letter_setup(t):
    if previously executed function A 
        turtle.fd(75)
    if previously executed function B 
        turtle.fd(75)
        turtle.lt(90)
    if previously executed function C
        turtle.fd(75)
        turtle.lt(90)   

为什么不在上一个字母的末尾移动到下一个字母的正确位置?

Perhaps there is a better way to do this but you could make a variable last_function_called and in each function you give it a different value then you'll know wich one was the last called : 也许有一种更好的方法可以执行此操作,但是您可以使变量last_function_called,并且在每个函数中给它一个不同的值,然后您将知道最后一个被叫到:

last_function_called = NONE;

def function1():
    last_function_called = FUNCTION1
    blablabla

...

if (last_function_called == FUNCTION1):
    call another one

and before of course something like : 当然之前是这样的:

NONE = 0
FUNCTION1 = 1
FUNCTION2 = 2
ect...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM