溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python怎么實(shí)現(xiàn)五子棋雙人對弈

發(fā)布時(shí)間:2022-05-05 10:18:54 來源:億速云 閱讀:222 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“python怎么實(shí)現(xiàn)五子棋雙人對弈”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“python怎么實(shí)現(xiàn)五子棋雙人對弈”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

我用的是pygame模塊來制作窗口

代碼如下:

# 1、引入pygame 和 pygame.locals
import pygame
from pygame.locals import *
import time
import sys
 
initChessList = []
initRole = 2 # 代表白子下 2:代表當(dāng)前是黑子下
resultFlag  = 0
userFlag = True
 
class StornPoint():
    def __init__(self, x, y, value = 0):
        '''
        :param x: 代表x軸坐標(biāo)
        :param y: 代表y軸坐標(biāo)
        :param value: 當(dāng)前坐標(biāo)點(diǎn)的棋子:0:沒有棋子 1:白子 2:黑子
        '''
        self.x = x
        self.y = y
        self.value = value
        pass
 
 
def initChessSquare(x, y):
    '''
    初始化棋盤的坐標(biāo)
    :param x:
    :param y:
    :return:
    '''
    # 使用二維列表保存了棋盤是的坐標(biāo)系,和每個(gè)落子點(diǎn)的數(shù)值
    for i in range(15):      # 每一行的交叉點(diǎn)坐標(biāo)
        rowList = []
        for j in range(15):  # 每一列的交叉點(diǎn)坐標(biāo)
            pointX = x + j*40
            pointY = y + i*40
            # value  = 0
            sp = StornPoint(pointX, pointY, 0)
            rowList.append(sp)
            pass
        initChessList.append(rowList)
    pass
 
# 處理事件
def eventHandler():
    global userFlag
    '''
    監(jiān)聽各種事件
    :return:
    '''
    for event in pygame.event.get():
 
        global  initRole
        # 監(jiān)聽點(diǎn)積退出按鈕事件
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
            pass
        # 監(jiān)聽鼠標(biāo)點(diǎn)積事件
        if event.type == MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos() #
            print((x, y))
            i = j = 0
            for temp in initChessList:
                for point in temp:
                    if   x >= point.x - 15 and x <= point.x + 15 \
                        and y >= point.y - 15 and y <= point.y + 15:
                        # 當(dāng)前區(qū)域沒有棋子,并且是白子下
                        if point.value == 0 and initRole == 1 and userFlag:
                            point.value = 1
                            judgeResult(i, j, 1)
                            initRole = 2    # 切換棋子顏色
                            pass
                        elif point.value == 0 and initRole == 2 and userFlag:
                            point.value = 2
                            judgeResult(i, j, 2)
                            initRole = 1    # 切換棋子顏色
                            pass
                        break
                        pass
                    j += 1
                    pass
                i += 1
                j = 0
            pass
        pass
 
    pass
 
# 判斷輸贏函數(shù)
def judgeResult(i, j, value):
    global resultFlag
 
    flag = False  # 用于判斷是否已經(jīng)判決出輸贏
    for  x in  range(j - 4, j + 5):  # 水平方向有沒有出現(xiàn)5連
        if x >= 0 and x + 4 < 15 :
            if initChessList[i][x].value == value and \
                initChessList[i][x + 1].value == value and \
                initChessList[i][x + 2].value == value and \
                initChessList[i][x + 3].value == value and \
                initChessList[i][x + 4].value == value :
                flag = True
                break
                pass
    for x in range(i - 4, i + 5):  # 垂直方向有沒有出現(xiàn)5連
        if x >= 0 and x + 4 < 15:
            if initChessList[x][j].value == value and \
                    initChessList[x + 1][j].value == value and \
                    initChessList[x + 2][j].value == value and \
                    initChessList[x + 3][j].value == value and \
                    initChessList[x + 4][j].value == value:
                flag = True
                break
                pass
 
    # 判斷東北方向的對角線是否出現(xiàn)了5連
    for x, y in zip(range(j + 4, j - 5, -1), range(i - 4, i + 5)):
        if x >= 0 and x+4  < 15 and y + 4 >= 0 and y < 15:
            if initChessList[y][x].value == value and \
                    initChessList[y - 1][x + 1].value == value and \
                    initChessList[y - 2][x + 2].value == value and \
                    initChessList[y - 3][x + 3].value == value and \
                    initChessList[y - 4][x + 4].value == value:
                flag = True
                break
                pass
            pass
        pass
 
    # 判斷西北方向的對角是否出現(xiàn)了五連
    for x, y in zip(range(j - 4, j + 5), range(i - 4, i + 5)):
        if x >= 0 and x + 4 < 15 and y >= 0 and y + 4 < 15:
            if initChessList[y][x].value == value and \
                    initChessList[y + 1][x + 1].value == value and \
                    initChessList[y + 2][x + 2].value == value and \
                    initChessList[y + 3][x + 3].value == value and \
                    initChessList[y + 4][x + 4].value == value:
                flag = True
                break
                pass
            pass
        pass
 
    if flag:
        resultFlag = value
        pass
    pass
 
# 加載素材
def main():
    initRole = 2  # 代表白子下 2:代表當(dāng)前是黑子下
    global resultFlag, initChessList
    initChessSquare(27, 27)  # 初始化棋牌
    pygame.init()            # 初始化游戲環(huán)境
    # 創(chuàng)建游戲窗口
    screen = pygame.display.set_mode((620,620), 0, 0) # 第一個(gè)參數(shù)是元組:窗口的長和寬
    # 添加游戲標(biāo)題
    pygame.display.set_caption("五子棋小游戲")
    # 圖片的加載
    background = pygame.image.load('images/bg.png')
    blackStorn = pygame.image.load('images/storn_black.png')
    whiteStorn = pygame.image.load('images/storn_white.png')
    winStornW = pygame.image.load('images/white.png')
    winStornB = pygame.image.load('images/black.png')
    rect = blackStorn.get_rect()
 
    while True:
        screen.blit(background, (0, 0))
        # 更新棋盤棋子
        for temp in initChessList:
            for point in temp:
                if point.value == 1:
                    screen.blit(whiteStorn, (point.x - 18, point.y - 18))
                    pass
                elif point.value == 2:
                    screen.blit(blackStorn, (point.x - 18, point.y - 18))
                    pass
                pass
            pass
        # 如果已經(jīng)判決出輸贏
        if resultFlag > 0:
            initChessList = []      # 清空棋盤
            initChessSquare(27, 27) # 重新初始化棋盤
            if resultFlag == 1:
                screen.blit(winStornW, (50,100))
            else:
                screen.blit(winStornB, (50,100))
            pass
        pygame.display.update()
 
        if resultFlag >0:
            time.sleep(3)
            resultFlag = 0
            pass
        eventHandler()
        pass
 
    pass
 
if __name__ == "__main__":
    main()
    pass

插圖:放在同一目錄下的images文件夾里

bg.png

python怎么實(shí)現(xiàn)五子棋雙人對弈

storn_white.png

python怎么實(shí)現(xiàn)五子棋雙人對弈

storn_black.png

python怎么實(shí)現(xiàn)五子棋雙人對弈

white.png

python怎么實(shí)現(xiàn)五子棋雙人對弈

black.png

python怎么實(shí)現(xiàn)五子棋雙人對弈

讀到這里,這篇“python怎么實(shí)現(xiàn)五子棋雙人對弈”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI