본문 바로가기
Coding/Python

[Python & PyQt5] 파이큐티를 활용하여 타자게임 만드는 방법 !! #1

by 포스트it 2024. 1. 31.
728x90
반응형

 

[Python & PyQt5] 파이큐티를 활용하여 타자게임 만드는 방법 !! #1

안녕하세요. pyqt5 라이브러리를 활용하여 간단한 타자게임을 만들기 시리즈를 만들어 보려고 합니다 !

게임은 5초마다 새로운 단어를 화면에 표시하고, 단어를 입력하면 단어를 새로운 단어로 교체 하는 로직입니다 ㅎ

 

예제코드
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit
from PyQt5.QtCore import QTimer, QTime

class TypingGame(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.words = ['apple', 'banana', 'cherry', 'date', 'elderberry']
        self.currentWord = ''

    def initUI(self):
        self.layout = QVBoxLayout()

        self.wordLabel = QLabel('Start typing...', self)
        self.layout.addWidget(self.wordLabel)

        self.inputLine = QLineEdit(self)
        self.inputLine.returnPressed.connect(self.checkWord)
        self.layout.addWidget(self.inputLine)

        self.setLayout(self.layout)
        self.setWindowTitle('Typing Game')
        self.show()

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.newWord)
        self.timer.start(5000)  # Change words every 5 seconds

    def newWord(self):
        import random
        self.currentWord = random.choice(self.words)
        self.wordLabel.setText(self.currentWord)

    def checkWord(self):
        if self.inputLine.text() == self.currentWord:
            self.newWord()
            self.inputLine.clear()
            # Update score here (if implementing scoring system)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = TypingGame()
    sys.exit(app.exec_())
728x90
반응형

댓글