2015-08-24 48 views
0

我正在寫一個類似憤怒鳥的遊戲,當玩家輸入'g',然後輸入'o',然後輸入'd'('god','god mode')時,我想要一個目標將會出現在鳥的目前位置(如果你開火的話)(現在給定度數和初始速度)。 - 神模式在遊戲中可用3次。如何知道某個字符序列的輸入時間?

所以,我試圖類,並存儲每個兩字符最後鍵入: [我使用的Tkinter和圖片,ImageTk從PIL]

class GodMode: 

times_left = 3 # Default 

def __init__(self, master, image_file): 
    self.master = master 
    self.target_image = Image.open(image_file) 
    self.target = ImageTk.PhotoImage(self.target_image.resize((RED_CROSS_WIDTH, RED_CROSS_HEIGHT), Image.ANTIALIAS)) 

def display(self, bird): 
    self.times_left -= 1 
    self.x = physics.distance_traveled(bird) 
    self.y = TARGET_Y_LOCATION 
    self.master.create_image(self.x, self.y, image = self.target) 

def typing_god(self, keystroke, bird): 
    '''This function is first called when "g" is typed, from another 
    function (who is binded in the "main" function) 
    ''' 
    if keystroke is None: 
     self.last_char = "g" 
    else: 
     if keystroke.char == "d" or keystroke.char == "D": 
      if self.last_char == "o" and self.char_before_last == "g": 
       if self.times_left == 3: 
        self.god_mode_start(bird) 
       elif 0 < self.times_left: 
        self.god_mode_alert(bird) 
       else: 
        help_over() 
        return 
     else: 
      self.char_before_last = self.last_char 
      self.last_char = keystroke.char 
    self.master.bind('<Key>', lambda event: typing_god(event, bird)) 

def god_mode_start(self): 
    start_input = messagebox.askyesno(title = "GOD Mode", message = "The god mode is only optional 3 times. Are you sure?") 
    if not start_input: 
     return 
    self.display(bird) 

def god_mode_alert(self, bird): 
    start = messagebox.showinfinfo(title = "GOD Mode", message = GOD_MODE_FORMAT % self.gode_mode_left) 
    if not start: 
     return 
    self.display(bird) 

@staticmethod 
def help_over(): 
    messagebox.showinfo(title = "GOD Mode not available", message = "You have used all your help.") 

...但沒有工作,我可以」告訴你爲什麼。

非常感謝您的幫助!

大衛

+0

但是究竟是什麼錯誤? – mic4ael

+0

沒有錯誤,它根本不起作用。如果你發射了它,目標不會在鳥會降落的地方發生。 – David

+1

'看起來很可疑的'self.master.bind('',lambda事件:typing_god(event,bird))'你想從'GodMode'類中調用'typing_god'嗎?如果是這樣,那麼你應該改變它'self.typing_god' – mic4ael

回答

-1

我會用一個deque。您可以設置maxlen = 3。當你得到一個新的擊鍵時,你可以附加到它。檢查如果在dequegod也很容易:

In [1]: from collections import deque 
In [2]: d = deque(maxlen=3) 
In [3]: d.append('g') # new keystroke 
In [4]: d.append('o') # new keystroke 
In [5]: d.append('d') # new keystroke 
In [6]: d 
Out[6]: deque(['g', 'o', 'd'], maxlen=3) 
In [7]: ''.join(d) == 'god' # check if we already in the god mode 
Out[7]: True 
In [8]: d.append('x') # new keystroke 
In [9]: d 
Out[9]: deque(['o', 'd', 'x'], maxlen=3) 
In [10]: ''.join(d) == 'god' # check if we already in the god mode 
Out[10]: False 
0

它看起來像self.master.bind('<Key>', lambda event: typing_god(event, bird))可能永遠不會得到所謂的,你有沒有加入解決這個聲明的打印檢查?

你可能想,而不是把它在構造函數中,並使用self.typing_god:做一個綁定時

def __init__(self, master, image_file): 
    self.master = master 
    self.target_image = Image.open(image_file) 
    self.target = ImageTk.PhotoImage(self.target_image.resize((RED_CROSS_WIDTH, RED_CROSS_HEIGHT), Image.ANTIALIAS)) 
    # Add a debug statement to check that this method is getting called 
    print "Binding key events to self.typing_god" 
    # Bind the key down event in the constructor 
    self.master.bind('<Key>', lambda event: self.typing_god(event, bird)) 
0

可以串在一起的事件。因此,舉例來說,可以在三個字符序列結合「神」(例如:"<g><o><d>"

def god_mode(event): 
    print("god mode is activated") 
some_widget.bind("<g><o><d>", god_mode) 

假設some_widget具有鍵盤焦點,當按下鍵「G」,「o」和「d 「按照這個順序,信息將會打印出來。

這是使用虛擬事件的好機會。您可以定義自己的事件並將其綁定到它:

some_widget.event_add("<<GodMode>>", "<g><o><d>") 
some_widget.bind("<<GodMode>>", god_mode) 
相關問題