PYTHON
PYQT 베이스볼 게임 만들기
9400
2023. 1. 3. 14:32
<?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>748</width>
<height>602</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="lbl1">
<property name="geometry">
<rect>
<x>70</x>
<y>30</y>
<width>71</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>스크라이크</string>
</property>
</widget>
<widget class="QLineEdit" name="le">
<property name="geometry">
<rect>
<x>140</x>
<y>30</y>
<width>111</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>80</x>
<y>80</y>
<width>171</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>맞추기</string>
</property>
</widget>
<widget class="QTextEdit" name="te">
<property name="geometry">
<rect>
<x>80</x>
<y>130</y>
<width>171</width>
<height>191</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>748</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
코드
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
import random
form_class = uic.loadUiType("main0b.ui")[0]
class WindowClass(QMainWindow, form_class) :
def __init__(self) :
super().__init__()
self.setupUi(self)
self.com =1
self.pb.clicked.connect(self.myclick)
self.comrandom()
def comrandom(self):
arr = ["1","2","3","4","5","6","7","8","9","0"]
self.com=""
for i in range(1000):
rnd = int(random.random()*len(arr))
a = arr[rnd]
b = arr[0]
arr[0] = a
arr[rnd] = b
self.com = arr[0]+arr[1]+arr[2]
print(self.com)
def myclick(self):
mine = self.le.text()
s = self.getStrike(self.com,mine)
b = self.getBall(self.com,mine)
str_old = self.te.toPlainText()
str_new = mine+" : "+str(s)+"strike,"+str(b)+"ball"+"\n"
self.te.setPlainText(str_old+" "+str_new)
if s==3:
QMessageBox.about(self,'정답',mine+"정답입니다.")
def getStrike(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 getBall(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
if __name__ == "__main__" :
#QApplication : 프로그램을 실행시켜주는 클래스
app = QApplication(sys.argv)
#WindowClass의 인스턴스 생성
myWindow = WindowClass()
#프로그램 화면을 보여주는 코드
myWindow.show()
#프로그램을 이벤트루프로 진입시키는(프로그램을 작동시키는) 코드
app.exec_()