-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman_controller.py
60 lines (48 loc) · 2.27 KB
/
hangman_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys
from PyQt5 import QtGui, QtWidgets
from hangman_logic import Hangman
from hangman_GUI import Ui_MainWindow
class Controller():
def __init__(self, hangman, gui):
self.hangman_instance = hangman
self.gui = gui
self.end_game = False
self.gui.current_word.setText(self.hangman_instance.indicator)
def update(self, letter):
if letter == 'new_game':
self.hangman_instance.new_game()
self.gui.end_game_message.setText("Press a letter to guess!")
self.gui.current_word.setText(self.hangman_instance.indicator)
self.gui.hangman_pic.setPixmap(QtGui.QPixmap("h{}.png".format(self.hangman_instance.guess_times+1)))
self.gui.show_result.setEnabled(True)
elif letter == 'show_result':
self.gui.end_game_message.setText('The word is')
self.gui.current_word.setText(self.hangman_instance.word)
self.gui.hangman_pic.setPixmap(QtGui.QPixmap("h{}.png".format(self.hangman_instance.guess_times+1)))
self.gui.show_result.setEnabled(False)
else:
self.hangman_instance.guess(letter)
self.gui.current_word.setText(self.hangman_instance.indicator)
self.gui.hangman_pic.setPixmap(QtGui.QPixmap("h{}.png".format(self.hangman_instance.guess_times+1)))
#picture from https://github.com/en3rypt/HANGMAN-GAME
self.check_endgame_condition()
def check_endgame_condition(self):
if self.hangman_instance.guess_times >=6:
self.gui.end_game_message.setText('Better luck next time!')
self.gui.current_word.setText(self.hangman_instance.word)
self.gui.disable_letters()
self.gui.show_result.setEnabled(False)
elif len(self.hangman_instance.word_letters) == 0:
self.gui.end_game_message.setText('Congratulations!')
self.gui.disable_letters()
self.gui.show_result.setEnabled(False)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
hangman = Hangman()
controller = Controller(hangman, ui)
ui.add_observer(controller)
MainWindow.show()
sys.exit(app.exec_())