Python语言 期末大作业 五子棋 (资源在博主资源拿)

        博主代码仅供参考,由于是边做边打补丁补功能,也没有时间去重构代码,所以质量不敢保证(doge)QAQ。。。。

        当然给大家给个参考,同样作为模板也能较好的去修改,怕麻烦的uu也可以直接cv过去应付大作业(bushi)

        阅读格式:代码+解释

效果展示

Python语言 期末大作业 五子棋 (资源在博主资源拿)_第1张图片

Python语言 期末大作业 五子棋 (资源在博主资源拿)_第2张图片

Python语言 期末大作业 五子棋 (资源在博主资源拿)_第3张图片

        这就是主要的三个界面,是调用tkinter包做的(第一次用TK,勿喷),其中注册登录界面,积分胜率需要去写入和读出txt文件,至于游戏界面增加了悔棋和人机(剩下的来不及肝了QAQ),接下来我们进行代码段的解读。

一.登录界面

代码段:

def framLogin():
    global user_entry, ss_entry, title_lab, login_btn, userlab, sslab, user, ss, w3,user_goal
    w3 = Canvas(game, width=600, height=400, background='tan')
    w3.pack()
    title_lab = tk.Label(w3, text="请登录", bg='tan', font="仿宋 20 bold")
    title_lab.place(x=280, y=50, )
    userlab = tk.Label(w3, text="用户名", bg='tan', font="仿宋 20 bold", width=8)
    userlab.place(x=100, y=130)
    user_entry = tk.Entry(w3, width=15, bg="white", font="仿宋 20 bold")
    user_entry.place(x=230, y=130)
    sslab = tk.Label(w3, text="登录密码", bg='tan', font="仿宋 20 bold", width=8)
    sslab.place(x=85, y=200)
    ss_entry = tk.Entry(w3, width=15, bg="white", font="仿宋 20 bold", show="*")
    ss_entry.place(x=230, y=200)
    login_btn = tk.Button(w3, text="登录", command=judge, font="仿宋 20 bold", bg="green", width=8)
    regist_btn = tk.Button(w3, text="注册", command=resgister, font="仿宋 20 bold", bg="green", width=8)
    login_btn.place(x=150, y=300)
    regist_btn.place(x=350, y=300)

framLogin()函数:

        可以看到,这段代码就是调用TK做的可视化界面,因为root(桌子)在另一个函数已经初始化了,所以我们在这直接引用,将建立好的Canvas直接放在桌子上就好,这段代码比较简单,我们直接跳过。

def judge():
    global user_entry, ss_entry, game,user_goal
    temp = False
    with open("sql", "r") as f:
        data = f.readlines()
        for i in range(0, len(data), 4):
            data[i] = data[i].strip("\n")
            data[i+1] = data[i+1].strip("\n")
            print(user_entry.get())
            if str(data[i]) == user_entry.get() and str(data[i + 1]) == ss_entry.get():
                user_goal = user_entry.get()
                temp = True
                break
    if temp:
        game.destroy()
        game = Tk()
        game.title("五子棋")
        game.iconphoto(True, tk.PhotoImage(file='image.png'))
        game.geometry("600x710")
        game.resizable(False,False)
        locating()
        framinit()
        mainloop()
    else:
        tkinter.messagebox.showinfo('提示', '密码或用户名不正确')

judge()函数:

        judge = 判断,旨在判断我们名为sql.txt的文件里是否存在用户输入的用户名及密码,temp为临时判别,遍历txt如果没有此用户或密码不正确,都会向用户报错。在if temp后,则为通过验证的代码段,通过locating(),framinit()这两个自定义函数去完成用户信息定位,游戏界面初始化两个功能(这两个函数后面有解读)

sql.txt补充:因为是大作业,也没有想到去用更高级的手段储存(懒),所以这是我的txt:           

可以看到,第一行是用户名,接下来是密码,三四行分别是储存的玩家胜场和机器胜场(12:25,被机器暴打)

二.注册界面

def resgister():
    global w4, u_entry, s_entry, re
    re = Tk()
    re.title("注册页面")
    re.geometry("600x400")
    re.resizable(False,False)
    w4 = Canvas(re, width=600, height=400, background='tan')
    title = tk.Label(w4, text="注册", bg='tan', font="仿宋 20 bold")
    title.place(x=280, y=50, )
    user = tk.Label(w4, text="用户名", bg='tan', font="仿宋 20 bold", width=8)
    user.place(x=100, y=130)
    u_entry = tk.Entry(w4, width=15, bg="white", font="仿宋 20 bold")
    u_entry.place(x=230, y=130)
    slab = tk.Label(w4, text="密码", bg='tan', font="仿宋 20 bold", width=8)
    slab.place(x=85, y=200)
    s_entry = tk.Entry(w4, width=15, bg="white", font="仿宋 20 bold", show="*")
    s_entry.place(x=230, y=200)
    agree_btn = tk.Button(w4, text="确认", command=registing, font="仿宋 20 bold", bg="green", width=8)
    agree_btn.place(x=250, y=280)
    w4.pack()

