2013-04-08 139 views
1

嘿,夥計們我正在爲uni做一件事,我必須製作一本使用戶可以使用空格鍵在兩種模式之間切換的着色書。在第一種模式下,用戶必須能夠移動龜畫出任何線條,第二種模式必須啓動用戶可以繪製的畫筆模式。我希望能夠在penup()和pendown()之間切換,當按下空格按鈕時龜會起作用。有任何想法嗎?當使用Python按鍵時在功能之間切換功能

這是我到目前爲止有:

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

剛纔編輯原來的問題 – 2013-04-08 06:23:15

回答

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 
+0

我只想移動龜和繪畫方式請 – 2013-04-08 06:52:34

+0

謝謝老兄現在的工作希望我能投你了,但我沒有足夠的代表:( – 2013-04-08 07:03:28

+0

快樂之間切換我也是新來的,但我認爲你可以將它標記爲你的問題的答案或其他什麼... – avrahamy 2013-04-08 07:25:14

1
from itertools import cycle 

funcs = cycle([f1, f2]) 
next(funcs)() # alternates 
0

這是你的筆了與落筆之間如何切換。

up = False 

def pen_up(): 

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

ts.onkey(pen_up, 'space')