class HangmanGame(QWidget): def __init__(self, parent=None): super().__init__(parent) # Initialize word database self.word = Word('words.txt') # Hangman display window self.hangmanWindow = QTextEdit() self.hangmanWindow.setReadOnly(True) self.hangmanWindow.setAlignment(Qt.AlignLeft) font = self.hangmanWindow.font() font.setFamily('Courier New') self.hangmanWindow.setFont(font) # Layout hangmanLayout = QGridLayout() hangmanLayout.addWidget(self.hangmanWindow, 0, 0) # Status Layout creation statusLayout = QGridLayout() # Display widget for current status self.currentWord = QLineEdit() self.currentWord.setReadOnly(True) self.currentWord.setAlignment(Qt.AlignCenter) font = self.currentWord.font() font.setPointSize(font.pointSize() + 8) self.currentWord.setFont(font) statusLayout.addWidget(self.currentWord, 0, 0, 1, 2) # Display widget for already used characters self.guessedChars = QLineEdit() self.guessedChars.setReadOnly(True) self.guessedChars.setAlignment(Qt.AlignLeft) self.guessedChars.setMaxLength(52) statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2) # Display widget for message output self.message = QLineEdit() self.message.setReadOnly(True) self.message.setAlignment(Qt.AlignLeft) self.message.setMaxLength(52) statusLayout.addWidget(self.message, 2, 0, 1, 2) # Input widget for user selected characters self.charInput = QLineEdit() self.charInput.setMaxLength(1) statusLayout.addWidget(self.charInput, 3, 0) # Button for submitting a character self.guessButton = QToolButton() self.guessButton.setText('Guess!') self.guessButton.clicked.connect(self.guessClicked) statusLayout.addWidget(self.guessButton, 3, 1) # Button for a new game self.newGameButton = QToolButton() self.newGameButton.setText('New Game') self.newGameButton.clicked.connect(self.startGame) statusLayout.addWidget(self.newGameButton, 4, 0) # Layout placement mainLayout = QGridLayout() mainLayout.setSizeConstraint(QLayout.SetFixedSize) mainLayout.addLayout(hangmanLayout, 0, 0) mainLayout.addLayout(statusLayout, 0, 1) self.setLayout(mainLayout) self.setWindowTitle('Hangman Game') # Start a new game on application launch! self.startGame() def startGame(self): self.hangman = Hangman() self.guess = Guess(self.word.randFromDB()) font = self.currentWord.font() if len(self.guess.secretWord) > 13: #font.setPointSize(font.pointSize() - 8) font.setPointSize(8) else: #font.setPointSize(font.pointSize() + 8) font.setPointSize(12) self.currentWord.setFont(font) self.gameOver = False self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.clear() def displayCurrentStatus(self): str = '' for i in self.guess.currentStatus: str += i str += ' ' return str def displayGuessedChars(self): str = '' for i in self.guess.guessedChars: str += i str += ' ' return str def guessClicked(self): guessedChar = self.charInput.text() self.charInput.clear() self.message.clear() if self.gameOver == True: # 메시지 출력하고 - message.setText() - 리턴 self.message.setText("Game_Over") return "gameover" # 입력의 길이가 1 인지를 판단하고, 아닌 경우 메시지 출력, 리턴 if len(guessedChar) != 1: self.message.setText("Not a len 1 char") return "lengthError" # 이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴 if guessedChar in self.guess.guessedChars: self.message.setText("Used char") return "Used char" success = self.guess.guess(guessedChar) if success == False: # 남아 있는 목숨을 1 만큼 감소 self.hangman.decreaseLife() # 메시지 출력 self.message.setText(guessedChar +" not in word") # hangmanWindow 에 현재 hangman 상태 그림을 출력 self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력 self.currentWord.setText(self.displayCurrentStatus()) # guessedChars 에 지금까지 이용한 글자들의 집합을 출력 self.guessedChars.setText(self.displayGuessedChars()) if self.guess.finished(): # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로 self.message.setText("Success!") self.gameOver = True elif self.hangman.getRemainingLives() == 0: # 메시지 ("Fail!" + 비밀 단어) 출력하고, self.gameOver 는 True 로 self.message.setText("Fail" + self.word) self.gameOver = True
class HangmanGame(QWidget): def __init__(self, parent=None): super().__init__(parent) # Initialize word database self.word = Word('words.txt') # Hangman display window self.hangmanWindow = QTextEdit() self.hangmanWindow.setReadOnly(True) self.hangmanWindow.setAlignment(Qt.AlignLeft) font = self.hangmanWindow.font() font.setFamily('Arial') self.hangmanWindow.setFont(font) # Layout hangmanLayout = QGridLayout() hangmanLayout.addWidget(self.hangmanWindow, 0, 0) # Status Layout creation statusLayout = QGridLayout() # Display widget for current status self.currentWord = QLineEdit() self.currentWord.setReadOnly(True) self.currentWord.setAlignment(Qt.AlignCenter) font = self.currentWord.font() font.setPointSize(font.pointSize() + 8) self.currentWord.setFont(font) statusLayout.addWidget(self.currentWord, 0, 0, 1, 2) # Display widget for already used characters self.guessedChars = QLineEdit() self.guessedChars.setReadOnly(True) self.guessedChars.setAlignment(Qt.AlignLeft) self.guessedChars.setMaxLength(52) statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2) # Display widget for message output self.message = QLineEdit() self.message.setReadOnly(True) self.message.setAlignment(Qt.AlignLeft) self.message.setMaxLength(52) statusLayout.addWidget(self.message, 2, 0, 1, 2) # Input widget for user selected characters self.charInput = QLineEdit() self.charInput.setMaxLength(1) statusLayout.addWidget(self.charInput, 3, 0) # Button for submitting a character self.guessButton = QToolButton() self.guessButton.setText('Guess!') self.guessButton.clicked.connect(self.guessClicked) statusLayout.addWidget(self.guessButton, 3, 1) # Button for a new game self.newGameButton = QToolButton() self.newGameButton.setText('New Game') self.newGameButton.clicked.connect(self.startGame) statusLayout.addWidget(self.newGameButton, 4, 0) # Layout placement mainLayout = QGridLayout() mainLayout.setSizeConstraint(QLayout.SetFixedSize) mainLayout.addLayout(hangmanLayout, 0, 0) mainLayout.addLayout(statusLayout, 0, 1) self.setLayout(mainLayout) self.setWindowTitle('이범석의 행맨') # Start a new game on application launch! self.startGame() def startGame(self): self.hangman = Hangman() self.guess = Guess(self.word.randFromDB()) self.gameOver = False self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.clear() def guessClicked(self): guessedChar = self.charInput.text() self.charInput.clear() self.message.clear() if self.gameOver == True: self.message.setText("실패했습니다.") return True if len(guessedChar) != 1: self.message.setText("1글자만 입력할 수 있습니다.") return True if guessedChar in self.guess.guessedChars: self.message.setText('이미 \"' + guessedChar + '\"' + "글자를 입력하였습니다.") return True if not guessedChar.isalpha(): self.message.setText("올바른 알파벳을 입력해 주세요.") return True guessedChar = guessedChar.lower() success = self.guess.guess(guessedChar) if not success: self.hangman.decreaseLife() self.message.setText("Life:" + str(self.hangman.getRemainingLives())) self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) if self.guess.finished(): self.message.setText("성공!") self.gameOver = True elif self.hangman.getRemainingLives() == 0: self.message.setText("실패!" + " 정답은 : " + self.guess.secretWord) self.gameOver = True
class HangmanGame(QWidget): def __init__(self, parent=None): super().__init__(parent) # Initialize word database self.word = Word('words.txt') # Hangman display window self.hangmanWindow = QTextEdit() self.hangmanWindow.setReadOnly(True) self.hangmanWindow.setAlignment(Qt.AlignLeft) font = self.hangmanWindow.font() font.setFamily('Courier New') self.hangmanWindow.setFont(font) # Layout hangmanLayout = QGridLayout() hangmanLayout.addWidget(self.hangmanWindow, 0, 0) # Status Layout creation statusLayout = QGridLayout() # Display widget for current status self.currentWord = QLineEdit() self.currentWord.setFixedWidth(350) self.currentWord.setReadOnly(True) self.currentWord.setAlignment(Qt.AlignCenter) font = self.currentWord.font() font.setPointSize(font.pointSize() + 8) self.currentWord.setFont(font) statusLayout.addWidget(self.currentWord, 0, 0, 1, 2) # Display widget for already used characters self.guessedChars = QLineEdit() self.guessedChars.setReadOnly(True) self.guessedChars.setAlignment(Qt.AlignLeft) self.guessedChars.setMaxLength(52) statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2) # Display widget for message output self.message = QLineEdit() self.message.setReadOnly(True) self.message.setAlignment(Qt.AlignLeft) self.message.setMaxLength(52) statusLayout.addWidget(self.message, 2, 0, 1, 2) # Input widget for user selected characters self.charInput = QLineEdit() self.charInput.setMaxLength(1) statusLayout.addWidget(self.charInput, 3, 0) # Button for submitting a character self.guessButton = QToolButton() self.guessButton.setText('Guess!') self.guessButton.clicked.connect(self.guessClicked) statusLayout.addWidget(self.guessButton, 3, 1) # Button for a new game self.newGameButton = QToolButton() self.newGameButton.setText('New Game') self.newGameButton.clicked.connect(self.startGame) statusLayout.addWidget(self.newGameButton, 4, 0) # Layout placement mainLayout = QGridLayout() mainLayout.setSizeConstraint(QLayout.SetFixedSize) mainLayout.addLayout(hangmanLayout, 0, 0) mainLayout.addLayout(statusLayout, 0, 1) self.setLayout(mainLayout) self.setWindowTitle('Hangman Game') # Start a new game on application launch! self.startGame() def startGame(self): self.hangman = Hangman() self.guess = Guess(self.word.randFromDB(20)) self.gameOver = False if len(self.guess.secretWord) >=10: font = self.currentWord.font() font.setPointSize(font.pointSize()) self.currentWord.setFont(font) self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.clear() def guessClicked(self): guessedChar = self.charInput.text() self.charInput.clear() self.message.clear() if self.gameOver == True: self.message.setText("게임이 끝났습니다.") if len(guessedChar) > 1: self.message.setText("한 글자만 입력하세요.") if guessedChar in self.guess.guessedChars: self.message.setText("이미 입력한 값 입니다.") self.hangman.decreaseLife() success = self.guess.guess(guessedChar) if success == False: if guessedChar == " ": self.message.setText("빈칸을 입력했습니다.") else: self.hangman.decreaseLife() self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.setText("틀렸습니다 다시 시도하세요.") if success == True: self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) if self.guess.finished(): self.message.setText("Success!") self.gameOver = True elif self.hangman.getRemainingLives() == 0: self.message.setText("Fail : "+ self.guess.secretWord) self.gameOver = True
class HangmanGame(QWidget): def __init__(self, parent=None): super().__init__(parent) # Initialize word database self.word = Word('words.txt') # Hangman display window self.hangmanWindow = QTextEdit() self.hangmanWindow.setReadOnly(True) self.hangmanWindow.setAlignment(Qt.AlignLeft) font = self.hangmanWindow.font() font.setFamily('Courier New') self.hangmanWindow.setFont(font) # Layout hangmanLayout = QGridLayout() hangmanLayout.addWidget(self.hangmanWindow, 0, 0) # Status Layout creation statusLayout = QGridLayout() # Display widget for current status self.currentWord = QLineEdit() self.currentWord.setReadOnly(True) self.currentWord.setAlignment(Qt.AlignCenter) self.currentWord.setMinimumWidth(400) font = self.currentWord.font() font.setPointSize(font.pointSize() + 8) self.currentWord.setFont(font) statusLayout.addWidget(self.currentWord, 0, 0, 1, 2) # Display widget for already used characters self.guessedChars = QLineEdit() self.guessedChars.setReadOnly(True) self.guessedChars.setAlignment(Qt.AlignLeft) self.guessedChars.setMaxLength(52) statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2) # Display widget for message output self.message = QLineEdit() self.message.setReadOnly(True) self.message.setAlignment(Qt.AlignLeft) self.message.setMaxLength(52) statusLayout.addWidget(self.message, 2, 0, 1, 2) # Input widget for user selected characters self.charInput = QLineEdit() self.charInput.setMaxLength(1) statusLayout.addWidget(self.charInput, 3, 0) # Button for submitting a character self.guessButton = QToolButton() self.guessButton.setText('Guess!') self.guessButton.clicked.connect(self.guessClicked) statusLayout.addWidget(self.guessButton, 3, 1) # Button for a new game self.newGameButton = QToolButton() self.newGameButton.setText('New Game') self.newGameButton.clicked.connect(self.startGame) statusLayout.addWidget(self.newGameButton, 4, 0) # Layout placement mainLayout = QGridLayout() mainLayout.setSizeConstraint(QLayout.SetFixedSize) mainLayout.addLayout(hangmanLayout, 0, 0) mainLayout.addLayout(statusLayout, 0, 1) self.setLayout(mainLayout) self.setWindowTitle('Hangman Game') # Start a new game on application launch! self.startGame() def startGame(self): self.hangman = Hangman() self.guess = Guess(self.word.randFromDB(0)) self.gameOver = False self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.clear() def guessClicked(self): guessedChar = self.charInput.text() self.charInput.clear() self.message.clear() if self.gameOver == True: # 메시지 출력하고 - message.setText() - 리턴 self.message.setText("Game Over...") return # 입력의 길이가 1 인지를 판단하고, 아닌 경우 메시지 출력, 리턴 if len(guessedChar) != 1: self.message.setText("You must input one charactor!") return # 입력된 문자가 알파벳이 아니면 처리하지 않음 if not ord('a') <= ord(guessedChar) <= ord('z'): self.message.setText("You can input a-z") return # 이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴 if guessedChar in self.guessedChars.text(): self.message.setText("You already input '%s'!" % guessedChar) return success = self.guess.guess(guessedChar) if success == False: # 남아 있는 목숨을 1 만큼 감소 self.hangman.decreaseLife() # 메시지 출력 if self.hangman.remainingLives > 1: self.message.setText("Wrong... %d lives left" % self.hangman.remainingLives) else: self.message.setText("Wrong... Only one life left") # hangmanWindow 에 현재 hangman 상태 그림을 출력 self.hangmanWindow.setText(self.hangman.currentShape()) # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력 self.currentWord.setText(self.guess.displayCurrent()) # guessedChars 에 지금까지 이용한 글자들의 집합을 출력 self.guessedChars.setText(self.guess.displayGuessed()) if self.guess.finished(): # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로 self.message.setText("Success!") self.gameOver = True pass elif self.hangman.getRemainingLives() == 0: # 메시지 ("Fail!" + 비밀 단어) 출력하고, self.gameOver 는 True 로 self.currentWord.setText(self.guess.secretWord) self.message.setText("Fail!") self.gameOver = True pass
class HangmanGame(QWidget): def __init__(self, parent=None): super().__init__(parent) # Initialize word database self.word = Word('words.txt') # Hangman display window self.hangmanWindow = QTextEdit() self.hangmanWindow.setReadOnly(True) self.hangmanWindow.setAlignment(Qt.AlignLeft) font = self.hangmanWindow.font() font.setFamily('Courier New') self.hangmanWindow.setFont(font) # Layout hangmanLayout = QGridLayout() hangmanLayout.addWidget(self.hangmanWindow, 0, 0) # Status Layout creation statusLayout = QGridLayout() # Display widget for current status self.currentWord = QLineEdit() self.currentWord.setReadOnly(True) self.currentWord.setAlignment(Qt.AlignCenter) font = self.currentWord.font() font.setPointSize(font.pointSize() + 8) self.currentWord.setFont(font) statusLayout.addWidget(self.currentWord, 0, 0, 1, 2) # Display widget for already used characters self.guessedChars = QLineEdit() self.guessedChars.setReadOnly(True) self.guessedChars.setAlignment(Qt.AlignLeft) self.guessedChars.setMaxLength(52) statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2) # Display widget for message output self.message = QLineEdit() self.message.setReadOnly(True) self.message.setAlignment(Qt.AlignLeft) self.message.setMaxLength(52) statusLayout.addWidget(self.message, 2, 0, 1, 2) # Input widget for user selected characters self.charInput = QLineEdit() #self.charInput.setMaxLength(1) statusLayout.addWidget(self.charInput, 3, 0) # Button for submitting a character self.guessButton = QToolButton() self.guessButton.setText('Guess!') self.guessButton.clicked.connect(self.guessClicked) statusLayout.addWidget(self.guessButton, 3, 1) # Button for a new game self.newGameButton = QToolButton() self.newGameButton.setText('New Game') self.newGameButton.clicked.connect(self.startGame) statusLayout.addWidget(self.newGameButton, 4, 0) # Layout placement mainLayout = QGridLayout() mainLayout.setSizeConstraint(QLayout.SetFixedSize) mainLayout.addLayout(hangmanLayout, 0, 0) mainLayout.addLayout(statusLayout, 0, 1) self.setLayout(mainLayout) self.setWindowTitle('Hangman Game') # Start a new game on application launch! self.startGame() def startGame(self): self.hangman = Hangman() self.guess = Guess(self.word.randFromDB()) self.gameOver = False self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.clear() def guessClicked(self): guessedChar = self.charInput.text() self.charInput.clear() self.message.clear() if self.gameOver == True: # 메시지 출력하고 - message.setText() - 리턴 self.message.setText('Game Over') return # 입력의 길이가 1 인지를 판단하고, 아닌 경우 메시지 출력, 리턴 if len(guessedChar) != 1: self.message.setText('Please enter only one character!') return # 입력한 문자가 대문자라면 소문자로 변경 # (주석 처리된 코드 : 아스키 코드표는 빈칸을 취급하지 않기 때문에 이 조건문이 맨 앞에 있으면 오류가 뜸) #if 65 <= ord(guessedChar) <= 90: # guessedChar = chr(ord(guessedChar) + 32) if guessedChar.upper(): guessedChar = guessedChar.lower() # 이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴 if guessedChar in self.guess.guessedChars: #if guessedChar in self.guess.displayGuessed(): self.message.setText('You already guessed \"' + guessedChar + '\"') return # 알파벳 이외의 다른 모든 경우의 수를 차단 # (아스키 코드표는 빈칸을 취급하지 않기 때문에 이 조건문이 맨 앞에 있으면 오류가 뜸) if ord(guessedChar) <= 64 or 91 <= ord(guessedChar) <= 96 or 123 <= ord(guessedChar): self.message.setText('Please enter the alphabet!') return success = self.guess.guess(guessedChar) if success == False: # 남아 있는 목숨을 1 만큼 감소 self.hangman.decreaseLife() # 메시지 출력 self.message.setText('Your guess is wrong...') # hangmanWindow 에 현재 hangman 상태 그림을 출력 self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력 self.currentWord.setText(self.guess.displayCurrent()) # guessedChars 에 지금까지 이용한 글자들의 집합을 출력 self.guessedChars.setText(self.guess.displayGuessed()) if self.guess.finished(): # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로 self.message.setText('Success!') self.gameOver = True elif self.hangman.getRemainingLives() == 0: # 메시지 ("Fail!" + 비밀 단어) 출력하고, self.gameOver 는 True 로 self.message.setText('Fail! ' + self.guess.secretWord) self.gameOver = True
class HangmanGame(QWidget): def __init__(self, parent=None): super().__init__(parent) # Initialize word database self.word = Word('words.txt') # Hangman display window self.hangmanWindow = QTextEdit() self.hangmanWindow.setReadOnly(True) self.hangmanWindow.setAlignment(Qt.AlignLeft) font = self.hangmanWindow.font() font.setFamily('Courier New') self.hangmanWindow.setFont(font) # Layout hangmanLayout = QGridLayout() hangmanLayout.addWidget(self.hangmanWindow, 0, 0) # Status Layout creation statusLayout = QGridLayout() # Display widget for current status self.currentWord = QLineEdit() self.currentWord.setReadOnly(True) self.currentWord.setAlignment(Qt.AlignCenter) # 가로길이를 300까지 크기를 키워주면 13글자 이상도 들어 갈 수 있다. self.currentWord.setFixedWidth(300) font = self.currentWord.font() font.setPointSize(font.pointSize() + 8) self.currentWord.setFont(font) statusLayout.addWidget(self.currentWord, 0, 0, 1, 2) # Display widget for already used characters self.guessedChars = QLineEdit() self.guessedChars.setReadOnly(True) self.guessedChars.setAlignment(Qt.AlignLeft) self.guessedChars.setMaxLength(52) statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2) # Display widget for message output self.message = QLineEdit() self.message.setReadOnly(True) self.message.setAlignment(Qt.AlignLeft) self.message.setMaxLength(52) statusLayout.addWidget(self.message, 2, 0, 1, 2) # Input widget for user selected characters self.charInput = QLineEdit() self.charInput.setMaxLength(1) statusLayout.addWidget(self.charInput, 3, 0) # Button for submitting a character self.guessButton = QToolButton() self.guessButton.setText('Guess!') self.guessButton.clicked.connect(self.guessClicked) statusLayout.addWidget(self.guessButton, 3, 1) # Button for a new game self.newGameButton = QToolButton() self.newGameButton.setText('New Game') self.newGameButton.clicked.connect(self.startGame) statusLayout.addWidget(self.newGameButton, 4, 0) # Layout placement mainLayout = QGridLayout() mainLayout.setSizeConstraint(QLayout.SetFixedSize) mainLayout.addLayout(hangmanLayout, 0, 0) mainLayout.addLayout(statusLayout, 0, 1) self.setLayout(mainLayout) self.setWindowTitle('Hangman Game') # Start a new game on application launch! self.startGame() def startGame(self): self.hangman = Hangman() #randFromDB() 괄호 사이에 숫자 입력하여 랜덤 길이 넣어준다. !! word = self.word.randFromDB(15) self.guess = Guess(self.word.randFromDB()) self.gameOver = False self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.clear() def guessClicked(self): guessedChar = self.charInput.text() self.charInput.clear() self.message.clear() if self.gameOver == True: # 메시지 출력하고 - message.setText() - 리턴 self.message.setText("Game Over") # return 추가 return "Finished" if len(guessedChar) != 1: # 입력의 길이가 1 인지를 판단하고, 아닌 경우 메시지 출력, 리턴 self.charInput.setText("1글자만 입력하세요.") return "Finished" # 먼저 guessedChar를 소문자로 변형 guessedChar = guessedChar.lower() if guessedChar in guess.guessedChars: self.message.setText("이미 추측한 글자입니다.") return "Finished" # 이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴 success = self.guess.guess(guessedChar) if success == False: # 남아 있는 목숨을 1 만큼 감소 hangman.decreaseLife() # 메시지 출력 self.message.setText("not " + '"' + guessedChar + '"' + "in the word") # hangmanWindow 에 현재 hangman 상태 그림을 출력 self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력 self.currentWord.setText(self.guess.displayCurrent()) # guessedChars 에 지금까지 이용한 글자들의 집합을 출력 self.guessedChars.setText(self.guess.displayGuessed()) if self.guess.finished(): # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로 self.message.setText("Success!") self.gmaeOver == True elif self.hangman.getRemainingLives() == 0: # 메시지 ("Fail!" + 비밀 단어) 출력하고, self.gameOver 는 True 로 self.message.setText("Fail!" + guess.secretWord) self.gameOver == True
class HangmanGame(QWidget): def __init__(self, parent=None): super().__init__(parent) # Initialize word database self.word = Word('words.txt') # Hangman display window self.hangmanWindow = QTextEdit() self.hangmanWindow.setReadOnly(True) self.hangmanWindow.setAlignment(Qt.AlignLeft) font = self.hangmanWindow.font() font.setFamily('Courier New') self.hangmanWindow.setFont(font) # Layout hangmanLayout = QGridLayout() hangmanLayout.addWidget(self.hangmanWindow, 0, 0) # Status Layout creation statusLayout = QGridLayout() # Display widget for current status self.currentWord = QLineEdit() self.currentWord.setReadOnly(True) self.currentWord.setAlignment(Qt.AlignCenter) font = self.currentWord.font() self.FONTSIZE = font.pointSize() + 8 font.setPointSize(font.pointSize() + 8) self.currentWord.setFont(font) statusLayout.addWidget(self.currentWord, 0, 0, 1, 2) # Display widget for already used characters self.guessedChars = QLineEdit() self.guessedChars.setReadOnly(True) self.guessedChars.setAlignment(Qt.AlignLeft) self.guessedChars.setMaxLength(52) statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2) # Display widget for message output self.message = QLineEdit() self.message.setReadOnly(True) self.message.setAlignment(Qt.AlignLeft) self.message.setMaxLength(52) statusLayout.addWidget(self.message, 2, 0, 1, 2) # Input widget for user selected characters self.charInput = QLineEdit() self.charInput.setMaxLength(1) statusLayout.addWidget(self.charInput, 3, 0) # Button for submitting a character self.guessButton = QToolButton() self.guessButton.setText('Guess!') self.guessButton.clicked.connect(self.guessClicked) statusLayout.addWidget(self.guessButton, 3, 1) # Button for a new game self.newGameButton = QToolButton() self.newGameButton.setText('New Game') self.newGameButton.clicked.connect(self.startGame) statusLayout.addWidget(self.newGameButton, 4, 0) # Layout placement mainLayout = QGridLayout() mainLayout.setSizeConstraint(QLayout.SetFixedSize) mainLayout.addLayout(hangmanLayout, 0, 0) mainLayout.addLayout(statusLayout, 0, 1) self.setLayout(mainLayout) self.setWindowTitle('Hangman Game') # Start a new game on application launch! self.startGame() def startGame(self): self.hangman = Hangman() self.guess = Guess(self.word.randFromDB(30)) self.gameOver = False self.charInput.setDisabled(False) self.guessButton.setDisabled(False) self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.clear() self.message.setText("새 게임!") font = self.currentWord.font() if len(self.currentWord.text()) > 20: font.setPointSize(self.FONTSIZE - (int((len(self.currentWord.text()) - 20) / 4) + 3)) self.currentWord.setFont(font) else: font.setPointSize(self.FONTSIZE) self.currentWord.setFont(font) self.currentWord.setText(self.guess.displayCurrent()) def guessClicked(self): guessedChar = self.charInput.text() self.charInput.clear() self.message.clear() if self.gameOver: # 메시지 출력하고 - message.setText() - 리턴 self.message.setText("이미 끝난 게임입니다.") return # 입력의 길이가 1 인지를 판단하고, 아닌 경우 메시지 출력, 리턴 elif len(guessedChar) != 1: self.message.setText("알파벳 한글자만 입력하세요.") return # 이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴 elif guessedChar in self.guess.guessedChars: self.message.setText("이미 입력된 단어 " + guessedChar) return success = self.guess.guess(guessedChar) if not success: # 남아 있는 목숨을 1 만큼 감소 # 메시지 출력 self.hangman.decreaseLife() self.message.setText(guessedChar + " 는 단어에 없습니다.") # hangmanWindow 에 현재 hangman 상태 그림을 출력 # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력 # guessedChars 에 지금까지 이용한 글자들의 집합을 출력 self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) if self.guess.finished(): # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로 self.message.setText("축하합니다!") self.charInput.setDisabled(True) self.guessButton.setDisabled(True) self.gameOver = True elif self.hangman.getRemainingLives() == 0: # 메시지 ("Fail!" + 비밀 단어) 출력하고, self.gameOver 는 True 로 self.message.setText("졌습니다. 정답은 " + self.guess.secretWord) self.charInput.setDisabled(True) self.guessButton.setDisabled(True) self.gameOver = True
class HangmanGame(QWidget): def __init__(self, parent=None): super().__init__(parent) # Initialize word database self.word = Word('words.txt') # Hangman display window self.hangmanWindow = QTextEdit() self.hangmanWindow.setReadOnly(True) self.hangmanWindow.setAlignment(Qt.AlignLeft) font = self.hangmanWindow.font() font.setFamily('Courier New') font.setBold(True) self.hangmanWindow.setFont(font) # Layout hangmanLayout = QGridLayout() hangmanLayout.addWidget(self.hangmanWindow, 0, 0) # Status Layout creation statusLayout = QGridLayout() # Display widget for current status self.currentWord = QLineEdit() self.currentWord.setReadOnly(True) self.currentWord.setAlignment(Qt.AlignCenter) font = self.currentWord.font() font.setPointSize(font.pointSize() + 10) self.currentWord.setFont(font) statusLayout.addWidget(self.currentWord, 0, 0, 1, 2) # Display widget for already used characters self.guessedChars = QLineEdit() self.guessedChars.setReadOnly(True) self.guessedChars.setAlignment(Qt.AlignLeft) self.guessedChars.setMaxLength(52) statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2) # Display widget for message output self.message = QLineEdit() self.message.setReadOnly(True) self.message.setAlignment(Qt.AlignLeft) self.message.setMaxLength(52) statusLayout.addWidget(self.message, 2, 0, 1, 2) # Input widget for user selected characters self.charInput = QLineEdit() self.charInput.setMaxLength(1) statusLayout.addWidget(self.charInput, 3, 0) # Button for submitting a character self.guessButton = QToolButton() self.guessButton.setText('Guess!') self.guessButton.clicked.connect(self.guessClicked) statusLayout.addWidget(self.guessButton, 3, 1) # Button for a new game self.newGameButton = QToolButton() self.newGameButton.setText('New Game') self.newGameButton.clicked.connect(self.startGame) statusLayout.addWidget(self.newGameButton, 4, 0) # Layout placement mainLayout = QGridLayout() mainLayout.setSizeConstraint(QLayout.SetFixedSize) mainLayout.addLayout(hangmanLayout, 0, 0) mainLayout.addLayout(statusLayout, 0, 1) self.setLayout(mainLayout) self.setWindowTitle('Hangman Game') # Start a new game on application launch! self.startGame() def startGame(self): self.hangman = Hangman() self.guess = Guess(self.word.randFromDB(5, 9)) self.gameOver = False self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.clear() def guessClicked(self): guessedChar = self.charInput.text().lower() self.charInput.clear() self.message.clear() if self.gameOver == True: self.message.setText('Game Over!') return if not str(guessedChar).isalpha(): self.message.setText("only english") return if len(guessedChar) != 1: self.message.setText('One Character at a time!') return elif guessedChar in self.guess.guessedChars: self.message.setText('You already guessed \"' + guessedChar + "\"!") return success = self.guess.guess(guessedChar) if not success: self.hangman.decreaseLife() self.message.setText('There are no \"' + guessedChar + "\"!") else: self.message.setText('There are \"' + guessedChar + "\"!") self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) if self.guess.finished(): self.message.setText('Success!') self.gameOver = True elif self.hangman.getRemainingLives() == 0: self.message.setText('Fail! ' + self.guess.secretWord) self.gameOver = True
class HangmanGame(QWidget): def __init__(self, parent=None): super().__init__(parent) # Initialize word database self.word = Word('words.txt',15) # 최소 15자 이상의 단어만 # Hangman display window self.hangmanWindow = QTextEdit() self.hangmanWindow.setReadOnly(True) self.hangmanWindow.setAlignment(Qt.AlignLeft) font = self.hangmanWindow.font() font.setFamily('Courier New') self.hangmanWindow.setFont(font) # Layout hangmanLayout = QGridLayout() hangmanLayout.addWidget(self.hangmanWindow, 0, 0) # Status Layout creation statusLayout = QGridLayout() # Display widget for current status self.currentWord = QLineEdit() self.currentWord.setReadOnly(True) self.currentWord.setAlignment(Qt.AlignCenter) font = self.currentWord.font() font.setPointSize(font.pointSize() + 6) # 8로 하면 단어가 넘쳐서 6으로 수정 self.currentWord.setFont(font) statusLayout.addWidget(self.currentWord, 0, 0, 1, 30) # CurrentWord Textbox 의 길이 2->30 수정 # Display widget for already used characters self.guessedChars = QLineEdit() self.guessedChars.setReadOnly(True) self.guessedChars.setAlignment(Qt.AlignLeft) self.guessedChars.setMaxLength(52) statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2) # Display widget for message output self.message = QLineEdit() self.message.setReadOnly(True) self.message.setAlignment(Qt.AlignLeft) self.message.setMaxLength(52) statusLayout.addWidget(self.message, 2, 0, 1, 2) # Input widget for user selected characters self.charInput = QLineEdit() self.charInput.setMaxLength(1) statusLayout.addWidget(self.charInput, 3, 0) # Button for submitting a character self.guessButton = QToolButton() self.guessButton.setText('Guess!') self.guessButton.clicked.connect(self.guessClicked) statusLayout.addWidget(self.guessButton, 3, 1) # Button for a new game self.newGameButton = QToolButton() self.newGameButton.setText('New Game') self.newGameButton.clicked.connect(self.startGame) statusLayout.addWidget(self.newGameButton, 4, 0) # Layout placement mainLayout = QGridLayout() mainLayout.setSizeConstraint(QLayout.SetFixedSize) mainLayout.addLayout(hangmanLayout, 0, 0) mainLayout.addLayout(statusLayout, 0, 1) self.setLayout(mainLayout) self.setWindowTitle('Hangman Game') # Start a new game on application launch! self.startGame() def startGame(self): self.hangman = Hangman() self.guess = Guess(self.word.randFromDB()) self.gameOver = False self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.clear() def guessClicked(self): guessedChar = self.charInput.text() self.charInput.clear() self.message.clear() if self.gameOver == True: self.message.setText("END") #추가:메시지 출력하고 - message.setText() - 리턴 return if len(guessedChar) != 1: self.message.setText("하나의 문자만 입력이 가능합니다.") #추가: self.message.setText("하나의 문자만 입력이 가능합니다.") return if guessedChar in self.guess.guessedChars: self.message.setText("시도 해본 문자입니다.") #추가:이미 사용한 글자인지를 판단하고, 아닌 경우 메시지 출력, 리턴 return success = self.guess.guess(guessedChar) if success == False: self.hangman.decreaseLife() # 남아 있는 목숨을 1 만큼 감소 self.message.setText(guessedChar + '는 단어 안에 없습니다.') # 메시지 출력 self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) # hangmanWindow 에 현재 hangman 상태 그림을 출력 self.currentWord.setText(self.guess.displayCurrent()) # currentWord 에 현재까지 부분적으로 맞추어진 단어 상태를 출력 self.guessedChars.setText(self.guess.displayGuessed()) # guessedChars 에 지금까지 이용한 글자들의 집합을 출력 if self.guess.finished(): self.message.setText("Success!") # 메시지 ("Success!") 출력하고, self.gameOver 는 True 로 self.gameOver = True elif self.hangman.getRemainingLives() == 0: self.message.setText("Fail! : " + self.guess.secretWord) self.gameOver = True
class HangmanGame(QWidget): def __init__(self, parent=None): super().__init__(parent) self.word = Word('words.txt') #Hangman 상태 표시 self.hangmanWindow = QTextEdit() self.hangmanWindow.setReadOnly(True) # 왼쪽 정렬 (그래야 올바르게 보임) self.hangmanWindow.setAlignment(Qt.AlignLeft) font = self.hangmanWindow.font() #고정 폭(fixed-with) 글꼴을 이용. font.setFamily('Courier New') self.hangmanWindow.setFont(font) # hangmanLayout을 만들고 그 안에 위젯을 배치. hangmanLayout = QGridLayout() hangmanLayout.addWidget(self.hangmanWindow, 0, 0) self.showRemainingLife = QLineEdit() self.showRemainingLife.setReadOnly(True) #status Layout 만들기. statusLayout = QGridLayout() #currentWord-사용자가 입력한 단어가 맞는지 표시 self.currentWord = QLineEdit() self.currentWord.setReadOnly(True) #읽기 전용.사용자가 편집불가. self.currentWord.setAlignment(Qt.AlignCenter) #가운데 정렬. font = self.currentWord.font() font.setPointSize(font.pointSize() + 8) #글꼴 크기. self.currentWord.setFont(font) # statusLayout에 위젯 배치. # (0,0,1,2)는 0행 0열 가로 1 세로 2 statusLayout.addWidget(self.currentWord, 0, 0, 1, 2) #guessedChars-사용자가 입력한 단어들 표시. self.guessedChars = QLineEdit() self.guessedChars.setReadOnly(True) self.guessedChars.setAlignment(Qt.AlignLeft) self.guessedChars.setMaxLength(52) # statusLayout에 위젯 배치. statusLayout.addWidget(self.guessedChars, 1, 0, 1, 2) #message-상태를 알려주는 메세지를 보임. self.message = QLineEdit() self.message.setReadOnly(True) self.message.setAlignment(Qt.AlignLeft) self.message.setMaxLength(52) # statusLayout에 위젯 배치. statusLayout.addWidget(self.message, 2, 0, 1, 2) #charInput-사용자가 단어를 입력할 칸. self.charInput = QLineEdit() self.charInput.setMaxLength(1) statusLayout.addWidget(self.charInput, 3, 0) #3행 0열 #guessButton-사용자가 단어를 입력하고 누를 버튼. self.guessButton = QToolButton() self.guessButton.setText('Guess!') self.guessButton.clicked.connect(self.guessClicked) # statusLayout에 위젯 배치. statusLayout.addWidget(self.guessButton, 3, 1) #3행 1열 # newGameButton-새로운 게임을 다시 시작하기 위해 누를 버튼. self.newGameButton = QToolButton() self.newGameButton.setText('New Game') self.newGameButton.clicked.connect(self.startGame) statusLayout.addWidget(self.newGameButton, 4, 0) statusLayout.addWidget(self.showRemainingLife, 4, 1) #mainLayout을 만들고 위에서 구성한 두 레이아웃을 가로로 배치. mainLayout = QGridLayout() mainLayout.setSizeConstraint(QLayout.SetFixedSize) mainLayout.addLayout(hangmanLayout, 0, 0) mainLayout.addLayout(statusLayout, 0, 1) self.setLayout(mainLayout) self.setWindowTitle('Hangman Game') self.startGame() def startGame(self): self.hangman = Hangman() self.guess = Guess(self.word.randFromDB(9)) print(self.guess.secretWord) self.gameOver = False self.charInput.setReadOnly(False) self.hangmanWindow.setText(self.hangman.currentShape()) #self.hangmanWindow.setPlaceholderText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.clear() def guessClicked(self): guessedChar = self.charInput.text() self.charInput.clear() self.message.clear() if self.gameOver == True: self.message.setText("Game Over") if len(guessedChar) != 1: self.message.setText("Input wrong word") if guessedChar in self.guess.guessedChars: self.message.setText("Already used") success = self.guess.guess(guessedChar) if success == False: self.hangman.decreaseLife() self.hangmanWindow.setText(self.hangman.currentShape()) self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.setText("No " + guessedChar + " in the word") self.showRemainingLife.setText("try : " + str(self.hangman.remainingLives)) elif success == True: self.currentWord.setText(self.guess.displayCurrent()) self.guessedChars.setText(self.guess.displayGuessed()) self.message.setText(guessedChar + " is in the word!") self.showRemainingLife.setText("try : " + str(self.hangman.remainingLives)) elif success == "alpha": self.message.setText("Input wrong value") if self.guess.finished(): self.message.setText("Success!") self.gameOver = True self.charInput.setReadOnly(True) self.hangman.RemainingLives = 6 if self.hangman.getRemainingLives() == 0: self.message.setText("Fail! " + self.guess.secretWord) self.gameOver = True self.charInput.setReadOnly(True) self.hangman.RemainingLives = 6