使用Python在按键时在函数之间切换
嘿,伙计们,我正在为uni做一个助手,我必须制作一本彩色书,使用户可以使用空格键在两种模式之间切换。在第一种模式中,用户必须能够在不绘制任何线条的情况下移动海龟,第二种模式必须启动画笔模式,用户可以在其中绘制。我希望在按下空格按钮时能够在penup()和pendown()海龟函数之间切换。有什么想法吗 这就是我到目前为止所做的:使用Python在按键时在函数之间切换,python,function,toggle,turtle-graphics,Python,Function,Toggle,Turtle Graphics,嘿,伙计们,我正在为uni做一个助手,我必须制作一本彩色书,使用户可以使用空格键在两种模式之间切换。在第一种模式中,用户必须能够在不绘制任何线条的情况下移动海龟,第二种模式必须启动画笔模式,用户可以在其中绘制。我希望在按下空格按钮时能够在penup()和pendown()海龟函数之间切换。有什么想法吗 这就是我到目前为止所做的: from turtle import * bgpic("Colour_A_Turkey.gif") # change this to change the pictur
from turtle import *
bgpic("Colour_A_Turkey.gif") # change this to change the picture
# PUT YOUR CODE HERE
setup(800,600)
home()
pen_size = 2
color("red")
title("Colouring Book")
speed("fastest") # Doesn't make any difference to accuracy, just makes turtle turn animation faster.
drawdist=10 # Distance in pixels pen travels when arrow key is pressed
penup()
###################BUTTON INSTRUCTIONS########################
def move_up():
seth(90)
forward(drawdist)
def move_down():
seth(270)
forward(drawdist)
def move_left():
seth(180)
forward(drawdist)
def move_right():
seth(0)
forward(drawdist)
def space_bar():
if isdown()==True:
penup()
if isdown()==False:
pendown()
####Change pen color####
def red():
color("red")
def green():
color("green")
def blue():
color("blue")
################BUTTON TRIGGERS##################
s= getscreen()
s.onkey(move_up,"Up")
s.onkey(move_down,"Down")
s.onkey(move_left,"Left")
s.onkey(move_right,"Right")
s.onkey(space_bar,"space")
s.onkey(red,"r")
s.onkey(green,"g")
s.onkey(blue,"b")
listen()
done()
调用空格键时,isdown()始终为true。
是否仅在按下空格键时切换或绘制 如果要切换,请执行以下操作:
current_state = penup
next_state = pendown
def space_bar():
global current_state, next_state
next_state()
current_state, next_state = next_state, current_state
这是您如何在“向上画笔”和“向下画笔”之间切换的
up = False
def pen_up():
global up
up = not up
if up:
t.penup()
else:
t.pendown()
ts.onkey(pen_up, 'space')
刚刚编辑了原始问题我想在移动乌龟和绘画模式之间切换pleasethanks bro现在可以了希望我能投票支持你但我没有足够的代表:(很乐意帮忙。我也是新来的,但我想你可以把它标记为你问题的答案或其他东西……啊,巨大的滴答声,我怎么会错过这个??这里最大的一个角落对,我不知道itertools。酷。
up = False
def pen_up():
global up
up = not up
if up:
t.penup()
else:
t.pendown()
ts.onkey(pen_up, 'space')