2013-04-11 18 views
0

我决定根据我收到的建议重做这个问题,这是我为第一年的uni编写的赋值问题。我有我的代码中的错误,无法找出修复它们的位置。 错误1即使笔开始运行,程序运行时龟也开始绘制。 BUG 2未定义键如的,7,标签'触发space_bar功能使用onkey()和字典使用龟(python)绘制的错误

着色书

在此任务中创建一个儿童填色游戏,其中 给出的图片可以通过周围的形状跟踪和着色 然后填充它。控件如下。

方向键 - 将“画笔”(乌龟光标)向左或向右移动 或向下移动一个固定的小数量。

'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() 
+0

什么功能FD,BK,撤消,RT,LT返回吗? – Thunderboltz 2013-04-11 05:06:02

+0

forward(x)/ back(x)/撤消最后一件事turtle did/right(x)/ left(x)。所有预定义的功能,与乌龟 – 2013-04-11 09:26:36

+0

IS现在修复现在感谢大家谁帮助,我只定义了一次z,并将“”更改为“空间” – 2013-04-11 12:58:21

回答

1

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

turn_lf = lt(step_size) 

这里的关键问题是,要存储“调用ltstep_size作为自变量的函数”,但在这里,你刚才叫ltstep_size作为参数立即和存储返回值。

可以说得到你想要的最简单的方法是使用functools.partial来“绑定”参数step_size

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. 

(另一个问题是,你还没有与此一致;如果你想要的一切在一个字典去,那么你需要指出的color功能以及绑定'brown'只是一个字符串。 。毕竟幸运的是,这仅仅是因为与其他功能一样简单:我们只是让我们的partial(color, 'brown')

至于"z" : delete,以及 - 我们没有任何参数绑定到undo所以当我们可能。按照模式,并写partial(undo)(注意,没有更多的论据,因为我们没有任何约束力),直接写undo就更有意义了。

顺便说一句,我们可以简化这个:

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

要这样:

for pressed_key, activated_key in key_action.items(): 
+0

没有模块调用fuctools..sorry我是新来的编码,在这个单元之前从来没有做过,我必须使用python 2.7标记原因,我会在哪里找到这个fuctools模块 – 2013-04-11 09:49:49

+0

nvm..reread your comment X_X, – 2013-04-11 09:53:59

-1

为什么要定义所有功能?我们可以直接使用: 打破字典为:

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) 
+0

得到了一个errorif len(color)== 1: TypeError:'NoneType'有没有len() – 2013-04-11 05:26:09

+0

gahh我进入而不是shift输入对不起,双后 如果len(color)== 1: TypeError:'NoneType'类型的对象没有len() 也我想能够控制带箭头键的龟和带z的撤消(),并用此功能改变颜色。 – 2013-04-11 05:32:16

+0

我编辑了我的答案。看看它。它与您的问题中回答的下一个答案类似。 – ashokadhikari 2013-04-11 05:54:10

-1

你的字典方法是沿着思维的正确方式。但是,您的功能将无法正常工作。使用字典比你想象的要简单。首先,您必须单独使用函数而不是STRING并正确处理它们。由于他们每个人都会表现不同:

# 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)