JS Coding
[Python] 파이썬 통한 오목게임 만들기 본문
import sys
from PyQt5 import uic, QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.Qt import QPushButton, QSize, QMessageBox
form_class = uic.loadUiType("omok03_19.ui")[0]
class MainClass(QMainWindow, form_class):
def __init__(self) :
QMainWindow.__init__(self)
self.setupUi(self)
self.flag_wb = True
self.flag_over = False
self.arr2D = [
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0],
[0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0]
]
self.l2D =[]
for i in range(19):
line = []
for j in range(19):
lego = QPushButton('', self)
lego.setIcon(QtGui.QIcon('0.png'))
lego.setToolTip("{},{}".format(i,j))
lego.setGeometry(j*40, i*40, 40, 40)
lego.setIconSize(QSize(40, 40))
lego.clicked.connect(self.myclick)
line.append(lego)
self.l2D.append(line)
self.show()
self.pb.clicked.connect(self.myreset)
self.myrender()
def myreset(self):
for i in range(19):
for j in range(19):
self.arr2D[i][j]=0
self.myrender()
self.flag_over=False
self.flag_wb=True
def myrender(self):
for i in range(19):
for j in range(19):
if self.arr2D[i][j] == 0 :
self.l2D[i][j].setIcon(QtGui.QIcon('0.png'))
if self.arr2D[i][j] == 1 :
self.l2D[i][j].setIcon(QtGui.QIcon('2.png'))
if self.arr2D[i][j] == 2 :
self.l2D[i][j].setIcon(QtGui.QIcon('1.png'))
def myclick(self):
if self.flag_over:
return
str_ij = self.sender().toolTip()
arr_ij = str_ij.split(",")
i = int(arr_ij[0])
j = int(arr_ij[1])
if self.arr2D[i][j]>0:
return
stone = -1
if self.flag_wb :
self.arr2D[i][j]=1
stone = 1
else:
self.arr2D[i][j]=2
stone = 2
up = self.getUP(i,j,stone)
dw = self.getDW(i,j,stone)
le = self.getLE(i,j,stone)
ri = self.getRI(i,j,stone)
ul = self.getUL(i,j,stone)
ur = self.getUR(i,j,stone)
dl = self.getDL(i,j,stone)
dr = self.getDR(i,j,stone)
d1 = up + dw + 1
d2 = le + ri + 1
d3 = ul + dr + 1
d4 = ur + dl + 1
if d1 == 5 or d2 == 5 or d3 == 5 or d4 == 5 :
if self.flag_wb == True:
QMessageBox.about(self,'GAME OVER',"흑돌 승리")
self.flag_over = True
elif self.flag_wb == False:
QMessageBox.about(self,'GAME OVER',"백돌 승리")
self.flag_over = True
self.myrender()
self.flag_wb = not self.flag_wb
def getDR(self,i,j,stone):
cnt = 0
try:
while True:
i += +1
j += +1
if i <0 :
return cnt
if j <0 :
return cnt
if self.arr2D[i][j]==stone:
cnt += 1
else :
return cnt
except:
return cnt
def getDL(self,i,j,stone):
cnt = 0
try:
while True:
i += +1
j += -1
if i <0 :
return cnt
if j <0 :
return cnt
if self.arr2D[i][j]==stone:
cnt += 1
else :
return cnt
except:
return cnt
def getUR(self,i,j,stone):
cnt = 0
try:
while True:
i += -1
j += +1
if i <0 :
return cnt
if j <0 :
return cnt
if self.arr2D[i][j]==stone:
cnt += 1
else :
return cnt
except:
return cnt
def getUL(self,i,j,stone):
cnt = 0
try:
while True:
i += -1
j += -1
if i <0 :
return cnt
if j <0 :
return cnt
if self.arr2D[i][j]==stone:
cnt += 1
else :
return cnt
except:
return cnt
def getUP(self,i,j,stone):
cnt = 0
try:
while True:
i += -1
if i <0 :
return cnt
if j <0 :
return cnt
if self.arr2D[i][j]==stone:
cnt += 1
else :
return cnt
except:
return cnt
def getDW(self,i,j,stone):
cnt = 0
try:
while True:
i += +1
if i <0 :
return cnt
if j <0 :
return cnt
if self.arr2D[i][j]==stone:
cnt += 1
else :
return cnt
except:
return cnt
def getLE(self,i,j,stone):
cnt = 0
try:
while True:
j += -1
if i <0 :
return cnt
if j <0 :
return cnt
if self.arr2D[i][j]==stone:
cnt += 1
else :
return cnt
except:
return cnt
def getRI(self,i,j,stone):
cnt = 0
try:
while True:
j += +1
if i <0 :
return cnt
if j <0 :
return cnt
if self.arr2D[i][j]==stone:
cnt += 1
else :
return cnt
except:
return cnt
if __name__ == "__main__" :
app = QApplication(sys.argv)
window = MainClass()
app.exec_()
기능 구현
<?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>921</width>
<height>917</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pb">
<property name="geometry">
<rect>
<x>830</x>
<y>20</y>
<width>61</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>RESET</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>921</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
MySwing 이미지 구현
'Python' 카테고리의 다른 글
[Python] 파이썬으로 테트리스 게임 만들기 (0) | 2024.01.03 |
---|