婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Python五子棋小游戲實例分享

Python五子棋小游戲實例分享

熱門標簽:獲客智能電銷機器人 不錯的400電話辦理 湛江電銷防封卡 鄭州智能外呼系統運營商 哈爾濱外呼系統代理商 南昌辦理400電話怎么安裝 徐州天音防封電銷卡 電話機器人適用業務 佛山防封外呼系統收費

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

使用的庫

pygame、pyautogui

流程簡述

1.畫棋盤

設置網格間隔40px ,留白 80 px ,與網格橫豎線數量 ,初定19 × 19 。

2.鼠標點擊

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

再通過:

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

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

3.判斷勝負

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

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)中的豎直方向從上往下檢查如果出現黑(白)棋 ,則將出現棋子的坐標加入列表 score 中 , 如果出現異色棋子或者沒有棋子,則清空 score 中的元素 ,如果列表 score 中的元素數量大于等于5個 ,則分勝負 。
如果棋子填滿棋盤但是仍沒有分出勝負 ,則平局 。

代碼及結果

代碼

import pygame,pyautogui
from pygame.locals import *
# 初始參數
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
# 主循環
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()

輸出

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

您可能感興趣的文章:
  • python游戲實戰項目之智能五子棋
  • python編寫五子棋游戲
  • python實現網絡五子棋
  • python實現雙人五子棋(終端版)
  • Python tkinter制作單機五子棋游戲
  • python實現簡單的五子棋游戲
  • python游戲實戰項目之智能五子棋簡易版

標簽:呂梁 廣西 蘭州 安康 懷化 紹興 吉安 蕪湖

巨人網絡通訊聲明:本文標題《Python五子棋小游戲實例分享》,本文關鍵詞  Python,五子棋,小游戲,實例,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python五子棋小游戲實例分享》相關的同類信息!
  • 本頁收集關于Python五子棋小游戲實例分享的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 长岛县| 房产| 定远县| 会宁县| 毕节市| 塔城市| 新绛县| 嘉定区| 丽江市| 阳新县| 阿克陶县| 田阳县| 瑞安市| 钟山县| 大姚县| 新乐市| 中宁县| 康马县| 新乡市| 万山特区| 南江县| 长白| 阳春市| 石首市| 石泉县| 灵宝市| 海门市| 宕昌县| 虎林市| 徐水县| 东方市| 海安县| 梁山县| 会同县| 融水| 微山县| 襄樊市| 正镶白旗| 崇州市| 图们市| 文成县|