I'm writing an angry-birds-like game, and I want that when the player types 'g', and then 'o', and then 'd' ('god', for 'god mode'), a target will apear on the spot that the bird will currently (given the degrees and initial velocity) land on (if you'd fire it). - god mode is available 3 times in a game.
So I tried class, and storing each two-last chars typed: [I use tkinter and Image, ImageTk from 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.")
...but that didn't work, I can't tell why.
Thanks a lot for any help!
David