最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python五子棋小游戲實例分享

 更新時間:2021年09月06日 15:30:23   作者:Maggie晨曦  
這篇文章主要為大家詳細介紹了Python五子棋小游戲實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

使用的庫

pygame、pyautogui

流程簡述

1.畫棋盤

設置網(wǎng)格間隔40px ,留白 80 px ,與網(wǎng)格橫豎線數(shù)量 ,初定19 × 19 。

2.鼠標點擊

鼠標點擊取得坐坐標(x0 , y0),再獲得最近的網(wǎng)格上的點(x1 , y1),再將每次動作獲得的(x1 , y1 )放入列表 chess_location 中。

再通過:

chess_location_b = chess_location[0::2]
chess_location_w = chess_location[1::2]

分別獲得黑棋和白棋所走過的坐標。

3.判斷勝負

這一塊網(wǎng)上有很多不同的方法,我為了讓大家讀懂盡量寫的詳細了。
首先 ,我們要知道連五有四個方向:豎直 ,水平 ,右上左下 , 右下左上 。
每次將新落下的子分別進行4個方向的判斷,判斷是否出現(xiàn)連五及以上。
我使用的方法是:

def result(x): # x 為 chess_location_b 或者 chess_location_w
    # 豎直
    score = []
    for i in range(cell_num): #cell_num = 19
        if [x[-1][0], i ] in x:
            score.append([x[-1][0], i ])
            if score.__len__() >= 5:
                return 1
        else:
            score =[]

大概意思就是最新落下的(x1 , y1)中的豎直方向從上往下檢查如果出現(xiàn)黑(白)棋 ,則將出現(xiàn)棋子的坐標加入列表 score 中 , 如果出現(xiàn)異色棋子或者沒有棋子,則清空 score 中的元素 ,如果列表 score 中的元素數(shù)量大于等于5個 ,則分勝負 。
如果棋子填滿棋盤但是仍沒有分出勝負 ,則平局 。

代碼及結果

代碼

import pygame,pyautogui
from pygame.locals import *
# 初始參數(shù)
cell_size = 40
space = 80
cell_num = 19
grid_size = (cell_num - 1)*cell_size + space*2
screen = pygame.display.set_mode([grid_size,grid_size],0,32)
chess_location , chess_location_w , chess_location_b = [] , [] , []
# 畫棋盤
def grid():
    screen.fill([208,173,108])
    font = pygame.font.SysFont("arial", 20)
    i = 0
    for x in range(0, cell_size * cell_num , cell_size):
        i += 1
        text_surface = font.render("{}".format(i), True, (0, 0, 0))
        screen.blit(text_surface,[(space - font.get_height()) - 10,(space - font.get_height()/2) + cell_size*(i -1 )])
        pygame.draw.line(screen, (0, 0, 0), (x + space, 0 + space), (x + space, cell_size * (cell_num - 1) + space), 2)
    i = 0
    for y in range(0, cell_size * cell_num, cell_size):
        i += 1
        text_surface = font.render("{}".format(chr(64 + i)), True, (0, 0, 0))
        screen.blit(text_surface,[(space + cell_size * (i - 1)) -5, (space - font.get_height() / 2) - 20])
        pygame.draw.line(screen, (0,0,0), (0 + space, y + space),(cell_size * (cell_num - 1) + space, y + space), 2)
# 分勝負
def result(x):
    # 豎直
    score = []
    for i in range(cell_num):
        if [x[-1][0], i ] in x:
            score.append([x[-1][0], i ])
            if score.__len__() >= 5:
                return 1
        else:
            score =[]
    # 水平
    score = []
    for i in range(cell_num):
        if [i , x[-1][1]] in x:
            score.append([i , x[-1][1]])
            if score.__len__() >= 5:
                return 1
        else:
            score = []
    # 右上左下
    score = []
    for i in range(cell_num):
        if [i,x[-1][0] + x[-1][1] - i] in x:
            score.append([i,x[-1][0] + x[-1][1] - i])
            if score.__len__() >= 5:
                return 1
        else:
            score = []
    # 右下左上
    score = []
    for i in range(cell_num):
        if [x[-1][0] - x[-1][1] + i,i] in x:
            score.append([x[-1][0] - x[-1][1] + i,i])
            if score.__len__() >= 5:
                return 1
        else:
            score = []
    # 平局
    if chess_location.__len__() == cell_num * cell_num :
        return 2
