0

我决定根据收到的建议重做这个问题,这是我在大学一年级 Python 编码中得到的一个作业问题。我的代码中有错误,不知道在哪里修复它们。BUG 1 程序运行时乌龟开始绘图,即使笔已启动。BUG 2 未定义键如's, 7, tab' 触发空格键功能

图画书

在此任务中,您将创建一个儿童填色游戏,其中给定的图片可以通过围绕一个形状进行描边然后填充它来着色。控制如下。

箭头键 - 将“画笔”(乌龟光标)向左、向右、向上或向下移动固定的少量。

'z' - 撤消最后一步。

'r'、'g'、'b' - 将画笔颜色分别更改为红色、绿色或蓝色。(如果您愿意,可以定义更多颜色,但我们希望至少有这三种。)

空格键 - 切换绘画模式。在初始模式“移动”模式下,“画笔”(乌龟)在屏幕周围移动而不绘图。在“绘画”模式下,画笔在移动时会留下一条彩色线条。最重要的是,当模式从“绘画”更改为“移动”时,画笔描出的区域会被颜色填充。

from turtle import *
from functools import partial 
bgpic("Colour_A_Turkey.gif") # change this to change the picture

#control the accuracy/speed of the drawing 
step_size =8
pensize(4)
penup()

# whenever spacebar is pressed the current state  and next state switch values 
current_state = penup
next_state = pendown
def space_bar():
    global current_state, next_state
    next_state()
    current_state, next_state = next_state, current_state
    #if the current stat is penup fill in with current colour
    if current_state == penup:
        end_fill()
    else:
        begin_fill()
onkey(space_bar, " ")

# undo do a mistake function
def mistake():
    undo()
onkey(mistake, "z")

#using partial function to store the following functions
#so they can be called  as arguments from a dictionary
#movement
strait = partial(fd, step_size)
reverse = partial(bk, step_size)
turn_rt = partial(rt, step_size)
turn_lf = partial(lt, step_size)

#colour
brow = partial(color, "brown")
gree = partial(color, "green")
yell = partial(color, "yellow")
oran = partial(color, "orange")
purp = partial(color, "purple")
red = partial(color, "red")
blue = partial(color, "blue")


#create a dictionary to store all the keys and there abilities 
key_action = {"b" : blue, "r" : red, "p" : purp, "o" : oran,\
          "y" : yell, "g" : gree, "w" : brow, "Right" : turn_rt , "Up" : strait,\
          "Down" : reverse, "Left" : turn_lf, "z" : undo()}

#when a key in then above dictionary
#is pressed it's function  is activated
for pressed_key, activated_key in key_action.items():
    onkey(activated_key, pressed_key)  

#make turtle look for key strokes with predefined function
listen()
#finish
done()
4

3 回答 3

1

请记住,在 Python 中,一切都是对象,我们的真正意思是. 函数也是对象。我们可以将函数存储在字典中,事实上这正是您在这里想要做的。

turn_lf = lt(step_size )

这里的关键问题是你想存储“一个调用ltwithstep_size作为参数的函数”,但在这里你只是立即调用ltwithstep_size作为参数,并存储了返回值。

Arguably the simplest way to get what you want is to use functools.partial to "bind" the step_size argument.

from functools import partial
turn_lf = partial(lt, step_size) # `lt` is not called yet.

# Now, later on, we can do
turn_lf() # and it's just as if we did `lt(step_size)`.
# So now we can store `turn_lf` in a dict, look it up and call it later, etc.

# Similarly for all the other functions you want to make.

(Another problem is that you haven't been consistent with this; if you want everything to go in one dict, then you need to indicate the bindings for the color function as well. 'brown' is just a string, after all. Fortunately, this is just as simple as with the other functions: we just make our partial(color, 'brown').

As for "z" : delete, well - we don't have any arguments to bind to undo. So while we could follow the pattern and write partial(undo) (notice, no more arguments, because we aren't binding anything), it makes much more sense to just write undo directly.

As an aside, we can simplify this:

for key in key_action:
    pressed_key = key
    activated_key = key_action[key]

To this:

for pressed_key, activated_key in key_action.items():
于 2013-04-11T07:23:24.133 回答
-1

为什么要定义所有函数?我们可以直接使用: 将 dict 分解为:

key_action = {"b" : "blue", "r" : "red", "p" : "purple", "o" : "orange", "y" : "yellow","g" : "green", "w" : "brown","Right" : rt , "Left": lt, "Up" : fd, "Down" : bk,"z" : undo}


no_input = ["z"]

one_input = ["Right", "Left", "Up", "Down"]

for key in key_action:
    activated_key = key_action[key]
    if key in no_input:
       activated_key()
    elif key in one_input():
        activated_key(step_size)
    else:
        onkey(color(activated_key), key)
于 2013-04-11T05:18:53.270 回答
-1

你的字典方法是正确的思维方式。但是,您的函数将无法正常运行。使用字典比你想象的要简单。首先,您必须对STRING有单独的FUNCTIONS并正确处理它们。因为他们每个人的行为都会不同:

# Get rid of the top four lines, they will not work
# All actions mapping
key_action = {"b" : "blue", "r" : "red", "p" : "purple", "o" : "orange",
              "y" : "yellow", "g" : "green", "w" : "brown",
              "Right" : rt , "Left": lt, "Up" : fd, "Down" : bk,
              "z" : undo}
# Note down which are special actions
# Functions with no input
foo = ["z"]
# Function with one input
bar = ["Right", "Left", "Up", "Down"]

# Assuming the input key is get here, I don't use turtle don't know how
# the input is read
key = listen()  # PSEUDOCODE!

# Handle the input
if key in foo:
     foo[key]() # Execute no input function
elif key in bar:
     foo[key](step_size)
else:
     onkey(key_action[key], key)
于 2013-04-11T05:27:16.960 回答