resgister()函数:

        这个函数一眼顶针,大家可能一下子就看出来是什么作用了,不就是注册界面的页面元素么,当然,重点是agree_btn = tk.Button(w4, text="确认", command=registing, font="仿宋 20 bold", bg="green", width=8)这句中的command—> registing

如下列代码:

def registing():
    temp = True
    global userID, userSS, u_entry, s_entry
    with open("sql", "r") as f:
        data = f.readlines()
        for i in range(0, len(data), 4):
            data[i] = data[i].strip("\n")
            if str(data[i]) == u_entry.get():
                tkinter.messagebox.showinfo('提示', '该用户名已被注册')
                temp = False
                break
    if temp:
        with open("sql", "a") as f:
            f.write(f'\r{u_entry.get()}')
            f.write(f'\r{s_entry.get()}')
            f.write("\r0")
            f.write("\r1")
        re.destroy()

registing()函数:

        首先,一个注册界面就需要我们去判断用户是否存在,参照judge()函数,我们建一个temp变量,通过对文本的操作去判别是否存在,如果存在就对用户抛出错误提示。但如果符合条件,就去追加文件,添加用户信息。

三.游戏界面初始化

def framinit():
    global im, image, w2, w2q, w2s, w2t
    im = Image.open('1.jpeg').resize((600, 600))
    image = ImageTk.PhotoImage(im)
    w2 = Canvas(game, width=600, height=600)
    w2.create_image(300, 300, image=image)
    w2t = Button(game, text="双人模式", width=10, height=1, command=framGame, font=('楷体', 15))
    w2s = Button(game, text="单人模式", width=10, height=1, command=act, font=('楷体', 15))
    w2q = Button(game, text="退出", width=10, height=1, command=quit, font=('楷体', 15))
    w2.pack()
    w2t.pack()
    w2s.pack()
    w2q.pack()

framinit()函数:

        看一下文本可以知道,此段代码对应效果展示的第二张图,这里要提一下imimage两个变量,这两个变量要设为全局变量以此来保留,不然会被删除,由于TK模块只支持gif形式的图片,所以这里我们通过调用PIL.ImagePIL.ImageTk,去解析这张'1.jpeg',剩下的就是常规的按钮及其绑定的command(命令)。

def act():
    global act
    framGame()
    act = 1

act()函数:
        act()函数比较好理解,就是激活一个变量act = 1,以进入人机功能,然后激活framGame()函数进入游戏。

def framGame():
    global w2, w1,text1,text2
    w2s.destroy()
    w2.destroy()
    w2q.destroy()
    w2t.destroy()
    w1 = Canvas(width=600, height=600, background='tan')
    for i in range(0, 15):
        if i != 0 and i != 14:
            w1.create_line(i * 40 + 20, 20, i * 40 + 20, 580)
            w1.create_line(20, i * 40 + 20, 580, i * 40 + 20)
        else:
            w1.create_line(i * 40 + 20, 20, i * 40 + 20, 580, width=3)
            w1.create_line(20, i * 40 + 20, 580, i * 40 + 20, width=3)
    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')
    text1 = tk.Text(game,width=13,height=1.4,font=('楷体',30))
    text1.insert(tk.INSERT, f'黑棋 {b_score}:{w_score} 白棋')
    h_score,rate = calculate()
    text2 = tk.Text(game, width=33, height=1.4, font=('楷体', 12))
    text2.insert(tk.INSERT,f'用户{user_goal}: 胜率 = {rate} 积分 = {h_score}')
    u = Button(game, text="认输", width=10, height=1, command=run, font=('楷体', 15))
    r = Button(game, text="悔棋", width=10, height=1, command=laterstep, font=('楷体', 15))
    q = Button(game, text="退出", width=10, height=1, command=quit, font=('楷体', 15))
    w1.bind("

framGame()函数:

        这段代码是主体代码之一,我们来详细解释下,开头四个destroy()是为了清除前朝余党,然后for循环中绘制棋盘,其中create_line中的参数和创建的Canvas画布的大小也有绝对关系,请根据大小来调参,w1.create_oval(455, 455, 465, 465, fill='black')这行代码是为了绘制五个定位点,同样的思路,等会我们绘制棋子也可以使用create_oval函数来完成,接下来text1,text2,u,r,q分别是对应的几个页面元素,看看即可,但是注意w1.bind("

你可能感兴趣的:(python,开发语言)