# 主循環(huán)
def running():
    global chess_location_w , chess_location_b
    while True:
        grid()
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            # 落子
            if event.type == MOUSEBUTTONDOWN:
                x0 , y0 = pygame.mouse.get_pos()
                if x0 > space and y0 > space and x0 < space + cell_size*(cell_num - 1) and y0 < space + cell_size * (cell_num - 1):
                    x1 = round((x0 - space) / cell_size)
                    y1 = round((y0 - space) / cell_size)
                    if [x1 , y1] not in chess_location:
                        chess_location.append([x1 , y1])
            # 悔棋
            elif event.type == KEYDOWN:
                if event.key == K_LEFT:
                    chess_location.pop(-1)
        chess_location_b = chess_location[0::2]
        chess_location_w = chess_location[1::2]
        # 黑棋
        for i in chess_location_b:
            pygame.draw.circle(screen, [ 0 , 0 , 0 ], [i[0]* cell_size + space, i[1]* cell_size + space], 15, 0)
        # 白棋
        for i in chess_location_w:
            pygame.draw.circle(screen, [255,255,255], [i[0]* cell_size + space, i[1]* cell_size + space], 15, 0)
        # 判斷勝負
        if chess_location_b and result(chess_location_b) == 1:
            pyautogui.alert(text='黑棋勝',title='游戲結束')
            exit()
        elif chess_location_w and result(chess_location_w) == 1:
            pyautogui.alert(text='白棋勝',title='游戲結束')
            exit()
        elif chess_location_b and chess_location_w:
            if result(chess_location_b) or result(chess_location_w) == 2:
                pyautogui.alert(text='平局', title='游戲結束')
                exit()
        pygame.display.update()


if __name__ == '__main__':
    pygame.init()
    running()

輸出

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

相關文章

  • 淺談Python中函數(shù)的參數(shù)傳遞

    淺談Python中函數(shù)的參數(shù)傳遞

    下面小編就為大家?guī)硪黄獪\談Python中函數(shù)的參數(shù)傳遞。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • 詳解Python如何生成詞云的方法

    詳解Python如何生成詞云的方法

    這篇文章主要介紹了詳解Python如何生成詞云的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • 一文詳解NumPy數(shù)組迭代與合并

    一文詳解NumPy數(shù)組迭代與合并

    NumPy?數(shù)組迭代是訪問和處理數(shù)組元素的重要方法,它允許您逐個或成組地遍歷數(shù)組元素,NumPy?提供了多種函數(shù)來合并數(shù)組,用于將多個數(shù)組的內(nèi)容連接成一個新數(shù)組,本文給大家詳細介紹了NumPy數(shù)組迭代與合并,需要的朋友可以參考下
    2024-05-05
  • Python?Matplotlib基本用法詳解

    Python?Matplotlib基本用法詳解

    Matplotlib?是Python中類似?MATLAB?的繪圖工具,熟悉?MATLAB?也可以很快的上手?Matplotlib,這篇文章主要介紹了Python?Matplotlib基本用法,需要的朋友可以參考下
    2023-03-03
  • Python內(nèi)置函數(shù)locals和globals對比

    Python內(nèi)置函數(shù)locals和globals對比

    這篇文章主要介紹了Python內(nèi)置函數(shù)locals和globals對比,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Python深度學習pytorch神經(jīng)網(wǎng)絡圖像卷積運算詳解

    Python深度學習pytorch神經(jīng)網(wǎng)絡圖像卷積運算詳解

    這篇文章主要介紹了Python深度學習關于pytorch神經(jīng)網(wǎng)絡圖像卷積的運算示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • python實現(xiàn)windows壁紙定期更換功能

    python實現(xiàn)windows壁紙定期更換功能

    這篇文章主要為大家詳細介紹了python實現(xiàn)windows壁紙定期更換功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Python IndexError報錯分析及解決方法

    Python IndexError報錯分析及解決方法

    在Python編程中,IndexError是一種常見的異常類型,它通常發(fā)生在嘗試訪問序列(如列表、元組或字符串)中不存在的索引時,本文將深入分析IndexError的成因、表現(xiàn)形式,并提供相應的解決辦法,同時附帶詳細的代碼示例,需要的朋友可以參考下
    2024-07-07
  • 淺談python 四種數(shù)值類型(int,long,float,complex)

    淺談python 四種數(shù)值類型(int,long,float,complex)

    下面小編就為大家?guī)硪黄獪\談python 四種數(shù)值類型(int,long,float,complex)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • Python 如何實時向文件寫入數(shù)據(jù)(附代碼)

    Python 如何實時向文件寫入數(shù)據(jù)(附代碼)

    這篇文章主要介紹了Python 如何實時向文件寫入數(shù)據(jù)(附代碼),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07

最新評論

项城市| 房产| 岳普湖县| 周至县| 旌德县| 耒阳市| 长武县| 合阳县| 铜川市| 广元市| 蓝山县| 潍坊市| 新巴尔虎左旗| 哈尔滨市| 仙游县| 健康| 和政县| 红原县| 中山市| 密山市| 开原市| 惠水县| 四平市| 红原县| 舟山市| 神池县| 济南市| 东城区| 宁蒗| 南陵县| 墨竹工卡县| 武威市| 奉贤区| 灌阳县| 陇川县| 青岛市| 澄迈县| 朝阳区| 建昌县| 响水县| 武鸣县|