Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/PyFlat/Hangman
Browse files Browse the repository at this point in the history
  • Loading branch information
PyFlat-JR committed Oct 12, 2023
2 parents 16ad6d7 + 0f737d8 commit a11d003
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.ui
*.ui

hangbreak.py
37 changes: 33 additions & 4 deletions hangman.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,37 @@ def analyse_word(self):
return dict

def start_game(self):
self.word = self.words.get_new_random_word()
self.word = self.word_saved = self.words.get_new_random_word()
self.analysed = self.analyse_word()
self.word = "_" * len(self.word)
self.drawWidget.setHangmanParts(0)
self.update_main_label(self.word)

self.keyboardWidget.new_key_typed.connect(self.new_char)
self.keyboardWidget.enableAll()

def lose(self):
self.reveal()
print(self.word_saved)
self.start_game()

def reveal(self):
self.update_main_label(self.word_saved)

def new_char(self, key):
self.keyboardWidget.disableByKey(key)
if key in self.analysed:
for index in self.analysed[key]:
self.word = self.word[:index] + key + self.word[index + 1:]
self.update_main_label(self.word)
else:
self.drawWidget.setHangmanParts(self.drawWidget.hangman_parts + 1)
if self.drawWidget.hangman_parts >= self.drawWidget.max_parts:
self.drawWidget.setHangmanParts(0)

self.keyboardWidget.new_key_typed.disconnect()
self.start_game()
self.lose()



class HangmanWidget(QWidget):
def __init__(self):
Expand Down Expand Up @@ -134,13 +148,15 @@ def __init__(self):
"ASDFGHJKL",
"YXCVBNM",
]
self.buttons = {}

for row in keyboard_layout:
row_layout = QHBoxLayout()
for letter in row:
button = QPushButton(letter)
button.clicked.connect(self.on_button_click)
row_layout.addWidget(button)
self.buttons[letter] = button
self.layout.addLayout(row_layout)

def on_button_click(self):
Expand All @@ -149,9 +165,22 @@ def on_button_click(self):
letter = sender.text()
self.new_key_typed.emit(letter.lower())

def keyPressEvent(self, event):
key = event.text().upper()
if key in self.buttons:
self.buttons[key].click()

def enableAll(self):
for button in self.buttons:
self.buttons[button].setEnabled(True)

def disableByKey(self, key:str):
self.buttons[key.upper()].setEnabled(False)


class Words():
def __init__(self, wordlist_path = None):
if wordlist_path is None: wordlist_path = "wortliste2.txt"
if wordlist_path is None: wordlist_path = "wortliste.txt"
self.lines = open(wordlist_path, "r", newline="\n").readlines()
self.wordlist = [line.strip().lower() for line in self.lines]
def get_new_random_word(self):
Expand Down

0 comments on commit a11d003

Please sign in to comment.