浔川官方在浔川AI五子棋v1.4的基础上,改变了画布、选项等,为广大五子棋爱好者提供最方便的游戏体验。
正式代码:
# -*- coding: utf-8 -*-
import tkinter as tk
import tkinter.messagebox
import pickle
import random
# 窗口
window = tk.Tk()
window.title('AI五子棋登录界面')
window.geometry('450x200')
# 画布放置图片
# canvas=tk.Canvas(window,height=300,width=500)
# imagefile=tk.PhotoImage(file='qm.png')
# image=canvas.create_image(0,0,anchor='nw',image=imagefile)
# canvas.pack(side='top')
# 标签 用户名密码
Verification_Code = random.randint(1000, 9999)#设置一个随机的四位数
Verification_Code = str(Verification_Code)#把类型转换为str型
print(type(Verification_Code))
tk.Label(window, text='用户名:').place(x=100, y=30)
tk.Label(window, text='密码:').place(x=100, y=70)
tk.Label(window, text='验证码').place(x=100, y=110)
tk.Label(window, text=Verification_Code).place(x=320, y=110)
# 用户名输入框
var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=30)
# 密码输入框
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=70)
#验证码输入框
var_usr_yzm = tk.StringVar()
entry_usr_yzm = tk.Entry(window, textvariable=var_usr_yzm)
entry_usr_yzm.place(x=160, y=110)
# 登录函数
def usr_log_in():
# 输入框获取用户名密码
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
usr_yzm = var_usr_yzm.get()
#测试类型
print(type(usr_yzm),type(Verification_Code))
# 从本地字典获取用户信息,如果没有则新建本地数据库
try:
with open('usr_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usr_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
# 判断验证码是否正确用户名和密码是否匹配
if usr_yzm == Verification_Code:
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='welcome',
message='欢迎您:' + usr_name)
else:
tk.messagebox.showerror(message='密码错误')
# 用户名密码不能为空
elif usr_name == '' or usr_pwd == '':
tk.messagebox.showerror(message='用户名或密码为空')
# 不在数据库中弹出是否注册的框
else:
is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册')
if is_signup:
usr_sign_up()
elif usr_yzm == '':
tk.messagebox.showerror(message='验证码不能为空')
else:
tk.messagebox.showerror(message='验证码有误!')
# 注册函数
def usr_sign_up():
# 确认注册时的相应函数
def signtowcg():
# 获取输入框内的内容
nn = new_name.get()
np = new_pwd.get()
npf = new_pwd_confirm.get()
# 本地加载已有用户信息,如果没有则已有用户信息为空
try:
with open('usr_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
except FileNotFoundError:
exist_usr_info = {}
# 检查用户名存在、密码为空、密码前后不一致
if nn in exist_usr_info:
tk.messagebox.showerror('错误', '用户名已存在')
elif np == '' or nn == '':
tk.messagebox.showerror('错误', '用户名或密码为空')
elif np != npf:
tk.messagebox.showerror('错误', '密码前后不一致')
# 注册信息没有问题则将用户名密码写入数据库
else:
exist_usr_info[nn] = np
with open('usr_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('欢迎', '注册成功')
# 注册成功关闭注册框
window_sign_up.destroy()
# 新建注册界面
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注册')
# 用户名变量及标签、输入框
new_name = tk.StringVar()
tk.Label(window_sign_up, text='用户名:').place(x=10, y=10)
tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
# 密码变量及标签、输入框
new_pwd = tk.StringVar()
tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50)
tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50)
# 重复密码变量及标签、输入框
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90)
tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90)
# 确认注册按钮及位置
bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册',
command=signtowcg)
bt_confirm_sign_up.place(x=150, y=130)
# 退出的函数
def usr_sign_quit():
window.destroy()
# 登录 注册按钮
bt_login = tk.Button(window, text='登录', command=usr_log_in)
bt_login.place(x=140, y=150)
bt_logup = tk.Button(window, text='注册', command=usr_sign_up)
bt_logup.place(x=210, y=150)
bt_logquit = tk.Button(window, text='退出', command=usr_sign_quit)
bt_logquit.place(x=280, y=150)
# 主循环
window.mainloop()
#下载、安装
import tkinter as tk
import time
# 创建主窗口
window = tk.Tk()
window.title('进度条')
window.geometry('630x150')
# 设置下载进度条
tk.Label(window, text='下载进度:', ).place(x=50, y=60)
canvas = tk.Canvas(window, width=465, height=22, bg="white")
canvas.place(x=110, y=60)
# 显示下载进度
def progress():
# 填充进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for i in range(x):
n = n + 465 / x
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0.02) # 控制进度条流动的速度
# 清空进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for t in range(x):
n = n + 465 / x
# 以矩形的长度作为变量值更新
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0) # 时间为0,即飞速清空进度条
btn_download = tk.Button(window, text='开始下载', command=progress)
btn_download.place(x=400, y=105)
window.mainloop()
import tkinter as tk
import time
# 创建主窗口
window = tk.Tk()
window.title('进度条')
window.geometry('630x150')
# 设置下载进度条
tk.Label(window, text='安装进度:', ).place(x=50, y=60)
canvas = tk.Canvas(window, width=465, height=22, bg="white")
canvas.place(x=110, y=60)
# 显示下载进度
def progress():
# 填充进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for i in range(x):
n = n + 465 / x
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0.02) # 控制进度条流动的速度
# 清空进度条
fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
x = 500 # 未知变量,可更改
n = 465 / x # 465是矩形填充满的次数
for t in range(x):
n = n + 465 / x
# 以矩形的长度作为变量值更新
canvas.coords(fill_line, (0, 0, n, 60))
window.update()
time.sleep(0) # 时间为0,即飞速清空进度条
btn_download = tk.Button(window, text='开始安装', command=progress)
btn_download.place(x=400, y=105)
window.mainloop()
#五子棋
from tkinter import *
import math
#定义棋盘类
class chessBoard() :
def __init__(self) :
self.window = Tk()
self.window.title("五子棋游戏")
self.window.geometry("660x470")
self.window.resizable(0,0)
self.canvas=Canvas(self.window , bg="#EEE8AC" , width=470, height=470)
self.paint_board()
self.canvas.grid(row = 0 , column = 0)
def paint_board(self) :
for row in range(0,15) :
if row == 0 or row == 14 :
self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 2)
else :
self.canvas.create_line(25 , 25+row*30 , 25+14*30 , 25+row*30 , width = 1)
for column in range(0,15) :
if column == 0 or column == 14 :
self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 ,width = 2)
else :
self.canvas.create_line(25+column*30 ,25, 25+column*30 , 25+14*30 , width = 1)
self.canvas.create_oval(112, 112, 118, 118, fill="black")
self.canvas.create_oval(352, 112, 358, 118, fill="black")
self.canvas.create_oval(112, 352, 118, 358, fill="black")
self.canvas.create_oval(232, 232, 238, 238, fill="black")
self.canvas.create_oval(352, 352, 358, 358, fill="black")
#定义五子棋游戏类
#0为黑子 , 1为白子 , 2为空位
class Gobang() :
#初始化
def __init__(self) :
self.board = chessBoard()
self.game_print = StringVar()
self.game_print.set("")
#16*16的二维列表,保证不会out of index
self.db = [([2] * 16) for i in range(16)]
#悔棋用的顺序列表
self.order = []
#棋子颜色
self.color_count = 0
self.color = 'black'
#清空与赢的初始化,已赢为1,已清空为1
self.flag_win = 1
self.flag_empty = 1
self.options()
#黑白互换
def change_color(self) :
self.color_count = (self.color_count + 1 ) % 2
if self.color_count == 0 :
self.color = "black"
elif self.color_count ==1 :
self.color = "white"
#落子
def chess_moving(self ,event) :
#不点击"开始"与"清空"无法再次开始落子
if self.flag_win ==1 or self.flag_empty ==0 :
return
#坐标转化为下标
x,y = event.x-25 , event.y-25
x = round(x/30)
y = round(y/30)
#点击位置没用落子,且没有在棋盘线外,可以落子
while self.db[y][x] == 2 and self.limit_boarder(y,x):
self.db[y][x] = self.color_count
self.order.append(x+15*y)
self.board.canvas.create_oval(25+30*x-12 , 25+30*y-12 , 25+30*x+12 , 25+30*y+12 , fill = self.color,tags = "chessman")
if self.game_win(y,x,self.color_count) :
print(self.color,"获胜")
self.game_print.set(self.color+"获胜")
else :
self.change_color()
self.game_print.set("请"+self.color+"落子")
#保证棋子落在棋盘上
def limit_boarder(self , y , x) :
if x<0 or x>14 or y<0 or y>14 :
return False
else :
return True
#计算连子的数目,并返回最大连子数目
def chessman_count(self , y , x , color_count ) :
count1,count2,count3,count4 = 1,1,1,1
#横计算
for i in range(-1 , -5 , -1) :
if self.db[y][x+i] == color_count :
count1 += 1
else:
break
for i in range(1 , 5 ,1 ) :
if self.db[y][x+i] == color_count :
count1 += 1
else:
break
#竖计算
for i in range(-1 , -5 , -1) :
if self.db[y+i][x] == color_count :
count2 += 1
else:
break
for i in range(1 , 5 ,1 ) :
if self.db[y+i][x] == color_count :
count2 += 1
else:
break
#/计算
for i in range(-1 , -5 , -1) :
if self.db[y+i][x+i] == color_count :
count3 += 1
else:
break
for i in range(1 , 5 ,1 ) :
if self.db[y+i][x+i] == color_count :
count3 += 1
else:
break
#\计算
for i in range(-1 , -5 , -1) :
if self.db[y+i][x-i] == color_count :
count4 += 1
else:
break
for i in range(1 , 5 ,1 ) :
if self.db[y+i][x-i] == color_count :
count4 += 1
else:
break
return max(count1 , count2 , count3 , count4)
#判断输赢
def game_win(self , y , x , color_count ) :
if self.chessman_count(y,x,color_count) >= 5 :
self.flag_win = 1
self.flag_empty = 0
return True
else :
return False
#悔棋,清空棋盘,再画剩下的n-1个棋子
def withdraw(self ) :
if len(self.order)==0 or self.flag_win == 1:
return
self.board.canvas.delete("chessman")
z = self.order.pop()
x = z%15
y = z//15
self.db[y][x] = 2
self.color_count = 1
for i in self.order :
ix = i%15
iy = i//15
self.change_color()
self.board.canvas.create_oval(25+30*ix-12 , 25+30*iy-12 , 25+30*ix+12 , 25+30*iy+12 , fill = self.color,tags = "chessman")
self.change_color()
self.game_print.set("请"+self.color+"落子")
#清空
def empty_all(self) :
self.board.canvas.delete("chessman")
#还原初始化
self.db = [([2] * 16) for i in range(16)]
self.order = []
self.color_count = 0
self.color = 'black'
self.flag_win = 1
self.flag_empty = 1
self.game_print.set("")
#将self.flag_win置0才能在棋盘上落子
def game_start(self) :
#没有清空棋子不能置0开始
if self.flag_empty == 0:
return
self.flag_win = 0
self.game_print.set("请"+self.color+"落子")
def options(self) :
self.board.canvas.bind("<Button-1>",self.chess_moving)
Label(self.board.window , textvariable = self.game_print , font = ("Arial", 20) ).place(relx = 0, rely = 0 ,x = 495 , y = 200)
Button(self.board.window , text= "开始游戏" ,command = self.game_start,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=15)
Button(self.board.window , text= "我要悔棋" ,command = self.withdraw,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=60)
Button(self.board.window , text= "清空棋局" ,command = self.empty_all,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=105)
Button(self.board.window , text= "结束游戏" ,command = self.board.window.destroy,width = 13, font = ("Verdana", 12)).place(relx=0, rely=0, x=495, y=420)
self.board.window.mainloop()
if __name__ == "__main__":
game = Gobang()
使用步骤:
1:登录浔川AI五子棋
2:下载浔川AI五子棋
3:安装浔川AI五子棋
4:进入五子棋游戏(双人)