欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python編寫五子棋游戲

 更新時間:2021年05月25日 09:32:42   作者:MiManchi729  
這篇文章主要為大家詳細介紹了python編寫五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python編寫五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下

游戲代碼及部分注釋

import pygame        #導(dǎo)入pygame游戲模塊
import time           #調(diào)用time庫
import sys
from pygame.locals import *

initChessList = []          #保存的是棋盤坐標(biāo)
initRole = 1                #1:代表白棋; 2:代表黑棋
resultFlag = 0              #結(jié)果標(biāo)志

class StornPoint():
    def __init__(self,x,y,value):
        '''
        :param x: 代表x軸坐標(biāo)
        :param y: 代表y軸坐標(biāo)
        :param value: 當(dāng)前坐標(biāo)點的棋子:0:沒有棋子 1:白子 2:黑子
        '''
        self.x = x            #初始化成員變量
        self.y = y
        self.value = value

def initChessSquare(x,y):     #初始化棋盤
    for i in range(15):       # 每一行的交叉點坐標(biāo)
        rowlist = []
        for j in range(15):   # 每一列的交叉點坐標(biāo)
            pointX = x+ j*40
            pointY = y+ i*40
            sp = StornPoint(pointX,pointY,0)
            rowlist.append(sp)
        initChessList.append(rowlist)

def eventHander():            #監(jiān)聽各種事件
    for event in pygame.event.get():
        global initRole
        if event.type == QUIT:#事件類型為退出時
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONDOWN: #當(dāng)點擊鼠標(biāo)時
            x,y = pygame.mouse.get_pos()  #獲取點擊鼠標(biāo)的位置坐標(biāo)
            i=0
            j=0
            for temp in initChessList:
                for point in temp:
                    if x>=point.x-10 and x<=point.x+10 and y>=point.y-10 and y<=point.y+10:
                        if point.value == 0 and initRole == 1:   #當(dāng)棋盤位置為空;棋子類型為白棋
                            point.value = 1             #鼠標(biāo)點擊時,棋子為白棋
                            judgeResult(i,j,1)
                            initRole = 2                #切換角色
                        elif point.value == 0 and initRole ==2:  #當(dāng)棋盤位置為空;棋子類型為黑棋
                            point.value = 2             #鼠標(biāo)點擊時,棋子為黑棋
                            judgeResult(i,j,2)
                            initRole = 1                #切換角色
                        break
                    j+=1
                i+=1
                j=0

def judgeResult(i,j,value):   #橫向判斷
    global resultFlag
    flag = False
    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

    # 先判斷東北方向的對角下輸贏 x 列軸, y是行軸 , i 是行 j 是列(右斜向)(在邊緣依次逐一遍歷,是否五個棋子的類型一樣)
    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

    # 2、判斷西北方向的對角下輸贏 x 列軸, y是行軸 , i 是行 j 是列(左斜向)(在邊緣依次逐一遍歷,是否五個棋子的類型一樣)
    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


    if flag:               #如果條件成立,證明五子連珠
        resultFlag = value #獲取成立的棋子顏色
        print("白棋贏" if value ==1 else "黑棋贏")

# 加載素材
def main():
    global initChessList,resultFlag
    initChessSquare(27,27)
    pygame.init()     # 初始化游戲環(huán)境
    screen = pygame.display.set_mode((620,620),0,0)          # 創(chuàng)建游戲窗口 # 第一個參數(shù)是元組:窗口的長和寬
    pygame.display.set_caption("陳曉超五子棋")                # 添加游戲標(biāo)題
    background = pygame.image.load("D:/cxc/4.png")          #加載背景圖片
    whiteStorn = pygame.image.load("D:/cxc/2.png") #加載白棋圖片
    blackStorn = pygame.image.load("D:/cxc/1.png") #加載黑棋圖片
    resultStorn = pygame.image.load("D:/cxc/3.png")#加載 贏 時的圖片
    rect = blackStorn.get_rect()

    while True:
        screen.blit(background,(0,0))
        for temp in initChessList:
            for point in temp:
                if point.value == 1:          #當(dāng)棋子類型為1時,繪制白棋
                    screen.blit(whiteStorn,(point.x-18,point.y-18))
                elif point.value == 2:        #當(dāng)棋子類型為2時,繪制黑棋
                    screen.blit(blackStorn,(point.x-18,point.y-18))

        if resultFlag >0:
            initChessList = []                 # 清空棋盤
            initChessSquare(27,27)             # 重新初始化棋盤
            screen.blit(resultStorn,(200,200)) #繪制獲勝時的圖片
        pygame.display.update()                #更新視圖

        if resultFlag >0:
            time.sleep(3)
            resultFlag = 0                     #置空之前的獲勝結(jié)果
        eventHander()                          #調(diào)用之前定義的事件函數(shù)
