我决定根据我收到的建议重做这个问题,这是我为第一年的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()
什么功能FD,BK,撤消,RT,LT返回吗? – Thunderboltz 2013-04-11 05:06:02
forward(x)/ back(x)/撤消最后一件事turtle did/right(x)/ left(x)。所有预定义的功能,与乌龟 – 2013-04-11 09:26:36
IS现在修复现在感谢大家谁帮助,我只定义了一次z,并将“”更改为“空间” – 2013-04-11 12:58:21