其他分享
首页 > 其他分享> > pygame五子棋再探索

pygame五子棋再探索

作者:互联网

这次继续探索pygame的五子棋实现,和之前不一样,这次我稍微研究了一下
用pyqt调用pygame有点难,下棋的时候容易报错
报错:-1073740791 (0xC0000409)

而不用pyqt,只用pygame就少了很多麻烦

使用pyqt的部分代码:

`
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(576, 309)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(250, 10, 81, 41))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(70, 90, 91, 51))
self.label_2.setObjectName("label_2")
self.doubleSpinBox = QtWidgets.QDoubleSpinBox(self.centralwidget)
self.doubleSpinBox.setGeometry(QtCore.QRect(140, 101, 66, 31))
self.doubleSpinBox.setObjectName("doubleSpinBox")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(190, 180, 131, 61))
self.pushButton.setObjectName("pushButton")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(300, 60, 81, 21))
self.label_5.setObjectName("label_5")
self.spinBox_2 = QtWidgets.QSpinBox(self.centralwidget)
self.spinBox_2.setGeometry(QtCore.QRect(380, 50, 51, 41))
self.spinBox_2.setObjectName("spinBox_2")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(300, 140, 71, 21))
self.label_6.setObjectName("label_6")
self.spinBox_3 = QtWidgets.QSpinBox(self.centralwidget)
self.spinBox_3.setGeometry(QtCore.QRect(380, 130, 51, 41))
self.spinBox_3.setObjectName("spinBox_3")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 576, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    self.pushButton.clicked.connect(MainWindow.enter) # type: ignore
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.label.setText(_translate("MainWindow", "五子棋"))
    self.label_2.setText(_translate("MainWindow", "音量调节"))
    self.pushButton.setText(_translate("MainWindow", "开始游戏"))
    self.label_5.setText(_translate("MainWindow", "棋盘行数"))
    self.label_6.setText(_translate("MainWindow", "棋盘列数"))

`

`
import sys
from PyQt5.QtWidgets import *
from uach import *
from five_chess import *

class My(QMainWindow,Ui_MainWindow):
def init(self, parent=None):
super(My, self).init(parent)
self.setupUi(self)

def enter(self):
    row = self.spinBox_2.value()
    col = self.spinBox_3.value()
    m = Manager(row,col)
    m.main()

if name == 'main':
app = QApplication(sys.argv)
#对象数组
n = int(input('创建几个窗口:'))
m = [My() for _ in range(n+1)]
for i in range(n):
m[i].show()
sys.exit(app.exec())
`

只用pygame的代码:

