def __init__(self, window): self.hangman = Hangman() self.window = window # create a new pen to write the dashes self.pen = Frog(self.window) self.pen.visible = False self.pen.write(' '.join('_' for char in self.hangman.word)) if SHOW_SOLUTION: # show the solution solution = Frog(self.window) solution.visible = False solution.jumpto(0, 50) solution.write(' '.join(char for char in self.hangman.word)) # prepare a pen for writing the gallow self.gallow_writer = Frog(self.window) self.gallow_writer.visible = False self.gallow_writer.jump(-150)
def __init__(self, window): self.hangman = Hangman() self.window = window # create a new pen to write the dashes self.pen = Frog(self.window) self.pen.visible = False self.pen.write(' '.join('_' for char in self.hangman.word)) if SHOW_SOLUTION: # show the solution solution = Frog(self.window) solution.visible = False solution.jumpto(0, 50) solution.write(' '.join(char for char in self.hangman.word)) # prepare a pen for writing the gallow self.gallow_writer = Frog(self.window) self.gallow_writer.visible = False self.gallow_writer.jump(-150)
class HangmanWidget(object): """ The GUI stuff """ def __init__(self, window): self.hangman = Hangman() self.window = window # create a new pen to write the dashes self.pen = Frog(self.window) self.pen.visible = False self.pen.write(' '.join('_' for char in self.hangman.word)) if SHOW_SOLUTION: # show the solution solution = Frog(self.window) solution.visible = False solution.jumpto(0, 50) solution.write(' '.join(char for char in self.hangman.word)) # prepare a pen for writing the gallow self.gallow_writer = Frog(self.window) self.gallow_writer.visible = False self.gallow_writer.jump(-150) def reset(self): """ clear the whole window and restart the game """ # delete the 'frogs' self.pen.exit() self.gallow_writer.exit() self.__init__(self.window) def attempt(self, event): guessed_char = event['name'] # lock the pen if it is just writing if self.gallow_writer.active: return self.gallow_writer.speed = 'max' # TODO: write a list of all guessed letters # right attempt if guessed_char.lower() in self.hangman.word: self.pen.write(self.hangman.guess(guessed_char)) if len(self.hangman.right_attempts) == len(set(self.hangman.word)): self.gallow_writer.message( 'info', 'Congratulation! You guessed the word correctly!' ) # start a new game self.reset() # wrong attempt else: # the number of mistakes num_mistakes = len(self.hangman.wrong_attempts) figures = [ self.hill, self.first_beam, self.second_beam, self.bracket, self.rope, self.head, self.body, self.first_leg, self.second_leg, self.first_arm, self.second_arm, ] # only draw a figure if the guessed # letter was not already guessed earlier if guessed_char not in self.hangman.attempts: figures[num_mistakes]() self.hangman.guess(guessed_char) def hill(self): self.gallow_writer.turnto(90) self.gallow_writer.circle(50, 180) def first_beam(self): self.gallow_writer.circle(50, -90, draw = False) self.gallow_writer.turnto(90) self.gallow_writer.move(100) def second_beam(self): self.gallow_writer.turnto(0) self.gallow_writer.move(100) def bracket(self): # move back and build the supporting beam (and yes, the strange # number 36 is necessary because the gallow must be hit exactly # on the right place) self.gallow_writer.jump(-75) self.gallow_writer.turnto(225) self.gallow_writer.move(36) # don't forget to to move to the place where the man will be hung self.gallow_writer.jump(-36) self.gallow_writer.turnto(0) self.gallow_writer.jump(75) def rope(self): self.gallow_writer.turnto(270) self.gallow_writer.move(25) def head(self): self.gallow_writer.turnto(0) self.gallow_writer.circle(-15) def body(self): self.gallow_writer.turnto(270) self.gallow_writer.jump(30) self.gallow_writer.move(50) def first_leg(self): self.gallow_writer.turnto(225) self.gallow_writer.move(25) def second_leg(self): self.gallow_writer.jump(-25) self.gallow_writer.turnto(315) self.gallow_writer.move(25) def first_arm(self): self.gallow_writer.jump(-25) self.gallow_writer.turnto(90) self.gallow_writer.jump(25) self.gallow_writer.turnto(135) self.gallow_writer.move(25) def second_arm(self): """ last figure -> user lost! """ self.gallow_writer.jump(-25) self.gallow_writer.turnto(45) self.gallow_writer.move(25) self.gallow_writer.message( 'Info', 'You lost! Shame on you! The word was: ' + self.hangman.word ) # start a new game self.reset()
class HangmanWidget(object): """ The GUI stuff """ def __init__(self, window): self.hangman = Hangman() self.window = window # create a new pen to write the dashes self.pen = Frog(self.window) self.pen.visible = False self.pen.write(' '.join('_' for char in self.hangman.word)) if SHOW_SOLUTION: # show the solution solution = Frog(self.window) solution.visible = False solution.jumpto(0, 50) solution.write(' '.join(char for char in self.hangman.word)) # prepare a pen for writing the gallow self.gallow_writer = Frog(self.window) self.gallow_writer.visible = False self.gallow_writer.jump(-150) def reset(self): """ clear the whole window and restart the game """ # delete the 'frogs' self.pen.exit() self.gallow_writer.exit() self.__init__(self.window) def attempt(self, event): guessed_char = event['name'] # lock the pen if it is just writing if self.gallow_writer.active: return self.gallow_writer.speed = 'max' # TODO: write a list of all guessed letters # right attempt if guessed_char.lower() in self.hangman.word: self.pen.write(self.hangman.guess(guessed_char)) if len(self.hangman.right_attempts) == len(set(self.hangman.word)): self.gallow_writer.message( 'info', 'Congratulation! You guessed the word correctly!') # start a new game self.reset() # wrong attempt else: # the number of mistakes num_mistakes = len(self.hangman.wrong_attempts) figures = [ self.hill, self.first_beam, self.second_beam, self.bracket, self.rope, self.head, self.body, self.first_leg, self.second_leg, self.first_arm, self.second_arm, ] # only draw a figure if the guessed # letter was not already guessed earlier if guessed_char not in self.hangman.attempts: figures[num_mistakes]() self.hangman.guess(guessed_char) def hill(self): self.gallow_writer.turnto(90) self.gallow_writer.circle(50, 180) def first_beam(self): self.gallow_writer.circle(50, -90, draw=False) self.gallow_writer.turnto(90) self.gallow_writer.move(100) def second_beam(self): self.gallow_writer.turnto(0) self.gallow_writer.move(100) def bracket(self): # move back and build the supporting beam (and yes, the strange # number 36 is necessary because the gallow must be hit exactly # on the right place) self.gallow_writer.jump(-75) self.gallow_writer.turnto(225) self.gallow_writer.move(36) # don't forget to to move to the place where the man will be hung self.gallow_writer.jump(-36) self.gallow_writer.turnto(0) self.gallow_writer.jump(75) def rope(self): self.gallow_writer.turnto(270) self.gallow_writer.move(25) def head(self): self.gallow_writer.turnto(0) self.gallow_writer.circle(-15) def body(self): self.gallow_writer.turnto(270) self.gallow_writer.jump(30) self.gallow_writer.move(50) def first_leg(self): self.gallow_writer.turnto(225) self.gallow_writer.move(25) def second_leg(self): self.gallow_writer.jump(-25) self.gallow_writer.turnto(315) self.gallow_writer.move(25) def first_arm(self): self.gallow_writer.jump(-25) self.gallow_writer.turnto(90) self.gallow_writer.jump(25) self.gallow_writer.turnto(135) self.gallow_writer.move(25) def second_arm(self): """ last figure -> user lost! """ self.gallow_writer.jump(-25) self.gallow_writer.turnto(45) self.gallow_writer.move(25) self.gallow_writer.message( 'Info', 'You lost! Shame on you! The word was: ' + self.hangman.word) # start a new game self.reset()