반응형
[Python & PyQt5] 파이큐티를 활용하여 타자게임 만드는 방법 !! #3
안녕하세요. pyqt5 라이브러리를 활용하여 간단한 타자게임을 만들기 시리즈 입니다.
추가 사항
- 점수 표시
- 시간바 표시
예제코드
import sys
import random
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QProgressBar
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QFont, QColor
class TypingGame(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.words = ['apple', 'banana', 'cherry', 'date', 'elderberry']
self.currentWord = ''
self.gameStarted = False
self.score = 0
self.gameTime = 0
def initUI(self):
self.layout = QVBoxLayout()
self.setStyleSheet("background-color: #f0f0f0;") # 배경색 변경
self.timeInputLine = QLineEdit(self)
self.timeInputLine.setPlaceholderText('Enter game time in seconds')
self.timeInputLine.setFont(QFont('Arial', 12)) # 폰트 변경
self.layout.addWidget(self.timeInputLine)
self.startButton = QPushButton('Start Game', self)
self.startButton.clicked.connect(self.startGame)
self.startButton.setStyleSheet("QPushButton { background-color: #4CAF50; color: white; border-radius: 10px; }"
"QPushButton:disabled { background-color: #A5D6A7; }"
"QPushButton:hover { background-color: #66BB6A; }") # 버튼 스타일 변경
self.startButton.setFont(QFont('Arial', 14, QFont.Bold))
self.layout.addWidget(self.startButton)
self.wordLabel = QLabel('Start typing...', self)
self.wordLabel.setAlignment(Qt.AlignCenter)
self.wordLabel.setFont(QFont('Courier', 18, QFont.Bold))
self.layout.addWidget(self.wordLabel)
self.scoreLabel = QLabel('Score: 0', self)
self.scoreLabel.setFont(QFont('Arial', 12))
self.layout.addWidget(self.scoreLabel)
self.progressBar = QProgressBar(self) # 진행 상황 표시
self.progressBar.setMaximum(100)
self.layout.addWidget(self.progressBar)
self.inputLine = QLineEdit(self)
self.inputLine.returnPressed.connect(self.checkWord)
self.inputLine.setFont(QFont('Arial', 12))
self.layout.addWidget(self.inputLine)
self.setLayout(self.layout)
self.setWindowTitle('Typing Game')
self.show()
self.gameTimer = QTimer(self)
self.wordTimer = QTimer(self)
self.wordTimer.timeout.connect(self.newWord)
def startGame(self):
if not self.gameStarted:
self.gameTime = int(self.timeInputLine.text())
self.progressBar.setMaximum(self.gameTime)
self.gameTimer.timeout.connect(self.updateProgressBar)
self.gameTimer.start(1000) # 매초마다 업데이트
self.wordTimer.start(5000)
self.newWord()
self.gameStarted = True
self.startButton.setDisabled(True)
self.timeInputLine.setDisabled(True)
def updateProgressBar(self):
self.gameTime -= 1
self.progressBar.setValue(self.progressBar.maximum() - self.gameTime)
if self.gameTime <= 0:
self.endGame()
def newWord(self):
self.currentWord = random.choice(self.words)
self.wordLabel.setText(self.currentWord)
def checkWord(self):
if self.inputLine.text() == self.currentWord:
self.score += 1
self.scoreLabel.setText(f'Score: {self.score}')
self.newWord()
self.inputLine.clear()
def endGame(self):
self.gameTimer.stop()
self.wordTimer.stop()
self.wordLabel.setText(f'Game Over! Final Score: {self.score}')
self.gameStarted = False
self.startButton.setDisabled(False)
self.timeInputLine.setDisabled(False)
self.inputLine.clear()
self.progressBar.setValue(0)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = TypingGame()
sys.exit(app.exec_())
코드 변경사항
결과
728x90
반응형
댓글