根据我收到的建议,我决定重做这个问题,这是我给第一年uni,python编码的一个任务问题。我的代码中有错误,无法确定修复它们的位置。 BUG 1即使笔已启动,程序运行时乌龟也会开始绘图。 BUG 2未定义的键,例如's,7,tab'触发space_bar函数
着色书
在此任务中,您将创建一个儿童着色游戏 给定的图片可以通过追踪形状来着色 然后填写它。控制如下。
箭头键 - 向左,向右移动“画笔”(乌龟光标) 或减少固定的少量。
'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 :(得分:1)
请记住,在Python中,一切都是对象,我们的意思是。函数也是对象。我们可以将函数存储在字典中,事实上这正是你在这里想要做的。
turn_lf = lt(step_size )
这里的关键问题是你要存储“一个用lt
作为参数调用step_size
的函数”,但是你刚刚调用了lt
step_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():
答案 1 :(得分:-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)
答案 2 :(得分:-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)