[Python] pyqt5 활용한 숫자 야구 게임

애옹·2024년 7월 2일

Python

목록 보기
8/13

day5/pyqt10.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>222</width>
    <height>348</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="lbl">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>30</y>
      <width>56</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>야구게임</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="le">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>30</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>60</y>
      <width>181</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>스트라이크 맞히기</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="te">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>100</y>
      <width>181</width>
      <height>221</height>
     </rect>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

day5/pyqt10.py

from random import random
import sys

from PyQt5 import uic 
from PyQt5.Qt import QMessageBox, QPlainTextEdit
from PyQt5.QtWidgets import QApplication, QMainWindow


form_class = uic.loadUiType("pyqt10.ui")[0]

class WindowClass(QMainWindow, form_class): 
    def __init__(self): 
        super().__init__() 
        self.setupUi(self)
        #숫자 한 번만 생성하기
        self.comNum()
        #엔터 활성화
        self.le.returnPressed.connect(self.checkAnswer)
        self.pb.clicked.connect(self.checkAnswer)
        
    def comNum(self):
        arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        
        #섞기
        for i in range(1000):
            rnd = int(random() * 9)
            a = arr[0]
            arr[0] = arr[rnd]
            arr[rnd] = a
            
        #컴퓨터 숫자 세 개 선택
        self.comArr = [arr[0], arr[1], arr[2]]
        #print("comArr:",self.comArr)
      
    def checkAnswer(self):
    	#le에 포커스 활성화
        self.le.setFocus()
        user = self.le.text()
        userArr = list(user)
        #print("userArr:",userArr)
        
        strike = 0
        ball = 0
        
        for i in range(3):
            if int(userArr[i]) == self.comArr[i]:
                strike += 1
            elif int(userArr[i]) in self.comArr:
                ball += 1
        
        if strike == 3:
            result = "3S    정답입니다"
            self.te.append(result)
            QMessageBox.about(self, 'message', "축하합니다\n" + str(userArr))
            
        else:
            result = "{}S {}B".format(strike, ball)
            self.te.append(result)
        
        self.le.clear()
                        
if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    myWindow = WindowClass() 
    myWindow.show() 
    app.exec_()

∴ 실행 결과

profile
괴발개발

0개의 댓글