python+pygame實(shí)現(xiàn)簡(jiǎn)易五子棋小游戲的三種方式
tkinter庫(kù):Python的標(biāo)準(zhǔn)Tk GUI工具包的接口
示例:
from tkinter import * root = Tk() #你的ui代碼 Label(root,text = 'hello world!').pack() root.mainloop()
彈窗結(jié)果:

五子棋小游戲?qū)崿F(xiàn)(一):
from tkinter import *
import tkinter.messagebox # 彈窗庫(kù)
import numpy as np
root = Tk() #創(chuàng)建窗口
root.title("五子棋游戲") #窗口名字
w1 = Canvas(root, width=600,height=600,background='chocolate')
w1.pack()
for i in range(0, 15):
w1.create_line(i * 40 + 20, 20, i * 40 + 20, 580)
w1.create_line(20, i * 40 + 20, 580, i * 40 + 20)
w1.create_oval(135, 135, 145, 145,fill='black')
w1.create_oval(135, 455, 145, 465,fill='black')
w1.create_oval(465, 135, 455, 145,fill='black')
w1.create_oval(455, 455, 465, 465,fill='black')
w1.create_oval(295, 295, 305, 305,fill='black')
num=0
A=np.full((15,15),0)
B=np.full((15,15),'')
def callback(event):
global num ,A
for j in range (0,15):
for i in range (0,15):
if (event.x - 20 - 40 * i) ** 2 + (event.y - 20 - 40 * j) ** 2 <= 2 * 20 ** 2:
break
if (event.x - 20 - 40 * i) ** 2 + (event.y - 20 - 40 * j) ** 2 <= 2*20 ** 2:
break
if num % 2 == 0 and A[i][j] != 1:
w1.create_oval(40*i+5, 40*j+5, 40*i+35, 40*j+35,fill='black')
A[i][j] = 1
B[i][j] = 'b'
num += 1
if num % 2 != 0 and A[i][j] != 1 :
w1.create_oval(40*i+5, 40*j+5, 40*i+35, 40*j+35,fill='white')
A[i][j] = 1.
B[i][j] = 'w'
num += 1
f = [[-1, 0], [-1, 1], [0, 1], [1, 1]]
for z in range(0, 4):
a, b = f[z][0], f[z][1]
count1, count2 = 0, 0
x, y = i, j
while B[x][y] == B[i][j]:
count1 += 1
if x + a >= 0 and y + b >= 0 and x + a < 15 and y + b < 15 and B[x + a][y + b] == B[i][j]:
[x, y] = np.array([x, y]) + np.array([a, b])
else:
x, y = i, j
break
while B[x][y] == B[i][j]:
count2 += 1
if x - a < 15 and y - b < 15 and x - a >= 0 and y - b >= 0 and B[x - a][y - b] == B[i][j]:
[x, y] = np.array([x, y]) - np.array([a, b])
else:
break
if count1 + count2 == 6:
if B[i][j] == 'b':
tkinter.messagebox.showinfo('提示', '黑棋獲勝')
else:
tkinter.messagebox.showinfo('提示', '白棋獲勝')
w1.bind("<Button -1>",callback)
w1.pack()
def quit():
root.quit()
u=Button(root,text="退出",width=10,height=1,command=quit,font=('楷體',15))
u.pack()
mainloop()運(yùn)行結(jié)果:



此程序確定勝利后會(huì)繼續(xù)在同一棋盤(pán)上繼續(xù)下棋,沒(méi)有刷新棋盤(pán)
w1 = Canvas(root, width=600,height=600,background='chocolate')可根據(jù)參數(shù)background改變棋盤(pán)顏色

