import tkinter as tk
import sys
import pygame
global flag
global list,list1,list2
list=[]
list1=[]
list2=[]
flag=1
win21 = tk.Tk()
win21.title("五子棋")
win21.geometry("500x500")
L324 = tk.Label(win21, text="棋王争霸", font=("Arial", 20)
, width=15, height=3)
L324.pack()
pygame.mixer.init()
pq = pygame.mixer.music.load("./76.mp3")
pygame.mixer.music.play()
def gameover1():
pk=tk.Tk()
pk.title("Gameover!")
pk.geometry("500x500")
L325 = tk.Label(pk,text="黑棋获胜",font=("Arial", 20)
, width=15, height=3)
L325.pack()
cjk = tk.Button(pk, text="退出游戏", bg="yellow", font=("Arial", 20)
, width=15, height=4, command=exit)
cjk.pack()
pk.mainloop()
def gameover2():
pk1=tk.Tk()
pk1.title("Gameover!")
pk1.geometry("500x500")
L325 = tk.Label(pk1,text="白棋获胜",font=("Arial", 20)
, width=15, height=3)
L325.pack()
cjk =tk.Button(pk1, text="退出游戏", bg="yellow", font=("Arial", 20)
, width=15, height=4, command=exit)
cjk.pack()
pk1.mainloop()
def winner(width,heigth):
kpl=0
kpl1=0
for i in list1:
if (i[0],i[1])in list1:
for j in range(1,5):#1
if (i[0]+j*width,i[1])in list1:
kpl=0
else:
kpl=1
break
if kpl==0:
return 1
for j in range(1,5):#2
if (i[0],i[1]+j*heigth)in list1:
kpl=0
else:
kpl=1
break
if kpl==0:
return 1
for j in range(1,5):#3
if (i[0]+j*width,i[1]+j*heigth)in list1:
kpl=0
else:
kpl=1
break
if kpl==0:
return 1
for j in range(1,5):#4
if (i[0]+j*width,i[1]-j*heigth)in list1:
kpl=0
else:
kpl=1
break
if kpl==0:
return 1
for i in list2:
for j in range(1,5):#1
if (i[0]+j*width,i[1])in list2:
kpl1=0
else:
kpl1=1
break
if kpl1==0:
return 0
for j in range(1,5):#2
if (i[0],i[1]+j*heigth)in list2:
kpl1=0
else:
kpl1=1
break
if kpl1==0:
return 0
for j in range(1,5):#3
if (i[0]+j*width,i[1]+j*heigth)in list2:
kpl1=0
else:
kpl1=1
break
if kpl1==0:
return 0
for j in range(1,5):#4
if (i[0]+j*width,i[1]-j*heigth)in list2:
kpl1=0
else:
kpl1=1
break
if kpl1==0:
return 0
def play2(): # 构建棋盘
global flag
pygame.init()
R = 600 # r是行
C = 600 # c是列
size = (R, C)
r = 25
c = 25
width = C / c # 单行宽
heigth = R / r # 单行高
screen = pygame.display.set_mode(size)
pygame.display.set_caption("五子棋")
pygame.draw.rect(screen, (238, 154, 73), (0, 0, R, C))
def oto(screen,x,y,R,C,width,heigth,r,c):
global flag,list,list1,list2
if x>=width and x<=C-width:
if y>=heigth and y<=R-heigth:
for i in range(1,r):
if x>=i*width and x<=(i+0.5)*width:
x=i*width
elif x>(i+0.5)*width and x<=(i+1)*width:
x=(i+1)*width
for i in range(1,c):
if y>=i*heigth and y<=(i+0.5)*heigth:
y=i*heigth
elif y>(i+0.5)*heigth and y<=(i+1)*heigth:
y=(i+1)*heigth
if flag==1:
if (x,y)not in list:
pygame.draw.circle(screen, "black", (x, y), 10)
list.append((x,y))
list1.append((x,y))
flag*=-1
else:
if (x,y)not in list:
pygame.draw.circle(screen, "white", (x, y), 10)
list.append((x,y))
list2.append((x,y))
flag *= -1
for i in range(1, r):
pygame.draw.line(screen, "pink", [width * i, heigth], [width * i, R-heigth], width=2)
for i in range(1, c):
pygame.draw.line(screen, "pink", [width, heigth * i], [C-width, heigth * i], width=2)
t= True
while t:
for event in pygame.event.get():
if event.type == pygame.QUIT:
t=False
pygame.quit()
sys.exit()
tim=0
x,y= pygame.mouse.get_pos()
keys_pressed = pygame.mouse.get_pressed() # 获取鼠标按键信息
if keys_pressed[0] and tim == 0:
oto(screen,x,y,R,C,width,heigth,r,c)
john=winner(width,heigth)
if john==1:#黑棋胜利
gameover1()
elif john==0:#白棋胜利
gameover2()
pygame.display.update()
bkpq = tk.Button(win21, text="开始游戏", bg="yellow", font=("Arial", 20)
, width=15, height=4, command=play2)
cjkl = tk.Button(win21, text="退出游戏", bg="yellow", font=("Arial", 20)
, width=15, height=4, command=exit)
bkpq.pack()
cjkl.pack()
win21.mainloop()
只是稍微用了一点tkinter里的简单页面,pygame库学起来很简单,程序相对简单,适合没有学过的小白选手学习,都是查资料可以看懂的大佬完全可以跳过这种没有难度的游戏,毕竟本人也只是菜鸟一只