我正在寫一個類似憤怒鳥的遊戲,當玩家輸入'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.")
...但沒有工作,我可以」告訴你爲什麼。
非常感謝您的幫助!
大衛
但是究竟是什麼錯誤? – mic4ael
沒有錯誤,它根本不起作用。如果你發射了它,目標不會在鳥會降落的地方發生。 – David
'看起來很可疑的'self.master.bind('',lambda事件:typing_god(event,bird))'你想從'GodMode'類中調用'typing_god'嗎?如果是這樣,那麼你應該改變它'self.typing_god' –
mic4ael