본문 바로가기
Python/Python 기초

파이썬(QT)야구게임 만들기 예제

by 미눅스[멘토] 2023. 6. 29.
728x90
from PyQt5 import uic
from PyQt5.QtWidgets import *
from random import random
import sys
form_class = uic.loadUiType("myqt08.ui")[0]
class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.ranC()
        self.setupUi(self)
        self.pb.clicked.connect(self.myclick)
        self.le.returnPressed.connect(self.myclick)
    
    def getS(self,com,mine):
        ret = 0
        c1 = com[0:1]
        c2 = com[1:2]
        c3 = com[2:3]
        
        m1 = mine[0:1]
        m2 = mine[1:2]
        m3 = mine[2:3]
        
        if c1 ==m1: ret+=1
        if c2 ==m2: ret+=1
        if c3 ==m3: ret+=1   
        return ret
    
    def getB(self,com,mine):
        ret = 0
        c1 = com[0:1]
        c2 = com[1:2]
        c3 = com[2:3]
        
        m1 = mine[0:1]
        m2 = mine[1:2]
        m3 = mine[2:3]
        
        if c1 ==m2 or c1==m3: ret+=1
        if c2 ==m1 or c2==m3: ret+=1
        if c3 ==m1 or c3==m2: ret+=1   
        return ret
   
    def ranC(self):
        arr=[1,2,3,4,5,6,7,8,9]
    
        for i in range(100):
            rnd= int(random()*9)
            a = arr[0]
            arr[0] = arr[rnd]
            arr[rnd]=a
   
        self.com = "{}{}{}".format(arr[0],arr[1],arr[2])
        print("self.com: ",self.com)
    
    def myclick(self):
        mine=self.le.text()
        s= self.getS(self.com,mine)
        b= self.getB(self.com,mine)
        str_new = mine+"\t"+str(s)+"S"+str(b)+"B"+"\n"
        str_old = self.te.toPlainText()
       
        self.te.setText(str_new+str_old)
        self.le.setText("")
        
        if s == 3:
            QMessageBox.about(self,'야구게임',mine+"맞췄습니다")
if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()

 

결과


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>800</width>
    <height>600</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>110</x>
      <y>80</y>
      <width>61</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>스트라이크:</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="le">
    <property name="geometry">
     <rect>
      <x>260</x>
      <y>80</y>
      <width>113</width>
      <height>20</height>
     </rect>
    </property>
   </widget>
   <widget class="QPushButton" name="pb">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>130</y>
      <width>271</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>게임하기</string>
    </property>
   </widget>
   <widget class="QTextEdit" name="te">
    <property name="geometry">
     <rect>
      <x>90</x>
      <y>190</y>
      <width>291</width>
      <height>351</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>