編程說(shuō)明:?pygame編程五子棋程序。
設(shè)計(jì)流程:新建棋盤,落子畫圓,校驗(yàn)五子相連,贏家信息顯示,自動(dòng)重啟下一局,程序打包成exe。
關(guān)鍵點(diǎn):
????新建棋盤:考慮線的間隔,需提前計(jì)算好。
? ??落子畫圓:考慮黑白交替,已落子坐標(biāo)位不可重復(fù)落子。
? ? 校驗(yàn)五子相連:分成橫、豎、左斜、右斜四個(gè)方向的相連,其中如果第一個(gè)為白子,第二個(gè)不為同色的白子,則列表清零重新計(jì)算,當(dāng)列表出現(xiàn)五子相連即列表長(zhǎng)度出現(xiàn)5個(gè)值,則跳出循環(huán),提示贏家信息。
? ? 贏家信息顯示:先提前獲取系統(tǒng)字體簡(jiǎn)稱,設(shè)定正確的字體,否則中文顯示為方框設(shè)定函數(shù)為pygame.font.SysFont("simhei",25)。
????程序打包成exe:準(zhǔn)備.ico圖標(biāo),使用指令在cmd框下執(zhí)行Pyinstaller -F -w -i wzq.ico wzq.py 指定icon圖標(biāo)進(jìn)行打包,在cmd黑框查看生成文件路徑,找到生成的exe文件即可,坑為Pyinstaller確認(rèn)為最新版本,版本過(guò)舊會(huì)報(bào)錯(cuò)導(dǎo)致打包失敗,pip升級(jí)或者卸載重裝即可。
參考鏈接:
1.pygame基礎(chǔ)教程
https://blog.csdn.net/qq_40801987/article/details/121506910
2.Python Pygame制作簡(jiǎn)單五子棋游戲(詳細(xì)代碼+解釋)
https://blog.csdn.net/weixin_43918046/article/details/119521845?spm=1001.2014.3001.5506
3.python中的tkinter庫(kù)彈窗messagebox詳解
https://www.jb51.net/article/216076.htm
4.pygame在屏幕上顯示中文的方法
https://www.jianshu.com/p/eeea79aaf4b8
5.pygame基礎(chǔ)語(yǔ)法
https://www.cnblogs.com/liuzhongkun/p/16165288.html
效果展示:
程序詳情:
# encoding:utf-8
#調(diào)用pygame庫(kù)
import pygame
import sys
#調(diào)用常用關(guān)鍵字常量,例當(dāng)產(chǎn)生 KEYDOWN 或 KEYUP 事件時(shí),key 屬性描述具體哪個(gè)按鍵被按下
from pygame.locals import QUIT,KEYDOWN
import numpy as np
# from tkinter import *
# from tkinter import messagebox? #一方獲勝后彈出面板,第二種顯示樣式
#判斷鼠標(biāo)的落點(diǎn)
def find_pos(x,y):
? ? for i in range(27,670,44):
? ? ? ? for j in range(27,670,44):
? ? ? ? ? ? L1=i-22
? ? ? ? ? ? L2=i+22
? ? ? ? ? ? R1=j-22
? ? ? ? ? ? R2=j+22
? ? ? ? ? ? if x>=L1 and x<=L2 and y>=R1 and y<=R2:
? ? ? ? ? ? ? ? return i,j
? ? return x,y
#已落子不可以重復(fù)落子
def check_over_pos(x,y,over_pos):
? ? for val in over_pos:
? ? ? ? if val[0][0]==x and val[0][1]==y:
? ? ? ? ? ? return False? #表示已落子
? ? return True #表示沒(méi)有落子
#判斷五子連心
def check_win(over_pos):
? ? mp=np.zeros([15,15],dtype=int)
? ? for val in over_pos:
? ? ? ? x=int((val[0][0]-27)/44)
? ? ? ? y=int((val[0][1]-27)/44)
? ? ? ? if val[1]==white_color: #通過(guò)顏色判斷白子
? ? ? ? ? ? mp[x][y]=2#表示白子
? ? ? ? else:
? ? ? ? ? ? mp[x][y]=1#表示黑子
? ? for i in range(15): #查看有沒(méi)豎型五子相連。例橫坐標(biāo)為0,查看縱坐標(biāo)是否有五子相連的白子或者黑子
? ? ? ? pos1=[]
? ? ? ? pos2=[]
? ? ? ? for j in range(15):
? ? ? ? ? ? if mp[i][j]==1:? #把黑子保存到列表post1
? ? ? ? ? ? ? ? pos1.append([i,j])
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? pos1=[] #關(guān)鍵點(diǎn),如果下次循環(huán)不為白色,則列表情空,重新計(jì)算,保證只有五子相連才保存列表,下面橫豎左斜右斜計(jì)算同理。
? ? ? ? ? ? if mp[i][j]==2: #把白子保存到列表post2
? ? ? ? ? ? ? ? pos2.append([i,j]) #關(guān)鍵點(diǎn),如果下次循環(huán)不為黑色,則列表情空,重新計(jì)算,保證只有五子相連才保存列表下面橫豎左斜右斜計(jì)算同理。
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? pos2=[]
? ? ? ? ? ? if len(pos1)>=5: #五子連心
? ? ? ? ? ? ? ? return(1,pos1)
? ? ? ? ? ? if len(pos2)>=5:
? ? ? ? ? ? ? ? return [2,pos2]
? ? for j in range(15): #查看有沒(méi)橫型五子相連。例豎坐標(biāo)為0,查看橫坐標(biāo)是否有五子相連的白子或者黑子
? ? ? ? pos1=[]
? ? ? ? pos2=[]
? ? ? ? for i in range(15):
? ? ? ? ? ? if mp[i][j]==1:
? ? ? ? ? ? ? ? pos1.append([i,j])
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? pos1=[]
? ? ? ? ? ? if mp[i][j]==2:
? ? ? ? ? ? ? ? pos2.append([i,j])
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? pos2=[]
? ? ? ? ? ? if len(pos1)>=5:
? ? ? ? ? ? ? ? return(1,pos1)
? ? ? ? ? ? if len(pos2)>=5:
? ? ? ? ? ? ? ? return [2,pos2]
? ? for i in range(15): #查看有沒(méi)左斜型五子相連。
? ? ? ? for j in range(15):
? ? ? ? ? ? pos1=[]
? ? ? ? ? ? pos2=[]
? ? ? ? ? ? for k in range(15):
? ? ? ? ? ? ? ? if i+k>=15 or j+k>=15:
? ? ? ? ? ? ? ? ? ? break
? ? ? ? ? ? ? ? if mp[i+k][j+k]==1:
? ? ? ? ? ? ? ? ? ? pos1.append([i+k,j+k])
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? pos1=[]
? ? ? ? ? ? ? ? if mp[i+k][j+k]==2:
? ? ? ? ? ? ? ? ? ? pos2.append([i+k,j+k])
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? pos2=[]
? ? ? ? ? ? ? ? if len(pos1)>=5:
? ? ? ? ? ? ? ? ? ? return(1,pos1)
? ? ? ? ? ? ? ? if len(pos2)>=5:
? ? ? ? ? ? ? ? ? ? return [2,pos2]
? ? for i in range(15): #查看有沒(méi)右斜型五子相連。
? ? ? ? for j in range(15):
? ? ? ? ? ? pos1=[]
? ? ? ? ? ? pos2=[]
? ? ? ? ? ? for k in range(15):
? ? ? ? ? ? ? ? if i+k>=15 or j-k<0:
? ? ? ? ? ? ? ? ? ? break
? ? ? ? ? ? ? ? if mp[i+k][j-k]==1:
? ? ? ? ? ? ? ? ? ? pos1.append([i+k,j-k])
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? pos1=[]
? ? ? ? ? ? ? ? if mp[i+k][j-k]==2:
? ? ? ? ? ? ? ? ? ? pos2.append([i+k,j-k])
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? pos2=[]
? ? ? ? ? ? ? ? if len(pos1)>=5:
? ? ? ? ? ? ? ? ? ? return(1,pos1)
? ? ? ? ? ? ? ? if len(pos2)>=5:
? ? ? ? ? ? ? ? ? ? return [2,pos2]
? ? return [0,[]]
#獲勝信息 一方獲勝后在(250,0)坐標(biāo)顯示獲勝信息
def message_rt(message_text):
? ? # fonts=pygame.font.get_fonts()
? ? # print(fonts) #查看系統(tǒng)字體
? ? text=pygame.font.SysFont("simhei",25) #坑,中文顯示都是方框
? ? print(message_text)
? ? print(message_text.encode("utf-8"))
? ? text_fmt=text.render(message_text,1,(255,0,0))
? ? screen.blit(text_fmt,(250,0))
while True:? #一局結(jié)束重新開(kāi)始下一局
? ? #初始化pygame
? ? pygame.init()
? ? #窗口創(chuàng)建
? ? #獲取對(duì)顯示系統(tǒng)的訪問(wèn),并創(chuàng)建一個(gè)窗口screen
? ? #窗口大小為670x670
? ? screen=pygame.display.set_mode((670,670))
? ? screen_color=[255,165,79] #背景顏色,[255,165,79]對(duì)應(yīng)為棕色
? ? line_color=[0,0,0] #線條顏色,[0,0,0]對(duì)應(yīng)黑色
? ? flag=False
? ? tm=0
? ? over_pos=[]
? ? white_color=[255,255,255]
? ? black_color=[0,0,0]
? ? while True: #不斷訓(xùn)練刷新畫布
? ? ? ? for event in pygame.event.get(): #獲取事件,如果鼠標(biāo)點(diǎn)擊右上角關(guān)閉按鈕,關(guān)閉
? ? ? ? ? ? if event.type in (QUIT,KEYDOWN):
? ? ? ? ? ? ? ? pygame.quit()
? ? ? ? ? ? ? ? sys.exit()
? ? ? ? screen.fill(screen_color)? #清屏,重新開(kāi)始
? ? ? ? #畫棋盤
? ? ? ? for i in range(27,670,44):
? ? ? ? ? ? #先畫豎線
? ? ? ? ? ? if i==27 or i==670-27: #邊緣線稍微粗一些
? ? ? ? ? ? ? ? pygame.draw.line(screen,line_color,[i,27],[i,670-27],4)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? pygame.draw.line(screen,line_color,[i,27],[i,670-27],2)
? ? ? ? ? ? #先畫橫線
? ? ? ? ? ? if i==27 or i==670-27: #邊緣線稍微粗一些
? ? ? ? ? ? ? ? pygame.draw.line(screen,line_color,[27,i],[670-27,i],4)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? pygame.draw.line(screen,line_color,[27,i],[670-27,i],2)
? ? ? ? #在棋盤中心畫個(gè)小圓表示正中心位置
? ? ? ? pygame.draw.circle(screen,line_color,[27+44*7,27+44*7],8,0)
? ? ? ? for val in over_pos: #顯示所有落下的棋子
? ? ? ? ? ? pygame.draw.circle(screen,val[1],val[0],20,0)
? ? ? ? #判斷是否存在五子連心
? ? ? ? res=check_win(over_pos)
? ? ? ? if res[0]!=0:
? ? ? ? ? ? for pos in res[1]:
? ? ? ? ? ? ? ? pygame.draw.rect(screen,[238,48,167],[pos[0]*44+27-22,pos[1]*44+27-22,44,44],2,5)
? ? ? ? ? ? pygame.display.update()? #刷新顯示,需要刷新后在界面顯示變化
? ? ? ? if res[0]==2:
? ? ? ? ? ? # Tk().wm_withdraw()
? ? ? ? ? ? # messagebox.showinfo("白棋獲勝","恭喜白棋一方獲勝")
? ? ? ? ? ? message_rt(u"恭喜白棋一方獲勝")? #贏家信息顯示
? ? ? ? ? ? pygame.display.update()
? ? ? ? ? ? pygame.time.wait(1000)
? ? ? ? ? ? break? #游戲結(jié)束,停止下面的操作
? ? ? ? elif res[0]==1:
? ? ? ? ? ? # Tk().wm_withdraw()
? ? ? ? ? ? # messagebox.showinfo("黑棋獲勝","恭喜黑棋一方獲勝")
? ? ? ? ? ? message_rt(u"恭喜黑棋一方獲勝") #贏家信息顯示
? ? ? ? ? ??pygame.display.update()
? ? ? ? ? ? pygame.time.wait(1000)
? ? ? ? ? ? break? #游戲結(jié)束,停止下面的操作
? ? ? ? #獲取鼠標(biāo)的坐標(biāo),調(diào)用函數(shù)find_pos,判斷是否可以落子
? ? ? ? x,y=pygame.mouse.get_pos()
? ? ? ? x,y=find_pos(x,y)
? ? ? ? pygame.draw.rect(screen,[0,229,238],[x-22,y-22,44,44],2,1)
? ? ? ? #鼠標(biāo)左鍵表示落子,tm用來(lái)延時(shí)的,因?yàn)槊看窝h(huán)時(shí)間間隔很斷,容易導(dǎo)致明明只按了一次左鍵,卻被多次獲取,認(rèn)為我按了多次
? ? ? ? keys_pressed=pygame.mouse.get_pressed() #獲取鼠標(biāo)按鍵信息
? ? ? ? if keys_pressed[0] and tm ==0:
? ? ? ? ? ? flag=True
? ? ? ? ? ? if check_over_pos(x,y,over_pos):
? ? ? ? ? ? ? ? if len(over_pos)%2==0:
? ? ? ? ? ? ? ? ? ? over_pos.append([[x,y],black_color])
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? over_pos.append([[x,y],white_color])
? ? ? ? #鼠標(biāo)左鍵的延時(shí)作用
? ? ? ? if flag:
? ? ? ? ? ? tm +=1
? ? ? ? if tm%20==0:? #延遲時(shí)間設(shè)定20ms
? ? ? ? ? ? flag=False
? ? ? ? ? ? tm=0
? ? ? ? for val in over_pos:
? ? ? ? ? ? pygame.draw.circle(screen,val[1],val[0],20,0)
? ? ? ? pygame.display.update()
? ? ?程序打包為exe:
準(zhǔn)備.ico圖標(biāo)和上面py文件,cmd輸入指令,然后執(zhí)行完成后找到對(duì)應(yīng)exe保存路徑即可。
1人點(diǎn)贊文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-477644.html
技術(shù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-477644.html
到了這里,關(guān)于Pygame 五子棋編程的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!