Skip to content

Latest commit

 

History

History
148 lines (124 loc) · 4.41 KB

211209.md

File metadata and controls

148 lines (124 loc) · 4.41 KB

Day 65

파이썬으로 배우는 게임 개발 입문편

체크 박스를 활용한 진단 테스트 게임

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

class WindowClass(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUI()
        self.show()

    def setupUI(self):
        self.setGeometry(100, 100, 800, 600)
        self.setWindowTitle('고양이 지수 진단 게임')

        oImage = QImage('Python_workspace\mini_game\image_dir\mina.png')
        sImage = oImage.scaled(QSize(800, 600))
        palette = QPalette()
        palette.setBrush(10, QBrush(sImage))
        self.setPalette(palette)

        button = QPushButton('진단하기', self)
        button.setFont(QFont('Times New Roman', 32))
        button.move(400, 500)
        button.setStyleSheet('background-color: #d2e9af')

        self.res_Label = QLabel(self)
        self.res_Label.setFixedSize(400, 100)
        self.res_Label.move(320, 30)
        self.res_Label.setStyleSheet('background-color: #ffffff')

        Questions = [
            '높은 곳이 좋다', 
            '공을 보면 굴리고 싶어진다', 
            '깜짝 놀라면 털이 곤두선다', 
            '쥐구멍이 마음에 든다', 
            '개에게 적대감을 느낀다', 
            '생선 뼈를 발라 먹고 싶다', 
            '야행성이다']
        self.cbtn = [None] * 7 # 체크 버튼 질문 정의용

        for i in range(7):
            self.cbtn[i] = QCheckBox(Questions[i], self)
            self.cbtn[i].move(400, 165 + 40 * i)
            self.cbtn[i].setFont(QFont('Times New Roman', 12))

        button.clicked.connect(self.btn_clicked)

    def btn_clicked(self):
        count = 0
        Test_result = [
            '0.1 고양이',
            '0.2 고양이',
            '0.3 고양이',
            '0.5 고양이',
            '0.7 고양이',
            '0.8 고양이',
            '0.9 고양이',
            '1.0 고양이'
        ]
        for i in range(7):
            if self.cbtn[i].isChecked() == True:
                count += 1
        percentage = int(count / 7 * 100)
        self.res_Label.setText('체크된 수는 ' + str(count) + '개\n고양이 지수는 ' + str(percentage) + '%입니다\n' + Test_result[count])

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_W = WindowClass()
    sys.exit(app.exec_())

이쪽에서는 반복문으로 체크버튼을 단체로 관리하는 부분을 눈여겨 봤다.

실시간 처리

이전 프로젝트에서 좋지 않다고 느낀 점이 응답 없음이 지속적으로 발생한다는 것인데 이에 대한 해결 방법으로 실시간 처리가 있었다.

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
import time

class Thread1(QThread): # 실시간 처리를 위한 클래스
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent # 이러면 WindowClass의 위젯을 건들 수 있음

    def run(self): # 실시간 처리를 할 내용
        for i in range(100):
            self.parent.label.setText(str(i))
            time.sleep(1)

class WindowClass(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUI()
        self.show()

    def setupUI(self):
        self.setGeometry(100, 100, 800, 600)

        self.label = QLabel(self)

        self.btn = QPushButton(self)
        self.btn.move(30, 30)
        self.btn.clicked.connect(self.btn_clicked)

    def btn_clicked(self):
        x = Thread1(self)
        x.start()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_W = WindowClass()
    sys.exit(app.exec_())

QThread를 사용하면 실시간 처리가 가능하다.

키 입력 받기(이벤트)

PyQt의 이벤트 핸들러는 여러가지가 있는데 자세한 것은 링크 참조

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

class WindowClass(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUI()
        self.show()

    def setupUI(self):
        self.setGeometry(100, 100,800, 600)

    def keyPressEvent(self, e):
        print(e.key())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_W = WindowClass()
    sys.exit(app.exec_())

입력한 키보드의 아스키 코드가 출력된다.