if __name__ == '__main__':
    main()        #調(diào)用主函數(shù)繪制窗口
    pass

運行后就會出現(xiàn)游戲的窗口,像這樣:

我們可以任意的在棋盤上落子,像這樣:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 6個Python辦公黑科技,助你提升工作效率

    6個Python辦公黑科技,助你提升工作效率

    這篇文章主要介紹了Python辦公黑科技,文中有非常詳細的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-08-08
  • 淺談Python numpy創(chuàng)建空數(shù)組的問題

    淺談Python numpy創(chuàng)建空數(shù)組的問題

    今天遇到一個小小的問題,是關(guān)于numpy創(chuàng)建空數(shù)組,今天特地整理了這篇文章,文中作出了非常詳細的介紹,對正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • python操作yaml說明

    python操作yaml說明

    這篇文章主要介紹了python操作yaml說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python操作xml文件示例

    python操作xml文件示例

    這篇文章主要介紹了python操作xml文件示例,需要的朋友可以參考下
    2014-04-04
  • 跟老齊學(xué)Python之list和str比較

    跟老齊學(xué)Python之list和str比較

    list和str兩種類型數(shù)據(jù),有不少相似的地方,也有很大的區(qū)別。本講對她們做個簡要比較,同時也是對前面有關(guān)兩者的知識復(fù)習(xí)一下,所謂“溫故而知新”。
    2014-09-09
  • Python OpenCV 直方圖的計算與顯示的方法示例

    Python OpenCV 直方圖的計算與顯示的方法示例

    這篇文章主要介紹了Python OpenCV 直方圖的計算與顯示的方法示例,主要介紹用NumPy和Matplotlib計算和繪制直方圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Python繪圖系統(tǒng)之繪制散點圖,極坐標(biāo)和子圖

    Python繪圖系統(tǒng)之繪制散點圖,極坐標(biāo)和子圖

    這篇文章主要為大家詳細介紹了如何基于Python實現(xiàn)一個繪圖系統(tǒng),可以支持繪制散點圖,極坐標(biāo)和子圖,文中的示例代碼講解詳細,感興趣的可以了解下
    2023-09-09
  • Python中find函數(shù)的詳細使用方法

    Python中find函數(shù)的詳細使用方法

    在Python中find()函數(shù)用于檢測字符串中是否包含子字符串sub,如果指定start(開始)和end(結(jié)束)范圍,則檢查是否包含在指定范圍內(nèi),這篇文章主要給大家介紹了關(guān)于Python中find函數(shù)的詳細使用方法,需要的朋友可以參考下
    2023-05-05
  • 進行數(shù)據(jù)處理的6個?Python?代碼塊分享

    進行數(shù)據(jù)處理的6個?Python?代碼塊分享

    這篇文章主要介紹了進行數(shù)據(jù)處理6個Python代碼塊的分享,分享內(nèi)容有選取有空值的行、快速替換列值、對列進行分區(qū)、將一列分為多列等內(nèi)容,需要的朋友可以參考一下
    2022-04-04
  • Python 二叉樹的概念案例詳解

    Python 二叉樹的概念案例詳解

    這篇文章主要介紹了二叉樹的概念案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09

最新評論