井字棋
1.设计登录界面
1.1导入需要的工具包
import tkinter as tk
from PIL import Image, ImageTk
import pickle
from tkinter import messagebox
import subprocess
1.2窗口显示
window = tk.Tk()
window.title("Welcome")
window.geometry('500x600')
1.3登录界面图片显示
image = Image.open("welcome.gif")
photo = ImageTk.PhotoImage(image)
value = "GaoYue"
entry_var = tk.StringVar(window)
entry_var.set(value)
1.6标签按钮输入框显示
label1_image = tk.Label(window,image=photo)
label1_image.place(x=10,y=10)
label2 = tk.Label(window,text='User name',width=10,height=5)
label2.place(x=40,y=130)
label3 = tk.Label(window,text='password',width=10,height=5)
label3.place(x=40,y=180)
label4 = tk.Entry(window,width=30,bd=5)
label4.place(x=140,y=210)
label5 = tk.Entry(window,textvariable=entry_var,width=30,bd=5)
label5.place(x=140,y=160)
button1 = tk.Button(window,text='Loign',width=10,command=usr_login)
button1.place(x=140,y=280)
button2 = tk.Button(window,text='Sign up',width=10,command=usr_sign_up)
button2.place(x=250,y=280)
2.登录功能实现
2.1用户数据存储
def usr_login():
usr_name = label5.get()
usr_pwd=label4.get()
print(usr_name)
try:
with open("usrs_info.pickle",'rb') as usr_file:
print("1")
usrs_info = pickle.load(usr_file)
print(usrs_info)
except FileNotFoundError:
with open("usrs_info.pickle",'wb') as usr_file:
print("2")
usrs_info = {'admin':'admin'}
pickle.dump(usrs_info,usr_file)
解释:
2.2登录和注册
2.2.1登录功能实现
print("ok")
print("usr_name:",usr_name)
if usr_name in usrs_info:
print("3")
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title="Welcome",message="How are you"+usr_name)
tk.messagebox.showinfo(title="Game Start", message="Let's start the game!")
subprocess.run(["python", "飞机大战.py"])
else:
tk.messagebox.showerror(message="Error,your password is wrong,try again.")
else:
print("4")
is_sign_up = tk.messagebox.askyesno(title="Welcome",message="You have not sign up yet.sign up today?")
2.2.2注册功能实现
def sign_up():
nn = entry9.get()
np = entry10.get()
npf = entry11.get()
with open('usrs_info.pickle','rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
if np != npf:
tk.messagebox.showerror("Error","The user has already signed up!")
elif nn in exist_usr_info:
print("已经注册过了")
tk.messagebox.showerror("Error","The user has already signed up!")
else:
exist_usr_info[nn] = np
with open("usrs_info.pickle","wb") as usr_file:
pickle.dump(exist_usr_info,usr_file)
tk.messagebox.showinfo("Welcome","You have successfully signed up!")
window.destroy()
button3 = tk.Button(window, text='Sign up', width=10,command=sign_up)
button3.place(x=140, y=150)
print("开始注册")
3.井字棋游戏
3.1 导入需要的工具包
import tkinter as tk
from tkinter import messagebox
3.2 窗口显示
window = tk.Tk()
window.geometry("600x500")
window.title("Welcome")
3.2 按钮标签显示
label2 = tk.Label(text="请输入要落下棋子的位置(1-9):", width=30, height=2)
label2.pack()
label2.place(x=190, y=310)
entry2 = tk.Entry(width=20)
entry2.pack()
entry2.place(x=220, y=350)
button11 = tk.Button(text="落下棋子", width=10, height=2,command=message2)
button11.pack()
button11.place(x=250, y=380)
button22 = tk.Button(text="重新开始", width=10, height=2)
button22.pack()
button22.place(x=250, y=430)
3.3 棋盘设置初始状态
gameover = False
board = ['']*9
step = 0
3.4 游戏设计
3.4.1创建白布
canvas = tk.Canvas(window, width=300, height=300, bg="white")
canvas.pack()
3.4.2 画出九宫格
def draw_nume(canvas,width,height,line_color='black'):
cell_width = width / 3
cell_height = height / 3
for i in range(3):
x = i * cell_width
canvas.create_line(x, 0, x, height, fill=line_color) # 垂直线
for i in range(3):
y = i * cell_height
canvas.create_line(0, y, width, y, fill=line_color) # 水平线
在每个格子中心绘制数字或 “X”
for i in range(3):
for j in range(3):
x = i * cell_width + cell_width / 2
y = j * cell_height + cell_height / 2
index = i * 3 + j
if board[index] == 'X':
canvas.create_text(y, x, text="X", font=("Helvetica", 20))
else:
canvas.create_text(y, x, text=str(index + 1), font=("Helvetica", 20))
def message():
Iemet = entry2.get()
if not Iemet.isdigit():
tk.messagebox.showinfo("输入错误", "请输入一个数字。")
elif int(Iemet) > 9:
tk.messagebox.showinfo("输入错误", "输入的数字不能大于 9。")
3.6设置输赢条件
def iswins():
global gameover
wins = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
for w in wins:
if (board[w[0]]!=" " and board[w[0]]==board[w[1]]==board[w[2]]):
gameover = True
tk.messagebox.showinfo(title="你别挣了",message="我赢了")
return
if step>=5:
gameover =True
return tk.messagebox.showinfo(title="你别挣了",message="都没有赢了")
return
3.7 进一步处理用户输入
def message2():
global board
global step
Iemet = entry2.get()
if Iemet.isdigit():
number = int(Iemet)
if 1 <= number <= 9:
index = number - 1
board[index] = 'X'
draw_nume(canvas, 300, 300)
is_Win()
step +=1
3.8设置AI步数
index = ai.AI().decide("AI","高越",board)
r = 20
canvas.create_oval()
board[index] = "AI"
iswins()
3.9 清空棋盘状态
def reset():
global canvas
canvas.delete('A')