Hey Guys我正在为uni工作,我必须制作一本着色书,使用户可以使用空格键在两种模式之间切换。在第一种模式中,用户必须能够在不绘制任何线条的情况下移动乌龟,第二种模式必须启动用户可以绘制的画笔模式。我希望能够在penup()和pendown()之间切换按下空格按钮时乌龟功能。有什么想法吗?
这是我到目前为止所做的:
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()
答案 0 :(得分:1)
调用space_bar时,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
答案 1 :(得分:1)
from itertools import cycle
funcs = cycle([f1, f2])
next(funcs)() # alternates
答案 2 :(得分:0)
这是你在笔上笔和笔下笔之间切换的方法。
up = False
def pen_up():
global up
up = not up
if up:
t.penup()
else:
t.pendown()
ts.onkey(pen_up, 'space')