Python五子棋小游戲?qū)嵗窒?/h1>
更新時(shí)間:2021年09月06日 15:30:23 作者:Maggie晨曦
這篇文章主要為大家詳細(xì)介紹了Python五子棋小游戲?qū)嵗闹惺纠a介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Python實(shí)現(xiàn)五子棋小游戲的具體代碼,供大家參考,具體內(nèi)容如下
使用的庫(kù)
pygame、pyautogui
流程簡(jiǎn)述
1.畫棋盤
設(shè)置網(wǎng)格間隔40px ,留白 80 px ,與網(wǎng)格橫豎線數(shù)量 ,初定19 × 19 。
2.鼠標(biāo)點(diǎn)擊
鼠標(biāo)點(diǎn)擊取得坐坐標(biāo)(x0 , y0),再獲得最近的網(wǎng)格上的點(diǎn)(x1 , y1),再將每次動(dòng)作獲得的(x1 , y1 )放入列表 chess_location 中。
再通過:
chess_location_b = chess_location[0::2]
chess_location_w = chess_location[1::2]
分別獲得黑棋和白棋所走過的坐標(biāo)。
3.判斷勝負(fù)
這一塊網(wǎng)上有很多不同的方法,我為了讓大家讀懂盡量寫的詳細(xì)了。
首先 ,我們要知道連五有四個(gè)方向:豎直 ,水平 ,右上左下 , 右下左上 。
每次將新落下的子分別進(jìn)行4個(gè)方向的判斷,判斷是否出現(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)棋子的坐標(biāo)加入列表 score 中 , 如果出現(xiàn)異色棋子或者沒有棋子,則清空 score 中的元素 ,如果列表 score 中的元素?cái)?shù)量大于等于5個(gè) ,則分勝負(fù) 。
如果棋子填滿棋盤但是仍沒有分出勝負(fù) ,則平局 。
代碼及結(jié)果
代碼
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)
# 分勝負(fù)
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)
# 判斷勝負(fù)
if chess_location_b and result(chess_location_b) == 1:
pyautogui.alert(text='黑棋勝',title='游戲結(jié)束')
exit()
elif chess_location_w and result(chess_location_w) == 1:
pyautogui.alert(text='白棋勝',title='游戲結(jié)束')
exit()
elif chess_location_b and chess_location_w:
if result(chess_location_b) or result(chess_location_w) == 2:
pyautogui.alert(text='平局', title='游戲結(jié)束')
exit()
pygame.display.update()
if __name__ == '__main__':
pygame.init()
running()
輸出

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
-
Python疫情確診折線圖實(shí)現(xiàn)數(shù)據(jù)可視化實(shí)例詳解
數(shù)據(jù)可視化是指用圖形或表格的方式來呈現(xiàn)數(shù)據(jù)。圖表能夠清楚地呈現(xiàn)數(shù)據(jù)性質(zhì),?以及數(shù)據(jù)間或?qū)傩蚤g的關(guān)系,可以輕易地讓人看圖釋義。用戶通過探索圖(Exploratory?Graph)可以了解數(shù)據(jù)的特性、尋找數(shù)據(jù)的趨勢(shì)、降低數(shù)據(jù)的理解門檻 2022-09-09
-
時(shí)間序列重采樣和pandas的resample方法示例解析
這篇文章主要為大家介紹了時(shí)間序列重采樣和pandas的resample方法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪 2023-09-09
-
python系統(tǒng)指定文件的查找只輸出目錄下所有文件及文件夾
這篇文章主要介紹了python系統(tǒng)指定文件的查找只輸出目錄下所有文件及文件夾,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下 2020-01-01
-
Django使用Mysql數(shù)據(jù)庫(kù)已經(jīng)存在的數(shù)據(jù)表方法
今天小編就為大家分享一篇Django使用Mysql數(shù)據(jù)庫(kù)已經(jīng)存在的數(shù)據(jù)表方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧 2018-05-05
-
python實(shí)現(xiàn)智能語(yǔ)音天氣預(yù)報(bào)
今天小編就為大家分享一篇python實(shí)現(xiàn)智能語(yǔ)音天氣預(yù)報(bào),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧 2019-12-12
-
python如何實(shí)現(xiàn)常用的五種排序算法詳解
排序有很多種實(shí)現(xiàn)方法,比如冒泡排序、選擇排序、歸并排序、希爾排序、快速排序、插入排序、堆排序、基數(shù)排序等,這篇文章主要給大家介紹了關(guān)于python如何實(shí)現(xiàn)常用的五種排序算法,需要的朋友可以參考下 2021-08-08
最新評(píng)論
本文實(shí)例為大家分享了Python實(shí)現(xiàn)五子棋小游戲的具體代碼,供大家參考,具體內(nèi)容如下
使用的庫(kù)
pygame、pyautogui
流程簡(jiǎn)述
1.畫棋盤
設(shè)置網(wǎng)格間隔40px ,留白 80 px ,與網(wǎng)格橫豎線數(shù)量 ,初定19 × 19 。
2.鼠標(biāo)點(diǎn)擊
鼠標(biāo)點(diǎn)擊取得坐坐標(biāo)(x0 , y0),再獲得最近的網(wǎng)格上的點(diǎn)(x1 , y1),再將每次動(dòng)作獲得的(x1 , y1 )放入列表 chess_location 中。
再通過:
chess_location_b = chess_location[0::2] chess_location_w = chess_location[1::2]
分別獲得黑棋和白棋所走過的坐標(biāo)。
3.判斷勝負(fù)
這一塊網(wǎng)上有很多不同的方法,我為了讓大家讀懂盡量寫的詳細(xì)了。
首先 ,我們要知道連五有四個(gè)方向:豎直 ,水平 ,右上左下 , 右下左上 。
每次將新落下的子分別進(jìn)行4個(gè)方向的判斷,判斷是否出現(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)棋子的坐標(biāo)加入列表 score 中 , 如果出現(xiàn)異色棋子或者沒有棋子,則清空 score 中的元素 ,如果列表 score 中的元素?cái)?shù)量大于等于5個(gè) ,則分勝負(fù) 。
如果棋子填滿棋盤但是仍沒有分出勝負(fù) ,則平局 。
代碼及結(jié)果
代碼
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) # 分勝負(fù) 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) # 判斷勝負(fù) if chess_location_b and result(chess_location_b) == 1: pyautogui.alert(text='黑棋勝',title='游戲結(jié)束') exit() elif chess_location_w and result(chess_location_w) == 1: pyautogui.alert(text='白棋勝',title='游戲結(jié)束') exit() elif chess_location_b and chess_location_w: if result(chess_location_b) or result(chess_location_w) == 2: pyautogui.alert(text='平局', title='游戲結(jié)束') exit() pygame.display.update() if __name__ == '__main__': pygame.init() running()
輸出
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python疫情確診折線圖實(shí)現(xiàn)數(shù)據(jù)可視化實(shí)例詳解
數(shù)據(jù)可視化是指用圖形或表格的方式來呈現(xiàn)數(shù)據(jù)。圖表能夠清楚地呈現(xiàn)數(shù)據(jù)性質(zhì),?以及數(shù)據(jù)間或?qū)傩蚤g的關(guān)系,可以輕易地讓人看圖釋義。用戶通過探索圖(Exploratory?Graph)可以了解數(shù)據(jù)的特性、尋找數(shù)據(jù)的趨勢(shì)、降低數(shù)據(jù)的理解門檻2022-09-09時(shí)間序列重采樣和pandas的resample方法示例解析
這篇文章主要為大家介紹了時(shí)間序列重采樣和pandas的resample方法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09python系統(tǒng)指定文件的查找只輸出目錄下所有文件及文件夾
這篇文章主要介紹了python系統(tǒng)指定文件的查找只輸出目錄下所有文件及文件夾,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01Django使用Mysql數(shù)據(jù)庫(kù)已經(jīng)存在的數(shù)據(jù)表方法
今天小編就為大家分享一篇Django使用Mysql數(shù)據(jù)庫(kù)已經(jīng)存在的數(shù)據(jù)表方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05python實(shí)現(xiàn)智能語(yǔ)音天氣預(yù)報(bào)
今天小編就為大家分享一篇python實(shí)現(xiàn)智能語(yǔ)音天氣預(yù)報(bào),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12python如何實(shí)現(xiàn)常用的五種排序算法詳解
排序有很多種實(shí)現(xiàn)方法,比如冒泡排序、選擇排序、歸并排序、希爾排序、快速排序、插入排序、堆排序、基數(shù)排序等,這篇文章主要給大家介紹了關(guān)于python如何實(shí)現(xiàn)常用的五種排序算法,需要的朋友可以參考下2021-08-08