1

嘿伙计们,我正在为 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()
4

3 回答 3

1

当调用 space_bar 时,isdown() 始终为真。
您是否只想在按下空格键时切换或绘制?

如果要切换,可以执行以下操作:

current_state = penup
next_state = pendown
def space_bar():
    global current_state, next_state
    next_state()
    current_state, next_state = next_state, current_state
于 2013-04-08T06:26:50.930 回答
1
from itertools import cycle

funcs = cycle([f1, f2])
next(funcs)() # alternates
于 2013-04-08T08:36:10.117 回答
0

这就是您在上笔和下笔之间切换的方式。

up = False

def pen_up():

    global up
    up = not up
    if up:
        t.penup()
    else:
        t.pendown()

ts.onkey(pen_up, 'space')
于 2013-05-17T23:24:53.613 回答