`
import pygame
import sys

class Manager:
def init(self, BOARD_ROW , BOARD_COL):
self.WHITE = (255, 255, 255)
self.BLACK = (0, 0, 0)
self.BACKCOLOR = (100,130,250)

    self.BOARD_ROW = BOARD_ROW  # 棋盘行数
    self.BOARD_COL = BOARD_COL  # 棋盘列数

    self.LEN_X = 10  # 棋盘至屏幕y轴距离
    self.LEN_Y = 10  # 棋盘至屏幕x轴距离

    self.CHESS_LEN = 50  # 棋盘里每一棋格的大小
    self.CHESS_R = 20  # 棋子半径

    self.SCREEN_X = self.BOARD_COL * self.CHESS_LEN + 2 * self.LEN_X
    self.SCREEN_Y = self.BOARD_ROW * self.CHESS_LEN + 2 * self.LEN_Y

    self.IMG = pygame.image.load('E:/图片/chessboard.jpg')
    self.IMG = pygame.transform.scale(self.IMG,(self.CHESS_LEN,self.CHESS_LEN))

    self.screen = pygame.display.set_mode((self.SCREEN_X,self.SCREEN_Y+50))
    #把窗口加宽50,是因为便于显示字体
    self.title = pygame.display.set_caption('五子棋')

    self.isblack = True
    self.boardcount = [[0 for i in range(BOARD_COL)] for i in range(BOARD_ROW)]

def draw(self):
    for i in range(self.BOARD_ROW):
        for j in range(self.BOARD_COL):
            position_x = self.LEN_Y+j*self.CHESS_LEN
            position_y = self.LEN_X+i*self.CHESS_LEN
            self.screen.blit(self.IMG,(position_x,position_y))

def draw2(self):
    for row in range(len(self.boardcount)):
        for col in range(len(self.boardcount[row])):
            position_x = self.LEN_Y+col*self.CHESS_LEN
            position_y = self.LEN_X+row*self.CHESS_LEN#定位至某个格子
            posx = position_x+self.CHESS_LEN/2
            posy = position_y+self.CHESS_LEN/2#在格子的中心画圆
            if self.boardcount[row][col] == 1:
                pygame.draw.circle(self.screen,self.BLACK,(posx,posy),self.CHESS_R)
            if self.boardcount[row][col] == -1:
                pygame.draw.circle(self.screen,self.WHITE,(posx,posy),self.CHESS_R)

def in_area(self,x,y):
    return self.LEN_X <= x <= self.SCREEN_X-self.LEN_X and \
           self.LEN_Y <= y <= self.SCREEN_Y-self.LEN_Y

def make_move(self,x,y):
    if self.boardcount[y][x] == 0:
        self.boardcount[y][x] = 1 if self.isblack else -1
        return True
    return False

def winner(self,a,x,y):
    if self.heng(x,y) or self.shu(x) or self.xie(x,y):
        return 1 if a else -1
    return 0

def heng(self,x,y):
    row = self.boardcount[y]
    left_start = max(0,x-4)
    left_end = x
    for i in range(left_start,left_end+1):
        if abs(sum(row[i:i+5])) == 5:
            return True
    return False

def shu(self,x):
    col = self.boardcount[x]
    top_start = max(0, x- 4)
    top_end = x
    for i in range(top_start, top_end + 1):
        if abs(sum(col[i:i + 5])) == 5:
            return True
    return False

def xie(self,x,y):
    all = []
    for i in range(-4,5):
        if 0 <= x+i < self.BOARD_ROW and 0 <= y+i < self.BOARD_COL:
            all.append(self.boardcount[y+i][x+i])
    roll = self.r(all,5)
    if 5 in roll:
        return True

    all = []
    for i in range(-4, 5):
        if 0 <= x + i < self.BOARD_ROW and 0 <= y - i < self.BOARD_COL:
            all.append(self.boardcount[y - i][x + i])
    roll = self.r(all, 5)
    if 5 in roll:
        return True
    return False

def r(self,a,b):
    r = []
    for i in range(len(a)-b+1):
        r.append(abs(sum(a[i:i+b])))
    return r

def end(self,a):
    winner = '黑棋' if a else '白棋'
    font = pygame.font.SysFont(['方正粗黑宋简体', 'microsoftsansserif'], 50)
    text = font.render(winner+'获胜了', True, (255, 0, 0), None)
    r = text.get_rect()
    r.topleft = (0,self.SCREEN_Y)
    self.screen.blit(text, r)

def main(self):
    while True:
        pygame.init()
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if e.type == pygame.MOUSEBUTTONUP:
                click_x,click_y = pygame.mouse.get_pos()#获取鼠标位置
                if self.in_area(click_x,click_y):
                    xx = (click_x-self.LEN_X)//self.CHESS_LEN
                    yy = (click_y-self.LEN_Y)//self.CHESS_LEN
                    self.isblack = not self.isblack if self.make_move(xx,yy) else self.isblack
                    if self.winner(self.isblack,xx,yy):
                        self.end(not self.isblack)
                        self.isblack = True
                        self.boardcount = [[0 for i in range(self.BOARD_COL)]
                                           for i in range(self.BOARD_ROW)]
        self.screen.fill(self.BACKCOLOR)
        self.draw()  # 画棋盘
        self.draw2()  # 画棋子
        pygame.display.flip()

r = int(input('棋盘有几行?'))
c = int(input('棋盘有几列?'))
m = Manager(r,c)
m.main()
`

标签:探索,self,五子棋,LEN,label,pygame,QtWidgets,MainWindow,CHESS
来源: https://www.cnblogs.com/karl-lighting/p/pygame-chess_down.html