五子棋小游戲?qū)崿F(xiàn)(二):
#調(diào)用pygame庫(kù)
import pygame
import sys
#調(diào)用常用關(guān)鍵字常量
from pygame.locals import QUIT,KEYDOWN
import numpy as np
#初始化pygame
pygame.init()
#獲取對(duì)顯示系統(tǒng)的訪問(wèn),并創(chuàng)建一個(gè)窗口screen
#窗口大小為670x670
screen = pygame.display.set_mode((670,670))
screen_color=[238,154,73]#設(shè)置畫(huà)布顏色,[238,154,73]對(duì)應(yīng)為棕黃色
line_color = [0,0,0]#設(shè)置線條顏色,[0,0,0]對(duì)應(yīng)黑色
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:
mp[x][y]=2#表示白子
else:
mp[x][y]=1#表示黑子
for i in range(15):
pos1=[]
pos2=[]
for j 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 j in range(15):
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):
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):
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,[]]
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
def check_over_pos(x,y,over_pos):#檢查當(dāng)前的位置是否已經(jīng)落子
for val in over_pos:
if val[0][0]==x and val[0][1]==y:
return False
return True#表示沒(méi)有落子
flag=False
tim=0
over_pos=[]#表示已經(jīng)落子的位置
white_color=[255,255,255]#白棋顏色
black_color=[0,0,0]#黑棋顏色
while True:#不斷訓(xùn)練刷新畫(huà)布
for event in pygame.event.get():#獲取事件,如果鼠標(biāo)點(diǎn)擊右上角關(guān)閉按鈕,關(guān)閉
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill(screen_color)#清屏
for i in range(27,670,44):
#先畫(huà)豎線
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)
#再畫(huà)橫線
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)
#在棋盤(pán)中心畫(huà)個(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,1)
pygame.display.update()#刷新顯示
continue#游戲結(jié)束,停止下面的操作
#獲取鼠標(biāo)坐標(biāo)信息
x,y = pygame.mouse.get_pos()
x,y=find_pos(x,y)
if check_over_pos(x,y,over_pos):#判斷是否可以落子,再顯示
pygame.draw.rect(screen,[0 ,229 ,238 ],[x-22,y-22,44,44],2,1)
keys_pressed = pygame.mouse.get_pressed()#獲取鼠標(biāo)按鍵信息
#鼠標(biāo)左鍵表示落子,tim用來(lái)延時(shí)的,因?yàn)槊看窝h(huán)時(shí)間間隔很斷,容易導(dǎo)致明明只按了一次左鍵,卻被多次獲取,認(rèn)為我按了多次
if keys_pressed[0] and tim==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:
tim+=1
if tim%50==0:#延時(shí)200ms
flag=False
tim=0
pygame.display.update()#刷新顯示Pygame是一個(gè)跨平臺(tái)Python庫(kù),包含圖像、聲音。建立在SDL基礎(chǔ)上,允許實(shí)時(shí)電子游戲研發(fā)而無(wú)需被低級(jí)語(yǔ)言(如機(jī)器語(yǔ)言和匯編語(yǔ)言)束縛?;谶@樣一個(gè)設(shè)想,所有需要的游戲功能和理念都(主要是圖像方面)都完全簡(jiǎn)化為游戲邏輯本身,所有的資源結(jié)構(gòu)都可以由高級(jí)語(yǔ)言提供,如Python。
運(yùn)行結(jié)果:

此程序會(huì)以紅框方式顯示勝利,但無(wú)法刷新棋盤(pán),游戲過(guò)程中任何鍵盤(pán)按鍵觸碰都會(huì)導(dǎo)致游戲退出
五子棋小游戲?qū)崿F(xiàn)(三):
同樣依賴于Pygame庫(kù)
#coding:utf-8
import sys
import pygame
import random
def do():
def black(x, y):
a = 20
b = 20
c = 20
d = 0
for i in range(50):
pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (10/(d-5)+10)*1.6)
a += 1
b += 1
c += 1
d += 0.08
pygame.display.update()
def white(x, y):
a = 170
b = 170
c = 170
d = 0
for i in range(50):
pygame.draw.circle(screen, (a, b, c), [19.5 + 32 * x, 19.5 + 32 * y], (10/(d-5)+10)*1.6)
a += 1
b += 1
c += 1
d += 0.08
pygame.display.update()
pygame.init()
screen = pygame.display.set_mode((615, 615))
pygame.display.set_caption('五子棋')
screen.fill("#DD954F")
a = pygame.Surface((603, 603), flags=pygame.HWSURFACE)
a.fill(color='#121010')
b = pygame.Surface((585, 585), flags=pygame.HWSURFACE)
b.fill(color="#DD954F")
c = pygame.Surface((579, 579), flags=pygame.HWSURFACE)
c.fill(color='#121010')
d = pygame.Surface((576, 576), flags=pygame.HWSURFACE)
d.fill(color="#DD954F")
e = pygame.Surface((31, 31), flags=pygame.HWSURFACE)
e.fill(color="#DD954F")
screen.blit(a, (6.5, 6.5))
screen.blit(b, (15, 15))
screen.blit(c, (18, 18))
for j in range(18):
for i in range(18):
screen.blit(e, (20 + 32 * i, 20 + 32 * j))
alist = []
for j in range(19):
alistone = []
for i in range(19):
alistone.append(0)
alist.append(alistone)
pygame.draw.circle(screen, '#121010', [307.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 307.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [115.5, 115.5], 5)
pygame.draw.circle(screen, '#121010', [499.5, 115.5], 5)
pygame.draw.circle(screen, '#121010', [307.5, 499.5], 5)
pygame.draw.circle(screen, '#121010', [307.5, 115.5], 5)
pygame.display.flip()
wb = "black"
font1 = pygame.font.SysFont('stxingkai', 70)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
x = round((x - 19.5) / 32)
y = round((y - 19.5) / 32)
if x < 0:
x = 0
if x > 18:
x = 18
if y < 0:
y = 0
if y > 18:
y = 18
z = False
if alist[x][y] == 0:
eval(wb + "({},{})".format(x, y))
if wb == "black":
alist[x][y] = 1
wb1 = "黑棋"
wb = "white"
elif wb == "white":
alist[x][y] = 2
wb1 = "白棋"
wb = "black"
xx = x
yy = y
while True:
if xx == 0:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
break
else:
xx -= 1
num = 0
while True:
if xx == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
yy += 1
break
else:
yy -= 1
num = 0
while True:
if yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
yy += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if xx == 0:
break
elif yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
yy += 1
break
else:
xx -= 1
yy -= 1
num = 0
while True:
if xx == 18:
break
elif yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
yy += 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
xx = x
yy = y
while True:
if xx == 0:
break
elif yy == 18:
break
elif alist[xx][yy] != alist[x][y]:
xx += 1
yy -= 1
break
else:
xx -= 1
yy += 1
num = 0
while True:
if xx == 18:
break
elif yy == 0:
break
elif alist[xx][yy] != alist[x][y]:
break
else:
xx += 1
yy -= 1
num += 1
if num >= 5:
pygame.font.init()
text = font1.render("{}贏了".format(wb1), True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (307.5, 307.5)
screen.blit(text, textRect)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
do()
do()運(yùn)行結(jié)果:

顯示某方棋子勝利之后,鼠標(biāo)點(diǎn)擊即可刷新棋盤(pán)重新開(kāi)始

以實(shí)際效果來(lái)看,目前 五子棋小游戲?qū)崿F(xiàn)(三)實(shí)現(xiàn)效果最優(yōu)
到此這篇關(guān)于python實(shí)現(xiàn)簡(jiǎn)易五子棋小游戲的三種方式的文章就介紹到這了,更多相關(guān)python簡(jiǎn)易五子棋內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)微信打飛機(jī)游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信打飛機(jī)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
pycharm安裝django框架詳細(xì)圖文教程(指定版本)
這篇文章主要給大家介紹了關(guān)于pycharm安裝django框架(指定版本)的相關(guān)資料,PyCharm是一種Python?IDE,帶有一整套可以幫助用戶在使用Python語(yǔ)言開(kāi)發(fā)時(shí)提高其效率的工具,需要的朋友可以參考下2023-10-10
python 讀取dicom文件,生成info.txt和raw文件的方法
今天小編就為大家分享一篇python 讀取dicom文件,生成info.txt和raw文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
分享Python開(kāi)發(fā)中要注意的十個(gè)小貼士
不管是python開(kāi)發(fā)還是其他什么語(yǔ)言的開(kāi)發(fā),如果在開(kāi)發(fā)中我們能掌握一些有用的貼士和技巧,那么肯定會(huì)大大提高我們的開(kāi)發(fā)效率,今天小編和大家分享的就是python開(kāi)發(fā)中,一些初學(xué)這門語(yǔ)言常常會(huì)犯的錯(cuò)誤,一起來(lái)看看吧。2016-08-08
Python中threading.Timer()定時(shí)器實(shí)現(xiàn)定時(shí)任務(wù)
本文主要介紹了Python中threading.Timer()定時(shí)器實(shí)現(xiàn)定時(shí)任務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實(shí)現(xiàn)線性表的順序
這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實(shí)現(xiàn)線性表的順序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Python實(shí)現(xiàn)序列化及csv文件讀取
這篇文章主要介紹了Python實(shí)現(xiàn)序列化及csv文件讀取,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Python將list元素轉(zhuǎn)存為CSV文件的實(shí)現(xiàn)
這篇文章主要介紹了Python將list元素轉(zhuǎn)存為CSV文件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

